目录
流程控制
-
顺序结构
练习一
package struct; public class ShunXunDemo { public static void main(String[] args) { System.out.println("hello1"); System.out.println("hello2"); System.out.println("hello3"); } } /* 运行结果 hello1 hello2 hello3 */
-
switch语句
package struct; public class SwitchDemo01 { public static void main(String[] args) { //case 穿透 switch 匹配一个具体的值 char grade = 'C'; switch (grade){ case 'A': System.out.println("优秀"); break; case 'B': System.out.println("良好"); break; case 'C': System.out.println("及格"); break; case 'D': System.out.println("再接再厉"); break; case 'E': System.out.println("挂科"); break; } } }
运行结果
练习二
package struct; public class SwitchDemo02 { public static void main(String[] args) { String name = "xiaozheng"; //JDK7 的新特性,表达式可以是字符串 // switch (name){ case "zxy": System.out.println("zxy"); break; case "xiaozheng": System.out.println("xiaozheng"); break; default: System.out.println("找不到"); break; } } }
运行结果
-
if
练习一
package struct; import java.util.Scanner; public class IfDemo1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); //equals:判断字符串是否相等 if (s.equals("Hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); } }
运行结果
练习二
package struct; import java.util.Scanner; public class IfDemo3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩"); int score = scanner.nextInt(); if (score==100){ System.out.println("恭喜满分"); }else if (score<100 && score>=90){ System.out.println("A"); }else if (score<90 && score>=80){ System.out.println("B"); }else if (score<80 && score>=70){ System.out.println("C"); }else if (score<70 && score>=60){ System.out.println("D"); }else if (score<60 && score>=0){ System.out.println("不及格"); }else { System.out.println("成绩不合格"); } } }
运行结果
练习三
package struct; import java.util.Scanner; public class lfDemo2 { public static void main(String[] args) { // Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩:"); int score = scanner.nextInt(); //if双选择结构 if (score>60){ System.out.println("及格"); }else{ System.out.println("不及格"); } scanner.close(); } }
运行结果
-
for
练习一
package struct; public class ForDemo01 { public static void main(String[] args) { int a = 1; //初始化条件 while (a <= 100) {//条件判断 System.out.println(a); //循环体 a += 2; //迭代 } System.out.println("while循环结束"); //初始化//条件判断 //迭代 for (int i = 1; i <= 100; i++) { System.out.println(i); } //快捷键100.for回车 System.out.println("for循环结束!"); } }
练习二
package struct; public class ForDemo02 { public static void main(String[] args) { //练习1:计算0到100之间的奇数和偶数的和 int oddSum = 0; int evenSum = 0; for (int i = 0; i <= 100; i++) { if (i%2!=0) { oddSum+=i; }else { //奇数 evenSum += i; //oddSum = oddSum + i } System.out.println("奇数的和:"+oddSum); System.out.println("偶数的和:"+evenSum); } } }
练习三
package struct; public class ForDemo03 { public static void main(String[] args) { //练习2 //用while或for循环输出1-100之间能被5整除的数, //并且每行输出3个 for (int i = 0; i <= 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ //System.out.println();//换行 System.out.print("\n"); //换行 } } //println 输出会换行 //print 输出不会换行 } }
练习四
package struct; public class ForDemo04 { //九九乘法表 public static void main(String[] args) { //1.先打印第一列, //2.把固定的1再用一个循环包起来 //3.去掉重复项,i <= j //4.调整样式 for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(j+"*"+i+"="+(j*i)+"\t"); } System.out.println(); } } } /* 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 */
练习五
package struct; //增强For循环 public class ForDemo05 { public static void main(String[] args) { int[] numbers = {10,20,30,40,50}; //定义了一个数组 for (int i = 0;i<5;i++){ System.out.println(numbers[i]); } System.out.println("====================="); //遍历数组的元素 for (int x :numbers){ System.out.println(x); } } } /* 运行结果 10 20 30 40 50 ===================== 10 20 30 40 50 */
-
-
-
while
练习一
package struct; public class WhileDemo01 { public static void main(String[] args) { //输出1-100 int i = 0; while (i<100){ i++; System.out.println(i); } } }
练习二
package struct; public class WhileDemo02 { public static void main(String[] args) { while (true){ //等待客户端连接 //死循环 } } }
练习三
package struct; public class WhileDemo03 { public static void main(String[] args) { //计算1+2+。。100? int i = 0; int sum = 0; while (i<=100){ sum = sum + i; i++; } System.out.println(sum); } } /* 运行结果 5050 */
-
do....while
练习一
package struct; public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do{ sum = sum + i; i++; }while (i<=100); System.out.println(sum); } } /* 运行结果 5050 */
-
break
package struct; public class BreakDemo { public static void main(String[] args) { int i = 0; while (i<100){ i++; System.out.println(i); if (i==30){ break; } } System.out.print("123"); } }
-
continue
package struct; public class ContinueDemo { public static void main(String[] args) { int i = 0; while (i<100){ i++; if (i%10==0){ System.out.println(); continue; } System.out.print(i); } } }
-
案例1:打印1-100的质数
package struct; public class LabelDemo { public static void main(String[] args) { //打印101-150之间所有的质数 int count = 0; outer:for (int i = 101;i<150;i++){ for(int j =2;j<i/2;j++){ if (i % j ==0){ continue outer; } } System.out.print(i+" "); } } } /* 运行结果 101 103 107 109 113 127 131 137 139 149 */
案例2:打印三角形
package struct; public class TestDemo { //打印三角形 5行三角形 public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) { System.out.print(" "); } for (int j =1; j <= i;j++){ System.out.print("*"); } for (int j = 1;j < i;j++) { System.out.print("*"); } System.out.println(); } } } /* * *** ***** ******* ********* 进程已结束,退出代码为 0 */