• Java基础_0205: 程序逻辑结构


    使用if语句进行判断
    
    public class TestDemo {
    	public static void main(String args[]) {
    		double score = 90.0;		// 定义变量
    		if (score > 60.0) {		// 设置判断条件
    			System.out.println("及格了!");
    		}
    	}
    }
    
    程序执行结果:	及格了!
    
    
    使用if…else判断
    public class TestDemo {
    	public static void main(String args[]) {
    		double score = 30.0; 	// 定义变量
    		if (score > 60.0) { 	// 条件判断满足
    			System.out.println("及格了!");
    		} else { 			// 条件判断不满足
    			System.out.println("小白的成绩!");
    		}
    	}
    }
    程序执行结果:	小白的成绩!
    
    使用if…else if…else判断
    public class TestDemo {
    	public static void main(String args[]) {
    		double score = 91.0; 		// 定义变量
    		if (score < 60.0) {		// 条件判断
    			System.out.println("小白的成绩!") ;
    		} else if (score >= 60 && score <= 90) {// 条件判断
    			System.out.println("中等成绩") ;
    		} else if (score > 90 && score <= 100) {// 条件判断
    			System.out.println("优秀成绩") ;
    		} else {			// 条件判断都不满足
    			System.out.println("你家的考试成绩这么怪异!") ;
    		}
    	}
    }
    
    程序执行结果:	优秀成绩
    
    
    switch

    对于多条件判断使用if..else if…else是可以判断布尔条件的,如果是多数值判断,可以通过switch完成,语法如下

    switch(整数 | 字符 | 枚举 | String) {
    	case 内容 : {
    		内容满足时执行 ;
    		break ;
    	}
    	case 内容 : {
    		内容满足时执行 ;
    		break ;
    	}
    	case 内容 : {
    		内容满足时执行 ;
    		break ;
    	} ... 
    	default : {
    		内容都不满足时执行 ;
    		break ;
    	}
    }
    
    
    public class TestDemo {
    	public static void main(String args[]) {
    		int ch = 1;
    		switch (ch) { 		// 判断的是数字
    			case 2: {	// 判断内容是否是2
    				System.out.println("内容是2");
    				break;
    			}
    			case 1: {	// 判断内容是否是1
    				System.out.println("内容是1");
    				break;
    			}
    			case 3: {	// 判断内容是否是3
    				System.out.println("内容是3");
    				break;
    			}
    			default: {	// 判断都不满足
    				System.out.println("没有匹配内容");
    				break;
    			}
    		}
    	}
    }
    
    程序执行结果:	内容是1
    
    public class TestDemo {
    	public static void main(String args[]) {
    		String str = "HELLO";
    		switch (str) { 	// 判断的是字符串
    		case "HELLO": {
    			System.out.println("内容是HELLO");
    			break;
    		}
    		case "hello": {
    			System.out.println("内容是hello");
    			break;
    		}
    		case "mldn": {
    			System.out.println("内容是mldn");
    			break;
    		}
    		default: {
    			System.out.println("没有匹配内容");
    			break;
    		}
    		}
    	}
    }
    程序执行结果:	内容是HELLO
    
    实现1 ~ 100的累加 —— 使用while循环
    public class TestDemo {
    	public static void main(String args[]) {
    		int sum = 0; 		// 保存总和
    		int current = 1; 		// 循环的初始化条件
    		while (current <= 100) { 	// 循环结束条件
    			sum += current; 	// 累加
    			current++;	// 改变循环条件
    		}
    		System.out.println(sum);
    	}
    }
    
    程序执行结果:	5050
    
    使用do..while循环实现累加操作
    public class TestDemo {
    	public static void main(String args[]) {
    		int sum = 0; 		// 保存总和
    		int current = 1; // 循环的初始化条件
    		do { 	// 循环结束条件
    			sum += current; // 累加
    			current++;	// 改变循环条件
    		} while (current <= 100); 	// 循环结束判断
    		System.out.println(sum);
    	}
    }
    
    程序执行结果:	5050
    
    使用for循环实现1 ~ 100累加
    public class TestDemo {
    	public static void main(String args[]) {
    		int sum = 0; 		// 保存总和
    		// 设置循环初始化条件current,同时此变量作为累加操作使用
    		// 每次执行循环体前都要进行循环判断(current <= 100)
    		// 循环体执行完毕后会自动执行“current++”改变循环条件
    		for (int current = 1; current <= 100; current++) {
    			sum += current;	// 循环体中实现累加操作
    		}
    		System.out.println(sum);
    	}
    }
    
    程序执行结果:	5050
    
    输出乘法口诀表
    public class TestDemo {
    	public static void main(String args[]) {
    		for (int x = 1; x <= 9; x++) { // 控制循环的行数
    			for (int y = 1; y <= x; y++) {// 控制列数
    				System.out.print(x + "*" + y + "=" + (x * y) + "	");
    			}
    			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	
    
    
    
    循环控制

    正常情况下只要执行了循环,那么只要循环条件满足,循环体的代码就会一直执行,但是在程序之中也提供有两个循环停止的控制语句:continue(退出本次循环)、break(退出整个循环)。此类的语句在使用时往往要结合分支语句进行判断。

    观察continue
    public class TestDemo {
    	public static void main(String args[]) {
    		for (int x = 0; x < 10; x++) {
    			if (x == 3) {
    				continue; // 之后的代码不执行,直接结束本次循环
    			}
    			System.out.print("x = " + x + "、");
    		}
    	}
    }
    程序执行结果:	x = 0、x = 1、x = 2、x = 4、x = 5、x = 6、x = 7、x = 8、x = 9、
    
    观察break
    public class TestDemo {
    	public static void main(String args[]) {
    		for (int x = 0; x < 10; x++) {
    			if (x == 3) {
    				break; // 退出整个循环
    			} 
    			System.out.print("x = " + x + "、");
    		}
    	}
    }
    程序执行结果:	x = 0、x = 1、x = 2、
    
    
  • 相关阅读:
    VUE中is的作用
    lable便签 for的作用
    Java第五课
    Java第四课课后作业
    Java第四课
    Unit6Java运算符
    Unit5Java数据类型
    Unit4如何使用类
    Java如何设计并编写类
    IDEA安装Alibaba,SonarLint代码规范检查插件
  • 原文地址:https://www.cnblogs.com/xuwei1/p/8338278.html
Copyright © 2020-2023  润新知