/**
* 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. 初读&quot;Thinking in Java&quot;读书笔记之第四章 ---控制执行流程

    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-(三) 操作符&amp;控制执行流程

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

  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. CSS 中关于background 属性功能

    background 是 css中的核心属性,我们对他应该充分了解. background-image   定义背景图像  这个属性是我们用的最多的属性 设置背景图像有两个方式 background: ...

  2. Socket原理与编程基础(转)

    一.Socket简介 Socket是进程通讯的一种方式,即调用这个网络库的一些API函数实现分布在不同主机的相关进程之间的数据交换. 几个定义: (1)IP地址:即依照TCP/IP协议分配给本地主机的 ...

  3. 【POJ 1094】拓扑排序

    题意 给出n,代表有以A开始的n个字母,给出它们的m个小于关系(A<B).如果前i个关系可以确定n个字母的一个顺序就输出: Sorted sequence determined after i ...

  4. Ajax的ActionLink方法(适用于异步加载)

    8.2.1  AJAX的ActionLink方法 在Razor视图中,AJAX辅助方法可以通过Ajax属性访问.和HTML辅助方法类似,Ajax属性上的大部分AJAX辅助方法都是扩展方法(除了Ajax ...

  5. RHEL7查看网卡配置命令ip addr show

    Validating Network Address Configuration To verify the configuration of the network address, you nee ...

  6. 【Python3爬虫】自动查询天气并实现语音播报

    一.写在前面 之前写过一篇用Python发送天气预报邮件的博客,但是因为要手动输入城市名称,还要打开邮箱才能知道天气情况,这也太麻烦了.于是乎,有了这一篇博客,这次我要做的就是用Python获取本机I ...

  7. mysql 开发进阶篇系列 5 SQL 优化(表优化)

    一. 使用sql提示 sql 提示(sql hint)是优化数据库的一个重要手段, 是在sql语句中加入一些人为的提示来达到优化操作的目的. 1.1 use index 在查询语句中表名的后面,添加u ...

  8. 安装配置OSA运维管理平台

    1.下载完整包V1.0.2wget http://www.osapub.com/download/OSA_BETA_V1.0.2.tar.gzV1.0.5wget http://www.osapub. ...

  9. Tomcat源码学习(1)

    Tomcat源码学习(1) IntelliJ IDEA 17.3.3 导入 Tomcat 9.0.6源码 下载源码 tomcat_9.0.6 启动 IDEA. 点击 Open,选择刚才下载的文件解压后 ...

  10. time 模块学习

    前情提要: time模块是经常使用的模块.主要是用来记录时间,以及时间上的相关操作 一:时间戳 1:第一种形式 import time print(time.time()) 从1970 1 1 0:0 ...