/**
* Created by Sandy.Liu on 2018/7/19.
* Thinking in java, version 4, chapter 4, practice 1
* Write a program to print from 1 to 100
*/
public class Chap4Prac1Printfrom1To100 {
public static void main(String[] args){
for(int i=1;i<=100;i++)
P.print(i+" ");
}
}
/**
* Created by Sandy.Liu on 2018/7/19.
* Thinking in java, chapter 4, practice 2
* Write a program which generating 25 random int numbers. Use if-else statement to classify it as
* greater than, less than or equal to a second randomly generated value.
*/
import java.util.*;
public class Chap4Prac2Generate25int {
public static void main(String[] args){
Random ran1 = new Random();
Random ran2 = new Random();
P.print("ran1: "+ran1);
P.print("ran2: "+ran2);
for(int i=0;i<25;i++){
int x = ran1.nextInt();
int y = ran2.nextInt();
if(x>y)
P.print(x+">"+y);
else if(x<y)
P.print(x+"<"+y);
else
P.print(x+"="+y);
} } }
import java.util.Random;

/**
* Created by Sandy.Liu on 2018/7/19.
* Thinking in java, version 4, chapter 4, practice 3
*/
import java.util.*;
public class Chap4Prac3 {
public static void main(String[] args){
Random ran3 = new Random();
Random ran4 = new Random();
while(true){
int x = ran3.nextInt(10);
int y = ran4.nextInt(10);
if(x>y)
P.print(x+">"+y);
else if(x<y)
P.print(x+"<"+y);
else
P.print(x+"="+y); }}}
/**
* Created by Sandy.Liu on 2018/7/19.
* Thinking in java, version 4, chapter 4, practice 4
* Write a program using two nested for loops and the modules operator(%) to detect and print
* prime numbers
*/
public class Chap4Prac4For {
public static void main(String[] args){
for(int i = 1;i<100;i++){
int factor = 0;
for(int j=1;j<=i;j++){
if(i%j==0)
factor++;
}
if(factor<=2)
P.print(i);
}
}
}
/**
* Created by Sandy.Liu on 2018/7/19.
* Thinking in java, version 4, chapter 4, practice 5
*/
public class Chap4Prac5 {
static void binaryPrint(int q){
if(q==0) P.print(0);
else{
int nlz = Integer.numberOfLeadingZeros(q);
q<<=nlz;
for(int p=0;p<32-nlz;p++){
int n = (Integer.numberOfLeadingZeros(q)==0)?1:0;
System.out.println(n);
q<<=1;
}
}
P.print(""); }
public static void main(String[] args){
int i=1+4+16+64;
int j = 2+8+32+128;
int k = 0x100;
int m = 0;
P.print("Using Integer.toBinaryString(): ");
P.print("i = "+Integer.toBinaryString(i));
P.print("j = "+Integer.toBinaryString(j));
P.print("k = "+Integer.toBinaryString(k));
P.print("m= "+Integer.toBinaryString(m));
P.print("i & j = "+(i&j)+"="+Integer.toBinaryString(i&j));
P.print("i | j = "+(i|j)+"="+Integer.toBinaryString(i|j));
P.print("i ^ j = "+(i^j)+"="+Integer.toBinaryString(i^j));
P.print("~i = "+Integer.toBinaryString(~i));
P.print("~j = "+Integer.toBinaryString(~j));
P.print("Using binaryPrint():");
P.print("i = "+i+" = ");
System.out.print(i);
P.print("j = "+j+" = ");
System.out.print(j);
P.print("k = " + k + " = ");
System.out.print(k);
P.print("m = " + m + " = ");
System.out.print(m);
P.print("i & j = " + (i & j) + " = ");
System.out.print(i & j);
P.print("i | j = " + (i | j) + " = ");
System.out.print(i | j);
P.print("i ^ j = " + (i ^ j) + " = ");
System.out.print(i ^ j);
P.print("~i = " + ~i + " = ");
System.out.print(~i);
P.print("~j = " + ~j + " = ");
System.out.print(~j);
}
}
/**
* Created by Sandy.Liu on 2018/7/22.
* Thinking in java, version 4, chapter 4, practice 6
* Modify method test(), make them accept two other parameters begin and end, check if
* testval is between begin and end.
*/
public class Chap4Prac6 {
static int test(int testval, int begin, int end){
if (end<begin)
P.print("end cannot be smaller than begin");
else if((testval <=end) &(testval>=begin))
return +1;
else if((testval<begin)||(testval>end))
return -1;
else
P.print("exceptional case");
return 13;
}
public static void main(String[] args){
test(10, 5,4);//begin<end
P.print("case2: "+test(5,4,10));//begin<testval<end
P.print("case3: "+test(1,4,10));//testval<begin
P.print( "case4: "+test(5,5,6));//testval=begin
P.print("case5: "+test(5,1,5));//testval=end
P.print("case6: "+test(10,1,5));//testval>end
}
}
/**
* Created by Sandy.Liu on 2018/7/22.
* Thinking in java, version 4, chapter 4, practice 7
* Modify excise 1, use break to make program exists at value 99
*/
public class Chap4Prac7{
static void test(int x){
for(int i=0;i<=x;i++){
if(i==99)
break;
P.print(i);
}
}
static void test1(int x){
for(int i=0;i<=x;i++){
if(i==99)
return;
P.print(i);
}
}
public static void main(String[] args) {
test(100);
test1(1000);
}
}
/**
* Created by Sandy.Liu on 2018/7/23.
* Thinking in java, version 4, chapter 4, practice 8
* Create a switch statement that prints a message for each case, and put the switch inside a for loop
* to try each case. Put a break after each case and test it. And then remove the breaks and test again.
*/
public class Chap4Prac8Switch {
public static void main(String[] args){
for(int i = 0;i<100;i++){
switch (i){
case 0: P.print("zero");break;
case 1: P.print("one");break;
case 2: P.print("two");break;
case 3: P.print("three");break;
default: P.print(i+"more than three");
}
}
}
}
import java.lang.reflect.Array;
import java.util.ArrayList; /**
* Created by Sandy.Liu on 2018/7/23.
* Thinking in java, version 4, chapter 4, practice 9
* A Fibonacci sequence is the sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on,
* each number (from the third one ) is the sum of the previous two numbers.
* Create a method that takes a integer as parameter,and display all Fibonacci numbers starting
* from the beginning to the gave parameter. e.g., if you run javaFibonacci 5 (where Fibonacci
* is the name of the class) the output will be 1, 1, 2, 3, 5.
*/
public class Chap4Prac9Fibonacci {
static void gFib(int i) {
if (i <= 0)
P.print("please input a number bigger than 0");
int[] a = new int[i];
if (i <= 2) {
for (int k = 0; k < i; k++)
a[k] = 1;
} else {
a[0] = 1;
a[1] = 1;
for (int j = 2; j < i; j++)
a[j] = a[j - 2] + a[j - 1];
}
for (int m = 0; m < i; m++){
System.out.print(a[m]);
}
P.print(" ");
} public static void main(String[] args){
gFib(0);
gFib(1);
gFib(2);
gFib(5);
} }
/**
* Created by Sandy.Liu on 2018/7/25.
* Thinking in java, version 4, chapter 4, practice 10
* A vampire number has an even number of digits and is formed by multiplying a pair of numbers
* containing half the number of digits of the result.
* The digits are taken from the original number in any order. Pairs of railing zeros are not
* allowed. Examples include:1260 = 21 * 60, 1827 = 21 * 87,
* 2187 = 27 * 81.
* Write a program that finds all the 4-digit vampire numbers.
*/
public class Chap4Prac10VaimpireNumber {
static int a(int i) {
return i / 1000;
} static int b(int i) {
return (i % 1000) / 100;
} static int c(int i) {
return ((i % 1000) % 100) / 10;
} static int d(int i) {
return ((i % 1000) % 100) % 10;
}
static int com(int i, int j){
return i*10+j;
}
static void productTest(int i, int m, int n){
if(i==m*n){
P.print(i+"="+m+"*"+n);
} } public static void main(String[] args) {
for(int i = 1001;i<9999;i++){
productTest(i,com(a(i),b(i)),com(c(i),d(i)));
productTest(i,com(a(i),b(i)),com(d(i),c(i)));
productTest(i,com(a(i),c(i)),com(b(i),d(i)));
productTest(i,com(a(i),c(i)),com(d(i),b(i)));
productTest(i,com(a(i),d(i)),com(b(i),c(i)));
productTest(i,com(a(i),d(i)),com(c(i),b(i)));
productTest(i,com(b(i),a(i)),com(c(i),d(i)));
productTest(i,com(b(i),a(i)),com(d(i),c(i)));
productTest(i,com(b(i),c(i)),com(d(i),a(i)));
productTest(i,com(b(i),d(i)),com(c(i),a(i)));
productTest(i,com(c(i),a(i)),com(d(i),b(i)));
productTest(i,com(c(i),b(i)),com(d(i),a(i)));
}
}
}

