1.if循环
1 import java.util.Scanner; 2 //if循环 3 public class Demo10 { 4 public static void main(String[] args) { 5 //判断考试分数,大于60就是及格,小于60就是不及格 6 Scanner scanner = new Scanner(System.in); 7 System.out.println("请输入成绩:"); 8 int score=scanner.nextInt(); 9 if (score<60&&score>=0){ 10 System.out.println("不及格"); 11 }else if (score>60 &&score<70){ 12 System.out.println("及格"); 13 }else if (score>70 && score<80){ 14 System.out.println("中等"); 15 }else if (score>80&&score<90){ 16 System.out.println("良好"); 17 }else if (score>902&&score<100){ 18 System.out.println("优秀"); 19 }else if (score==100){ 20 System.out.println("满分!"); 21 }else{ 22 System.out.println("成绩不合法"); 23 } 24 scanner.close(); 25 } 26 }
2.while、dowhile、for循环
1 public class Demo11 { 2 public static void main(String[] args) { 3 //while(){} 4 int a =0; 5 while( a < 10 ){ 6 System.out.print(a+"\t"); 7 a++; 8 } 9 System.out.println(); 10 System.out.println("==========================="); 11 12 13 //do{}while() 14 int b=0; 15 do { 16 System.out.print(b+"\t"); 17 b++; 18 }while(b<10); 19 System.out.println(); 20 System.out.println("==========================="); 21 22 23 //for(){} 24 for (int c=0;c<10;c++){ 25 System.out.print(c+"\t"); 26 } 27 System.out.println(); 28 System.out.println("==========================="); 29 30 } 31 }
3.增强for循环
1 //增强for循环:遍历数组 2 int[] numbers= {10,20,30,40,50}; //定义一个数组 3 //for循环写法 4 for(int i=0;i<5;i++){ 5 System.out.print(numbers[i]); 6 System.out.print(‘\t‘); 7 } 8 System.out.println(); 9 System.out.println("==========================="); 10 //增强for循环写法 for (int x:numbers)相当于for x in numbers[] 11 for (int x:numbers){ 12 System.out.print(x); 13 System.out.print(‘\t‘); 14 }
4.switch循环
1 public class Demo12 { 2 public static void main(String[] args) { 3 //根据成绩的等级判断成绩的好坏 4 char grade = ‘C‘; 5 6 switch (grade){ 7 case ‘A‘: 8 System.out.println("优秀"); 9 break; //每个case下面都要加上break; 否则会出现case穿透的错误 10 case ‘B‘: 11 System.out.println("良好"); 12 break; 13 case ‘C‘: 14 System.out.println("及格"); 15 break; 16 case ‘D‘: 17 System.out.println("差"); 18 break; 19 default: 20 System.out.println("未知等级"); 21 } 22 } 23 }
5.continue
break在任何循环的主体部分,均可以用于强制退出循环,不执行循环中剩余的语句。
continue用于终止某次循环过程,即跳过循环中剩余的语句,接着进行下一次是否执行循环的判定。
1 public class Demo13 { 2 public static void main(String[] args) { 3 int i =0; 4 while (i<100){ 5 i++; 6 if (i%10==0){ 7 System.out.println(); 8 continue; //continue在这里就是当i%10为0的时候就跳出当前循环,也就是不执行System.out.print(i); 9 } 10 System.out.print(i); 11 } 12 } 13 }
原文:https://www.cnblogs.com/ruanzhendong/p/14922323.html