/**
* 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. ykit入门

    init  lint pack server  watch 1.创建现有工程的ykit项目 在项目文件夹下 ykit init 2.lint 检查当前项目的代码质量 手动执行代码 可验证代码正误 yk ...

  2. Adversarial Examples for Semantic Segmentation and Object Detection 阅读笔记

    Adversarial Examples for Semantic Segmentation and Object Detection (语义分割和目标检测中的对抗样本) 作者:Cihang Xie, ...

  3. 在线jquery.min.js、vue.min.js引用

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js ...

  4. OpenGL坐标系的理解

    搬运自: https://learnopengl-cn.github.io/01%20Getting%20started/08%20Coordinate%20Systems/#3d 为了将坐标从一个坐 ...

  5. maven下载源码

    能下载到源代码的原则是仓库中打了resource的jar包 1.使用命令 mvn dependency:sources 下载依赖包的源代码. mvn dependency:sources -Ddown ...

  6. jQuery中有关each方法的使用

    概述: each() 方法规定为每个匹配元素规定运行的函数. 返回 false 可用于及早停止循环,相当于break. 返回 true 可以结束本次循环,相当于continue. 语法: $(sele ...

  7. [模板] 树状数组 (C++ class)

    闲来无事(其实是打了两三道树状数组题),写了个树状数组模板…… /* Author: hotwords */ template<typename tp> class BinTree { p ...

  8. hashlib模块(加密模块)

    hash = hashlib.md5(b"str") #md5对象,md5不能反解,可加参数 hash.update(b"str") #对字符串进行加密 has ...

  9. React Native - 网页组件(WebView)的使用详解

    一.WebView组件介绍 使用 WebView 组件我们可以通过 url 来加载显示一个网页,也可以传入一段 html 代码来显示.下面对其主要属性和方法进行介绍.   1,属性介绍 source: ...

  10. AutomaticReferenceCounting.html#runtime-support

    https://clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime-support Runtime support This sec ...