1 do-while

开发中常用for和while。

  1. package com.atguigu.work;
  2. import java.util.Scanner;
  3. /*
  4. 题目:
  5. 从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入
  6. 为0时结束程序。
  7. 最简单“无限” 循环格式:while(true) , for(;;),无限循环存在的原因是并不
  8. 知道循环多少次,需要根据循环体内部某些条件,来控制循环的结束。
  9. */
  10. public class Practice1 {
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13. int numZhengShu = 0;
  14. int numFuShu = 0;
  15. while (true) {
  16. for (int i = 0; i>=0; i++) {
  17. System.out.println("请输入一个整数:");
  18. int zhengShu = sc.nextInt();
  19. if (zhengShu>0){
  20. numZhengShu++;
  21. } else if (zhengShu<0){
  22. numFuShu++;
  23. } else {
  24. System.out.println("正数个数为:"+numZhengShu);
  25. System.out.println("负数个数为:"+numFuShu);
  26. break;
  27. }
  28. }
  29. break;
  30. }
  31. }
  32. }
  33. //我写的
  1. package com.atguigu.work;
  2. import java.util.Scanner;
  3. /*
  4. 题目:
  5. 从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。
  6. 最简单“无限” 循环格式:while(true) , for(;;),无限循环存在的原因是并不
  7. 知道循环多少次,需要根据循环体内部某些条件,来控制循环的结束。
  8. */
  9. public class Practice2 {
  10. public static void main(String[] args) {
  11. Scanner sc = new Scanner(System.in);
  12. int positiveNumber = 0;
  13. int negativeNumber = 0;
  14. while (true) {
  15. int number = sc.nextInt();
  16. if (number > 0) {
  17. positiveNumber++;
  18. } else if (negativeNumber < 0) {
  19. negativeNumber++;
  20. } else {
  21. break;
  22. }
  23. }
  24. System.out.println("输入的正数为:"+positiveNumber);
  25. System.out.println("输入的负数为:"+negativeNumber);
  26. }
  27. }
  28. //老师写的
  29. for(;;)类似于while(true)

注意:最简单“无限” 循环格式:while(true) , for(;;),无限循环存在的原因是并不 知道循环多少次,需要根据循环体内部某些条件,来控制循环的结束。
结束循环的方式:循环条件部分返回false;在循环体中,执行break

2 嵌套循环

将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for ,while ,do…while均可以作为外层循环或内层循环。 嵌套循环最多不超过3层。
实质上,嵌套循环就是把内层循环当成外层循环的循环体。当只有内层循环的循环条件为false时,才会完全跳出内层循环,才可结束外层的当次循环,开始下一次的循环。
设外层循环次数为m次,内层为n次,则内层循环体实际上需要执行m*n次
技巧:外层循环控制行数,内层循环控制列数。

  1. package com.atguigu.work;
  2. //九九乘法表
  3. public class Practice3 {
  4. public static void main(String[] args) {
  5. for (int i =1;i<=9;i++){
  6. for (int j=1;j<=i;j++){
  7. System.out.print(j+"*"+i+"="+i*j);
  8. System.out.print('\t');
  9. }
  10. System.out.println();
  11. }
  12. }
  13. }
  1. package com.atguigu.work;
  2. /*
  3. 100以内的所有质数
  4. */
  5. public class Practice4 {
  6. public static void main(String[] args) {
  7. System.out.println("100以内的所有质数:");
  8. for (int i = 2;i<=100;i++){
  9. int count=0;
  10. for (int j=i;j>=1;j--){
  11. if (i%j==0){
  12. count++;
  13. }
  14. }
  15. if (count==2){
  16. System.out.println(i);
  17. }
  18. }
  19. }
  20. }
  1. package com.atguigu.work;
  2. public class Practice5 {
  3. public static void main(String[] args) {
  4. long start = System.currentTimeMillis();
  5. boolean isFlag = true;
  6. System.out.println("100以内的所有质数:");
  7. for (int i = 2; i <= 100000; i++) {
  8. for (int j = 2; j < i; j++) {//此处可以用Math.sqrt(i)
  9. if (i % j == 0) {
  10. isFlag = false;
  11. break;
  12. }
  13. }
  14. if (isFlag == true) {
  15. System.out.println(i);
  16. }
  17. isFlag = true;
  18. }
  19. long end = System.currentTimeMillis();
  20. System.out.println("所需要的时间:" + (end - start));
  21. }
  22. }
  23. //老师的优化

3 break、continue关键字的使用

break:只能用于switch语句和循环语句中。
continue: 只能用于循环语句中。
二者功能类似,但continue是终止本次循环,break是终止本层循环。
break、continue之后不能有其他的语句,因为程序永远不会执行其后的语句。
标号语句必须紧接在循环的头部。标号语句不能用在非循环语句的前面。
很多语言都有goto语句,goto语句可以随意将控制转移到程序中的任意一条
语句上,然后执行它。但使程序容易出错。Java中的break和continue是不同
于goto的。

break默认跳出包裹此关键字最近的一层循环或者
QQ截图20211129154802.png
continue 语句
continue只能使用在循环结构中
continue语句用于跳过其所在循环语句块的一次执行,继续下一次循环
continue语句出现在多层嵌套的循环语句体中时,也可以通过标签指明要跳过的是哪一层循环

return:并非专门用于结束循环的,它的功能是结束一个方法。当一个方法执行到一个return语句时,这个方法将被结束。
与break和continue不同的是,return直接结束整个方法,不管这个return处于多少层循环之内

4、项目一练习

见项目作业