1 think before coding code incrementally 2 sentinel value sentinel-controlled loop 3 输入输出重定向 > < input redirection output redirection 4 pretest loop posttest loop 5 从小到大添加浮点数比从大到小精确 6 Integer.toBinaryString(int) Integer.toHexString(int) 7 PIE =4* (1 - 1 / 3 + 1 / 5 - 1 / 7 ... - 1 / (2 * i - 1) + 1 / (2 * i + 1)) Keyword break statement continue statement do-while loop for loop loop control statement infinite loop input redirection iteration labeled continue statement loop loop-condition condition :循环控制条件 loop body nested body off-by-one error output redirection sentinel value while loop 4.1 always true 4.2 无限循环 4.3 无限次;9次;9次 11111111111111111 2,4,6,8 3,5,7,9 4.4 循环控制条件和循环体的执行顺序 int sum = 0; int number = 0; do{ sum += number; number = input.nextInt(); }while(number != 0) 4.5 相同 4.6 循环初始化,循环控制条件,每次迭代后操作 for(int i = 0;i < 100;i++)System.out.println(i+1); 4.7 max is 5 number is 0 4.8 sum is 14 number is 0 4.9 max is 5 number is 0 4.10 无限循环 4.11 不能 4.12 可以,但是for循环更直接,明晰 4.13 long sum =0; int i = 0; while(i <= 1000){ sum += i++; } /* do{ sum+=i++; }while(i<=1000) */ 4.14 cant because of integer division 4.15 跳出当前循环;跳出当前迭代;能,1;不能 4.16 int sum = 0; int i = 1; for(;sum < 10000;i++)sum += i; 4.17 if(i % 3 == 0){i++;continue;} 4.18 ......... 4.19 System.out.println(i);1; 4.20 System.out.println(i);1; 4.21 2 public static void 3:int i 在循环体外声明 5:声明int j 并初始化 7:分号 11:去掉分号 4.22 未初始化;多加了分号; 4.23 0 0 1 0 1 2 3 0 1 2 3 4; compile Error; 1xxx2xxx4xxx8xxx16xxx 1xxx2xxx4xxx8xxx 1xxx2xxx4xxx 1xxx2xxx 1xxx ; 1G 1G2G 1G2G4G 1G2G4G8G 1G2G4G8G16G ; 4.24 no output result;because the loop never end 4.25 n n-5 n+1 (n-3)/3 编程题 4.23 //从左向右 double sum = 0; for(int i =1;i<=50000;i++){ sum += 1d/i; } System.out.println(sum); //11.397003949278504 //从右向左 double sum = 0; for(int i = 50000;i >= 1;i--){ sum += 1d/i; } System.out.println(sum); //11.397003949278519
时间: 2024-11-06 07:32:05