首页 > 代码库 > java程序设计基础篇 复习笔记 第四单元

java程序设计基础篇 复习笔记 第四单元

1 think before codingcode incrementally2sentinel valuesentinel-controlled loop3 输入输出重定向 > <input redirectionoutput redirection4pretest loopposttest loop5从小到大添加浮点数比从大到小精确6Integer.toBinaryString(int)Integer.toHexString(int)7PIE =4* (1 - 1 / 3 + 1 / 5 - 1 / 7 ... - 1 / (2 * i - 1) + 1 / (2 * i + 1))Keywordbreak statementcontinue statementdo-while loopfor looploop control statementinfinite loopinput redirectioniterationlabeled continue statementlooploop-condition condition :循环控制条件loop bodynested bodyoff-by-one erroroutput redirectionsentinel valuewhile loop4.1 always true4.2无限循环4.3无限次;9次;9次111111111111111112,4,6,83,5,7,94.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.7max is 5number is 04.8sum is 14number is 04.9max is 5number is 04.10无限循环4.11不能4.12可以,但是for循环更直接,明晰4.13long sum =0;int i = 0;while(i <= 1000){	sum += i++;}/*do{	sum+=i++;}while(i<=1000)*/4.14cant because of integer division4.15跳出当前循环;跳出当前迭代;能,1;不能4.16int sum  = 0;int i = 1;for(;sum < 10000;i++)sum += i;4.17if(i % 3 == 0){i++;continue;}4.18.........4.19System.out.println(i);1;4.20System.out.println(i);1;4.212 public static void3:int i 在循环体外声明5:声明int j 并初始化7:分号11:去掉分号4.22未初始化;多加了分号;4.230 0 1 0 1 2 3 0 1 2 3 4;compile Error;1xxx2xxx4xxx8xxx16xxx1xxx2xxx4xxx8xxx1xxx2xxx4xxx1xxx2xxx1xxx;1G1G2G1G2G4G1G2G4G8G1G2G4G8G16G;4.24no output result;because the loop never end4.25nn-5n+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

  

java程序设计基础篇 复习笔记 第四单元