练习1: (1)写一个程序,打印从1到100的值。
package com.laputa.chapter4.practice1;public class Count {public static void main(String[] args) {for(int i = 1; i <= 100; i++){System.out.print(i + " ");}}
}
练习2: (2)写一个程序,产生25个int类型的随机数。对于每一个随机值,使用if-else语句来将其分类为大于、小于,或等于紧随它而随机生成的值。
package com.laputa.chapter4.practice2;import java.util.Random;public class RandomInts {static Random r = new Random(47);public static void compareRand() {int a = r.nextInt();int b = r.nextInt();System.out.println("a = " + a + ", b = " + b);if(a < b) {System.out.println("a < b");} else if(a > b) {System.out.println("a > b");} else {System.out.println("a = b");}}public static void main(String[] args) {for(int i = 0; i < 25; i++){compareRand();}}
}
练习3: (1) 修改练习2,把代码用一个while无限循环包括起来。然后运行它直至用键盘中断其运行(通常是通过按Ctrl-C)。
package com.laputa.chapter4.practice3;import com.laputa.chapter4.practice2.RandomInts;public class RandomInts2 {public static void main(String[] args) {// 死循环while (true){RandomInts.compareRand();}}
}
练习4: (3) 写一个程序,使用两个嵌套的for循环和取余操作符(%)来探测和打印素数(只能被其自身和1整除,而不能被其他数字整除的整数)。
package com.laputa.chapter4.practice4;public class Primes {public static void main(String[] args) {for(int i = 1; i < 1000; i++ ) {// 能整除个数统计int factors = 0;for(int j = 1; j < (i + 2)/2; j++ ) {if((i % j) == 0) {factors++;}}if(factors < 2) {System.out.println(i + " is prime");}}}
}
练习5: (4) 重复第3章中的练习10,不要用Integer.toBinaryString()方法,而是用三元操作符和按位操作符来显示二进制的1和0。
package com.laputa.chapter4.practice5;public class BitwiseOperators {public static void main(String[] args) {int i1 = 0xaaaaaaaa;int i2 = 0x55555555;System.out.print("i1 = ");toBinaryString(i1);System.out.println();System.out.print("i2 = ");toBinaryString(i2);}private static void toBinaryString(int i) {char[] buffer = new char[32];int index = 32;do {buffer[--index] = ((i & 0x01) != 0) ? '1' : '0';i >>>= 1;} while (i != 0);for(int j = index; j < 32; j++){System.out.print(buffer[j]);}}
}
练习6: (2)修改前两个程序中的两个test()方法,让它们接受两个额外的参数begin和end,这样在测试testvalI时将判断它是否在begin和end之间(包括begin和end) 的范围内。
package com.laputa.chapter4.practice6;public class E06_RangeTest2 {static boolean test(int testval, int begin, int end) {if(testval >= begin && testval <= end) {return true;}return false;}public static void main(String[] args) {System.out.println(test(10, 5, 15));System.out.println(test(5, 10, 15));System.out.println(test(5, 5, 5));}
}
练习7: (1) 修改本章练习1,通过使用break关键词,使得程序在打印到99时退出。然后尝试使用return来达到相同的目的。
package com.laputa.chapter4.practice7;public class E07_To98 {public static void main(String[] args) {for(int i = 1; i <= 100; i++) {if(i == 99) {break;}System.out.print(i + " ");}}
}
练习8: (2)写一个switch开关语句,为每个case打印一个消息。然后把这个switch放进for循环来测试每个case。先让每个case后面都有break,测试一下 会怎样;然后把break删了,看看会怎样。.
package com.laputa.chapter4.practice8;public class E08_SwitchDemo {public static void main(String[] args) {for(int i = 0; i < 7; i++) {switch(i) {case 1: System.out.println("case 1");break;case 2: System.out.println("case 2");break;case 3: System.out.println("case 3");break;case 4: System.out.println("case 4");break;case 5: System.out.println("case 5");break;default: System.out.println("default");}}}
}
//: control/E08_SwitchDemo2.java
// E08_SwitchDemo.java with the breaks removed.
package com.laputa.chapter4.practice8;public class E08_SwitchDemo2 {public static void main(String[] args) {for(int i = 0; i < 7; i++) {// 没了break,无论是否匹配,都会执行;switch(i) {case 1: System.out.println("case 1");case 2: System.out.println("case 2");case 3: System.out.println("case 3");case 4: System.out.println("case 4");case 5: System.out.println("case 5");default: System.out.println("default");}}}
}
练习9: (4) 一个斐波那契数列是由数字1、1、 2、3、5、8、13. 21、34等等组成的,其中每一个数字(从第三个数字起)都是前两个数字的和。创建一个方法, 接受一个整数参数, 并显示从第一个元素开始总共由该参数指定的个数所构成的所有斐波那契数字。例如,如果运行java Fibonacci 5 (其中Fibonacci是类名),那么输出就应该是1、1. 2、3、5。
package com.laputa.chapter4.practice9;public class E09_Fibonacci {static int fib(int n) {if (n <= 2) {return 1;}return fib(n-1) + fib(n-2);}public static void main(String[] args) {/* int n = Integer.parseInt(args[0]);if(n < 0) {System.out.println("Cannot use negative numbers");return;}*/// 这里以5为例了,方便演示int n = 5;for(int i = 1; i <= n; i++) {System.out.print(fib(i) + ", ");}}
}
练习10: (5) 吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到, 而这对数字各包含乘积的一半位数的数字, 其中从最初的数字中选取的数字可以任意排序。以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼"数字:
1260=21 * 60
1827=21 * 87
2187 =27* 81
写一个程序,找出4位数的所有吸血鬼数字(Dan Forhan推荐)。
package com.laputa.chapter4.practice10;public class E10_Vampire {public static void main(String[] args) {int[] startDigit = new int[4];int[] productDigit = new int[4];for(int num1 = 10; num1 <= 99; num1++) {for(int num2 = num1; num2 <= 99; num2++) {/*if((num1 * num2) % 9 != (num1 + num2) % 9) {continue;}*/int product = num1 * num2;startDigit[0] = num1 / 10;startDigit[1] = num1 % 10;startDigit[2] = num2 / 10;startDigit[3] = num2 % 10;productDigit[0] = product / 1000;productDigit[1] = (product % 1000) / 100;productDigit[2] = product % 1000 % 100 / 10;productDigit[3] = product % 1000 % 100 % 10;int count = 0;for(int x = 0; x < 4; x++) {for(int y = 0; y < 4; y++) {if(productDigit[x] == startDigit[y]) {count++;productDigit[x] = -1;startDigit[y] = -2;if(count == 4) {System.out.println(num1 + " * " + num2+ " : " + product);}}}}}}}
}

【Java编程思想第四版】第4章练习题相关推荐

  1. java编程思想第四版第三章要点习题

    使用"简短的" 和正常的 打印语句来编写一个程序 package net.mindview.util;public class Print {/*** 不带有回车* @param ...

  2. Java编程思想第四版第十一章习题(下)

    这是第十一章最后一节,之后我会做前11章节的回顾(按着目录捋) 题目27-32,其中30-32因为我没有源代码,所以我贴的官网答案. 编译器:IDEA 27.写一个称为Command的类,它包含一个S ...

  3. Java编程思想第四版第六章习题

    断更了一天,今天开始第六章,隔热感觉有点像C++的Iclude. 闲话少说,直接上题,编译器IDEA. 1.在某个包中创建一个类,在这个类所处的包的外部创建该类的一个实例. class A: /*** ...

  4. Java编程思想第四版——第十五天

    2012-04-23 121-131/913 Java编程思想第四版--第十五天 5.5.3 终结条件 通常不能指望finalize(),必须创建其他的"清理"方法,并明确的调用它 ...

  5. Java编程思想第四版学习总结

    Java编程思想第四版学习总结 文章目录 Java编程思想第四版学习总结 第 1 章 对象入门 1.1 抽象的进步 1.2 对象的接口 1.3 实现方案的隐藏 1.4 方案的重复使用 1.5 继承:重 ...

  6. Java编程思想 第四版 读书笔记巩固基础,完善知识框架。

    Java编程思想读书笔记 此笔记仅仅是作者的阅读此书时,发现自己错误认识和不牢固的知识,用来完善作者的知识框架,其列出重点不具有参考性,代码也是为了省工作量,简写代码,能看懂即可,语法并不规范 第一章 ...

  7. Java编程思想第四版第九章练习

    这一章讲的是接口, 其中抽象和C++中的纯虚函数特别相似,我会在Java编程思想专栏做完以后,专门写一篇文章来对比C++和Java的不同. 1.修改第8章练习9中的Rodent,使其成为一个抽象类.只 ...

  8. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects...

    The genesis of the computer revolution was a machine. The genesis of out programming languages thus ...

  9. java编程思想第四版第十四章 类型信息习题

    fda dfa 第三题u package net.mindview.typeinfo.test4;import java.util.ArrayList; import java.util.Arrays ...

  10. Thinking in Java 4th(Java编程思想第四版)文档、源码、习题答案

    Thinking in Java 4th 中.英文两版pdf文档,书中源码及课后习题答案.链接:https://pan.baidu.com/s/1BKJdtgJ3s-_rN1OB4rpLTQ 密码:2 ...

最新文章

  1. 面向对象方法综述(工具<方法<思维<价值观)
  2. 玩玩.net的ildasm與ilasm (转)
  3. TreeMap之floorKey
  4. 好看的html导航栏作品,精选10款超酷的HTML5/CSS3菜单
  5. [code] spectral cluster
  6. go语言中的闭包结构
  7. 设计模式常见面试真题详解
  8. opengl多重纹理映射
  9. JavaScript基础知识【内置对象】(六)
  10. TypeScript 热度超 C 与 Python、Go 开发收入高、运维吃香,调查了 65000 名开发者有这些发现!...
  11. 96% 移动恶意软件针对 Android 系统:逾50亿应用可被攻击
  12. 国内pinterest模式昙花一现 社交电商不该这么玩
  13. 过去的一年,2013!
  14. MVC+angularjs
  15. android 微信分享小程序 图片显示不全
  16. linux 单网卡 双网段,用单网卡连接两个网段
  17. matlab改进平方根算法,改进平方根请教
  18. python NLP英式英语和美式英语的转换
  19. 出入库单据小票移动打印,盘点机PDA连接蓝牙打印机打印单据小票
  20. python sphinx_Python Sphinx 生成简洁大方的文档

热门文章

  1. 【持续更新】威胁情报 | 情报分析(主要是APT)信息源
  2. 《Attentive Generative Adversarial Network for Raindrop Removal from A Single Image》论文阅读之AttentiveGAN
  3. 计算机学院新生入学讲座,安全意识,铭记我心 --计算机学院开展新生入学安全教育讲座...
  4. 聊聊软测人员知识的深与广,解读老鸟成长经历
  5. 神经机器翻译(NMT)中的不确定性(Uncertainty)应用思考
  6. rs232读取智能电表_揭秘智能电表上的红外有什么作用,一起学习一下吧!
  7. android studio build variants,Android studio构建App的不同variants和types
  8. Android Studio 报错:No variants found for ‘app‘
  9. 初学JAVA之简单模拟拳皇
  10. 区块链安全—详谈共识攻击(四)