Thing in java 第四章,控制执行流程,练习题答案的更多相关文章

  1. 初读"Thinking in Java"读书笔记之第四章 ---控制执行流程

    true和false Java不允许将数字作为布尔值使用. 所有条件表达式都将布尔值作为判断条件,决定执行路径. if-lese 迭代 while,do-while,for为三个迭代语句. ,逗号操作 ...

  2. 《Java编程思想》笔记 第四章 控制执行流程

    1.true和false if--else if--else, while, do--while 都使用条件表达式的真假来决定执行路径. Java不允许数字作为真假判断,C和C++可以非0即真. 2. ...

  3. 《Java基础复习》-控制执行流程

    最近任务太多了,肝哭我了,boom 参考书目:Thinking in Java <Java基础复习>-控制执行流程 Java使用了C的所有流程控制语句 涉及关键字:if-else.whil ...

  4. [Java编程思想-学习笔记]第4章 控制执行流程

    4.1  return 关键字return有两方面的用途:一方面指定一个方法结束时返回一个值:一方面强行在return位置结束整个方法,如下所示: char test(int score) { if ...

  5. Java编程思想之四控制执行流程

    程序必须再执行过程中控制它的世界,并做出选择.在Java中,你要使用执行控制语句来做出选择. 4.1true和false 所有条件语句都利用条件表达式的真或假来决定执行路径. Java不允许使用数字作 ...

  6. “全栈2019”Java第四章:创建第一个Java程序

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. java控制执行流程

    控制执行流程 欢迎转载,转载烦请注明出处,谢谢. https://www.cnblogs.com/sx-wuyj/p/11177257.html java当中涉及到的关键字包括if-else.whil ...

  8. 大家一起和snailren学java-(三) 操作符&控制执行流程

    “又是新的一周,感觉要学的东西还有好多,加油.由于第三章和第四章内容要总结的不是很多,没太多需要拿出来说的,就整合到一个帖子好了” 操作符 操组符,什么是操作符?其实就是+-*/=&^~| 等 ...

  9. [uboot] (第四章)uboot流程——uboot编译流程

    http://blog.csdn.net/ooonebook/article/details/53000893 以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为 ...

  10. [uboot] (第四章)uboot流程——uboot编译流程 (转)

    以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为例 [uboot] uboot流程系列:[project X] tiny210(s5pv210)上电启动流程(B ...

随机推荐

  1. Oracle数据库各种名字的区别

    数据库名(DB_NAME).数据库实例名(INSTANCE_NAME).操作系统环境变量(ORACLE_SID).数据库服务名(SERVICE_NAME).数据库域名(DB_DOMAIN)以及全局数据 ...

  2. classPath与PATH

    PATH是window的变量,而不是Java的变量: 通常配置PATH路径是为了找到需要的XX.exe命令,而且配置在用户的变量下面: 例如:JDK中的javac与java命令在cmd中使用,需要把命 ...

  3. mybatis进行一对多时发现的问题总结

    1.定义一对多xml文件时,所有的resultMap中的column的值一定不要重复,否则mybatis会发生错误,如果有重名,定义别名,column中的名字一定要与查询出的名字一致,如: 52行的别 ...

  4. springboot整合多数据源及事物

    有两种方式:一种是分包的方式.一种是加注解的方式(@DataSource(ref="")). 分包方式:项目结构图如下: 分为com.itmayiedu.test01.com.it ...

  5. vim里添加自动补齐插件,与python 函数补齐

    参考  http://www.jb51.net/article/58009.htm 将 # cat ~/.vimrc filetype plugin on let g:pydiction_locati ...

  6. 免费的DDos网络测试工具集合

    今天晚上看YT上的hulk VS monster Dogs 然后想看电影资源,给我推送了hulk这款工具了解下,发现了一些东西,收藏下 1.卢瓦(LOIC) (Low Orbit Ion Canon) ...

  7. Android BLE dfu升级

    dfu升级适用于nordic  nRF51  nRF52 的系统,github上提供了相关升级的库https://github.com/NordicSemiconductor/Android-DFU- ...

  8. 2018-软工机试-A-西班牙馅饼

    A. 西班牙馅饼 单点时限: 1.0 sec 内存限制: 256 MB 港岛妹妹,你献给我的西班牙馅饼 甜蜜地融化了我,天空之城在哭泣 港岛妹妹,我们曾拥有的甜蜜的爱情 疯狂地撕裂了我,天空之城在哭泣 ...

  9. 最新版本汉化-PowerDesigner 16.6 汉化并河蟹

    更新日志: 2019-03-14 V1.1 1.支持反复多次汉化: 2.修复少许bug. 最新的16.6版本已经在汉化中了,基本上所有的菜单均已汉化完成,部分窗体还没有编译通过. 不过,不影响尝鲜使用 ...

  10. Mac下截屏方法

    Refer to:https://zh.wikihow.com/在Mac-OS-X上截取屏幕截图 先来说几个需要用到的Mac键盘和普通键盘不一样的名字: Mac键盘 普通键盘 control Ctrl ...