热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

执行流程_Thinginjava第四章,控制执行流程,练习题答案

篇首语:本文由编程笔记#小编为大家整理,主要介绍了Thinginjava第四章,控制执行流程,练习题答案相关的知识,希望对你有一定的参考价值。/***

篇首语:本文由编程笔记#小编为大家整理,主要介绍了Thing in java 第四章,控制执行流程,练习题答案相关的知识,希望对你有一定的参考价值。


/**
* 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 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 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 P.print("end cannot be smaller than begin");
else if((testval <=end) &(testval>=begin))
return +1;
else if((testvalend))
return -1;
else
P.print("exceptional case");
return 13;
}
public static void main(String[] args){
test(10, 5,4);//begin P.print("case2: "+test(5,4,10));//begin P.print("case3: "+test(1,4,10));//testval 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 a[k] = 1;
} else {
a[0] = 1;
a[1] = 1;
for (int j = 2; j a[j] = a[j - 2] + a[j - 1];
}
for (int m = 0; 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)));
}
}
}


推荐阅读
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
author-avatar
光明使者之快乐天使_101
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有