Java语言程序设计 第五章笔记
5.1 引言
Java 提供了三种类型的循环语句: while循环、do-while循环和for循环。
5.2 while循环
计时器控制的循环:确切的知道循环体需要执行的次数
标记位控制的循环:使用标记值用以表明循环的结束
输入和输出重定向
java test < input.txt
java test > output.txt
java test <input.txt> output.txt
5.3 do-while循环
如果循环中的语句至少需要执行一次,建议使用 do-while 循环
5.4 for循环
for (初始操作; 循环鏈续条件; 每次迭代后的操作) {
//循环体;
语句(组);
}
5.5 采用哪种循环
三种循环方式是等价的,可以采用自己喜欢的方式决定使用的类别。通常,如果已经提前知道重复次数,那就采用for循环;如果无法确定重复次数,就采用 while 循环;如果在检验继续条件前需要执行循环体,就用do while循环
while (loop-continuation-condition) {
//loop body
}
等价于<==>
for ( ; loop-continuation-condition; ){
//loop body
}
for (initial-action;
loop-continuation-condition;
action-after-each-interation) {
//loop body
}
等价于<==>
initial-action;
while (loop-continuation-condition) {
//loop body
action-after-each-interation
}
5.6 嵌套循环
嵌套循环是由一个外层循环和一个或多个内层循环组成的。每当重复执行一次外层循环时将再次进人内部循环,然后重新开始。
5.7 最小化数值错误
在循环继续条件中使用浮点数将导致数值错误
5.8 示例学习
5.9 关键字break和continue
Java中的break语句和continue语句只能运行在循环中或者 switch语句中。break语句跳出整个循环,而continue语句跳出循环的当前迭代
5.10 示例学习
5.11 示例学习
Java语言程序设计 第五章习题
5.2习题
5.1 分析下面的代码。在 Point A 处、Point B 处和 Point C 处,count<0总是 true 还是总是 false, 或者有时是 true 有时是 false?
int count = 0;
while (count < 100) {
//Point A
System.out.println("Welcome to Java!");
count++;
//Point B
}
//Point C
总是false,因为count从0计数,每次循环+1,不可能<0
5.2 在程序清单5-3中的第11行中,如果guess初始化为0, 会出现什么错误?
随机数number的值可能也为0,此时循环不会执行
5.3 下面的循环体会重复多少次?这个循环的输出是什么?
int i = 1;
while (i < 10)
if (i % 2 == 0)
System.out.println(i);
循环无数次:变量i不会变化,永远都是1
没有输出结果:1%2永远不会等于0
int i = 1;
while (i < 10)
if(i % 2 == 0)
System.out.println(i++);
循环无数次:i永远不会进入if循环自增,变量i不会变化,永远都是1
没有输出结果:i永远不会进入if循环输出,1%2永远不会等于0
int i = 1;
while (i < 10)
if((i++) % 2 == 0)
System.out.println(i);
要注意在表达式中,i++指的是,先用原来的i值执行,再i+1
循环9次
输出结果是
3
5
7
9
5.4 假设输入是2 3 4 5 0, 那么下面代码的输出结果是什么?
Scanner input = new Scanner(System.in);
int number, max;
number = input.nextInt();
max = number;
while (number != 0) {
number = input.nextInt();
if (number > max)
max = number;
}
System.out.println("max is " + max)
System.out.println("number " + number);
2
3
4
5
0
max is 5
number 0
5.5 下面代码的输出结果是什么? 解释原因。
int x = 80000000;
while (x > 0)
x++;
System.out.println("x is " + x);
x is -2147483648
当x自增到最高位为1时,这个数字在二进制下的表示就会变成负数
5.3习题
5.6 假设输人是2 3 4 5 0, 那么下面代码的输出结果是什么?
import java.util.Scanner;
public class Main{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int number,max;
number = input.nextInt();
max = number;
do {
number = input.nextInt();
if (number > max)
max = number;
} while (number != 0);
System.out.println("max is " + max);
System.out.println("number " + number);
}
}
2 4 6 8 0
max is 8
number 0
5.7 while循环和do-while循环之间的区别是什么? 将下面的 while 循环转换成do-while循环。
do-while循环至少会执行一次,while循环可能不会执行
Scanner input = new Scanner(System.in);
int sum = 0;
System.out.println("Enter an integer " + "(the input ends if it is 0)");
int number = input.nextInt();
while (number != 0){
sum += number;
System.out.println("Enter an integer " + "(the input ends if it is 0)");
number = input.nextInt();
}
import java.util.Scanner;
public class Main{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int sum = 0;
int number = 0;
do {
System.out.println("Enter an integer " + "(the input ends if it is 0)");
number = input.nextInt();
sum += number;
} while (number != 0);
System.out.print("sum is " + sum);
}
}
Enter an integer (the input ends if it is 0)
1
Enter an integer (the input ends if it is 0)
2
Enter an integer (the input ends if it is 0)
3
Enter an integer (the input ends if it is 0)
0
sum is 6
5.4习题
5.8 完成下列两个循环之后,sum是否具有相同的值?
for (int i = 0; i < 10; ++i) {
sum += i;
}
for (int i = 0; i < 10; i++) {
sum += i;
}
一样,只有在表达式中才会有区别
5.9 for循环控制的三个部分是什么? 编写一个for循环,输出从1到100的整数。
初始操作,循环继续条件,每次迭代后的操作
public class Main{
public static void main(String[]args){
for (int i = 1; i < 101; ++i) {
System.out.print(i + "\t");
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
5.10 假设输入是2 3 4 5 0, 那么下面代码的输出结果是什么?
import java.util.Scanner;
public class Main{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int number, sum = 0, count;
//count会在最后一次循环+1,发现不满足条件,就退出了循环
for (count = 0; count < 5; count++){
number = input.nextInt();
sum += number;
}
System.out.println("sum is " + sum);
System.out.println("count is " + count);
}
}
2 4 6 8 0
sum is 20
count is 5
5.11 下面的语句做什么?
for ( ; ; ) {
// Do something
}
无限循环
5.12 如果在for循环控制中声明一个变量,在退出循环后还可以使用它吗?
不可以
5.13 将下面的for循环语句转换为while循环和do-while循环:
long sum = 0;
for (int i = 0; i <= 1000; i++)
sum = sum + i;
public class Main{
public static void main(String[]args){
int i = 0;
int sum = 0;
while(i <= 1000) {
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
public class Main{
public static void main(String[]args){
int i = 0;
int sum = 0;
do {
sum = sum + i;
i++;
}while(i <= 1000);
System.out.println(sum);
}
}
5.14 计算下面循环体的重复次数。
//n次
int count = 0;
while (count < n){
count++;
}
//n+1次
for (int count = 0; count <= n; count++) {
}
//n-5次
int count = 5;
while (count < n){
count++;
}
//n=10, 循环2次
int count = 5;
while (count < n){
count = count + 3;
}
5.5习题
5.15 可以将for循环转换为while循环吗? 列出使用for循环的好处?
所有循环理论上都可以互相转换,知道循环次数可以非常方便的使用for循环
5.16 while循环总是可以转换成for循环么? 将下面的while循环转换为for循环。
理论上可以
int i = 1;
int sum = 0;
while (sum < 10000){
sum = sum + 1;
i++;
}
for (int i = 1, sum = 0; sum < 10000; i++)
sum = sum + 1;
5.17 找到下面代码中的错误并且进行修正。
public class Main{
public void main(String[]args){
//不可以有;
for (int i = 0; i < 10; i++);
sum += i;
//不可以有;
if (i < j);
//需要有;
System.out.println(i)
else
System.out.println(j);
//不可以有;
while (j < 10);
{
j++;
}
do {
//需要有;
j++
//需要有;
} while (j < 10)
}
}
5.18 下面的程序有什么错误?
public class Main{
public static void main(String[]args){
int i = 0;
do {
System.out.println(i + 4);
i++;
}
//需要有;
while (i < 10)
}
}
public class Main {
public static void main(String[]args){
//不可以有;
for (int i = 0; i < 10; i++);
System.out.println(i+4);
}
}
5.6习题
5.19 println语句执行了多少次?
for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
System.out.println(i * j);
45次
5.20 给出下面程序的输出结果。(提示: 绘制一个表格,在列中列出变量,对这些程序进行跟踪。)
public class Main{
public static void main(String[]args){
for (int i = 1; i < 5; i++){
//每次退出内圈循环后,外圈循环都会赋值j=0
int j = 0;
while (j < i){
System.out.print(j + " ");
j++;
}
}
}
}
0 0 1 0 1 2 0 1 2 3
public class Main {
public static void main(String[]args) {
int i = 0;
while (i < 5){
for (int j = i; j > 1; j--)
System.out.print(j + " ");
System.out.println("****");
i++;
}
}
}
****
****
2 ****
3 2 ****
4 3 2 ****
public class Main{
public static void main(String[]args){
int i = 5;
while (i >= 1){
int num = 1;
for (int j = 1; j <= i; j++){
System.out.print(num + "xxx");
num *= 2;
}
System.out.println();
i--;
}
}
}
1xxx2xxx4xxx8xxx16xxx
1xxx2xxx4xxx8xxx
1xxx2xxx4xxx
1xxx2xxx
1xxx
public class Main{
public static void main(String[]args){
int i = 1;
do {
int num = 1;
for (int j = 1; j <= i; j++){
System.out.print(num + "G");
num += 2;
}
System.out.println();
i++;
}while(i<=5);
}
}
1G
1G3G
1G3G5G
1G3G5G7G
1G3G5G7G9G
5.8习题
5.21 如果将程序淸单5-9中第17行的n1和n2用n1/2和n2/2来替换,程序还会工作吗?
import java.util.Scanner;
public class Main{
public static void main(String[]args){
//Create a Scanner
Scanner input = new Scanner(System.in);
//Prompt the user to enter two integers
System.out.print("Enter first integer: ");
int n1 = input.nextInt();
System.out.print("Enter second integer:");
int n2 = input.nextInt();
int gcd = 1; //Initial gcd is 1
int k = 2; //Possible gcd
while(k <= n1/2 && k <= n2/2){
if (n1 % k == 0 && n2 % k == 0)
gcd = k; //Update gcd
k++;
}
System.out.println("The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd);
}
}
某些状态下不会正常工作,假设n1=2,n2=4,n1/2=1,这样就限制了最大公约数只能是1
5.22 程序淸单5-11中,如果你将第21行的代码(char)(hexValue + ‘0’)改为 hexValue + ‘0’, 为什么会出错?
必须要转换为char,需要的是char数据,而hexValue + '0’得到的是int
5.23 程序淸单5-11中,对于十进制数245而言, 循环体将执行多少次? 对于十进制数3245而言, 循环体将执行多少次?
2次和3次
5.9习题
5.24 关键字break的作用是什么?关键字continue的作用是什么?下列程序能够结束吗?如果能,给出结果。
break直接结束当前整个循环,即使在循环内,break之后的语句也不会执行
continue只是结束当前的一次迭代,还会重新进入下一次循环,即使在循环内,continue之后的语句也不会执行
int balance = 10;
while (true){
if (balance > 9)
break;
balance = balance - 9;
}
System.out.println("Balance is " + balance);
Balance is 10
int balance = 10;
while (true){
if (balance > 9)
continue;
balance = balance - 9;
}
System.out.println("Balance is " + balance);
最后一行代码会有编译错误,Unreachable code,因为是无限循环
5.25 将下面左边的for循环转换成右边的while循环,其中有什么错误? 改正该错误。
public class Main{
public static void main(String[]args){
int sum = 0;
for (int i = 0; i < 4; i++){
if (i % 3 == 0)
continue;
sum += i;
}
System.out.print(sum);
}
}
import java.util.Scanner;
public class Main{
public static void main(String[]args){
int i= 0, sum = 0;
while (i < 4) {
if (i % 3 == 0)
continue;
sum += i;
i++;
System.out.print(sum);
}
}
}
改为while循环,循环会变成无限循环,i永远等于3不会退出循环,原因在于i++的位置不当
5.26 不使用关键字break和continue,改写程序清单5-12和程序清单5-13的程序TestBreak和TestContinue
public class Main{
public static void main(String[]args){
int sum = 0;
int number = 0;
while (sum <= 100){
number++;
sum += number;
}
System.out.println("The number is " + number);
System.out.println("The sum is " + sum);
}
}
The number is 14
The sum is 105
public class Main{
public static void main(String[]args){
int sum = 0;
int number;
for (number = 0; number <= 20; number++) {
if (number == 10 || number == 11)
sum = sum + 0;
else
sum += number;
}
System.out.println("The sum is " + sum);
}
}
The sum is 189
5.27 a中break语句之后,执行哪条语句? 给出输出。b中continue语句之后,执行哪条语句?给出输出。
public class Main{
public static void main(String[]args){
for (int i = 1; i < 4; i++) {
//每次退出内圈循环后,重新进入外圈循环,然后再次进入内圈循环,都会赋值j=1
for (int j = 1; j < 4; j++) {
if (i * j > 2)
break;
System.out.println("i * j is " + i * j);
}
System.out.println("i is " + i);
}
}
}
i * j is 1
i * j is 2
i is 1
i * j is 2
i is 2
i is 3
public class Main{
public static void main(String[]args){
for (int i = 1; i < 4; i++){
//每次退出内圈循环后,重新进入外圈循环,然后再次进入内圈循环,都会赋值j=1
for (int j = 1; j < 4; j++){
if (i * j > 2)
continue;
System.out.println("i * j is " + i * j);
}
System.out.println("i is " + i);
}
}
}
i * j is 1
i * j is 2
i is 1
i * j is 2
i is 2
i is 3