/**
* 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. springboot快速开发(简单web)

    这是一个springboot基础配置文件介绍的demo.只涉及到 控制层与前端的简单交互,用于验证代码的畅通. spring-boot  pom.xml解释 <?xml version=&quo ...

  2. css flex布局,小程序flex布局,垂直居中完美解决

    flex弹性布局,很好的解决了垂直居中的问题,上代码: wxml: <view class='container'> <view class='item item1'>item ...

  3. vim 匹配查找指定位置的数字,并将数字做运算后赋值

    举例,以下文本中有个DSC开头的以数字命名的jpg文件,我想修改文件名为在原来的基础上加上32,比如第一行中的字符改为:DSC00099.JPG 在vim中输入: :%s/DSC[]\+\(\d\+\ ...

  4. 利用Django实现webUI展示

    1.说明 最近老大想要做一个webUI界面,为了展示我们数据中心工作内容,需要把各自的工作内容用webUI展示出来.目前我负责的做公司名称归一化的问题. 2.Django实现web 具体实现是完全按照 ...

  5. Debian 系linux切换登录管理器(display manager)

    在控制台中sudo dpkg-reconfigure <你的dm包名>即可dm选择列表,选择自己需要的dm 例如ubutu18默认使用gdm3,则输入命令: sudo dpkg-recon ...

  6. pycharm 输入法光标跟随

  7. 用Spring Boot去创建web service

    1. 环境 JDK1.8 JavaSE1.8 web容器是 webSphere IDE是Eclipse 2. 创建一个空的 Maven Project 3. 打开pom.xml 配置相应的packag ...

  8. matlab程序设计

    一.M文件 1.脚本文件 (1)英文字母开头命名 (2)所产生的所有变量驻留在base workspace,只要不用clear,就只有关闭MATLAB,才会被删除 2.函数文件 (1)function ...

  9. python基础语法三

    集合: 1.不同元素组成 2.无序 3.集合中的元素必须是不可变类型  s = {1, 2, 3 } #定义集合 s = set('hello') print(s) s.pop() #指定删除 s.r ...

  10. HTTP Status 500 - Error instantiating servlet class cn.it.bd.S011

    HTTP Status 500 - Error instantiating servlet class cn.it.bd.S011 出现此报错的很大可能是因为 <servlet-class> ...