判断语句

1、If的基本使用及格式

格式1:if(条件表达式)

执行代码


注意:1、以后代码格式:

if(条件表达式){

执行代码

}

2、if(条件表达式)

执行代码

执行代码格式:

A、单条语句 ----- 容易造成代码误读

B、块语句 ------- {} ---提高程序的易读性和易维护性

C、带标签的块语句

编码规范:

1、只跟块语句

2、如果非要跟单条语句

格式:if(条件表达式)code;

import java.util.Scanner;
class FlowPath00
{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个数字:");
		int num = scanner.nextInt();
		if(num > 0){
			System.out.println(num + "是正数");
		}
		if(num == 0){
			System.out.println(num + "是0");
		}
		if(num < 0){
			System.out.println(num + "是负数");
		}
	}
}
/**
	1、当if判断时,结果不是true就是false
	2、如果使用两次if,判断两次,资源浪费
	3、使用 if else

	格式:
	if(条件表达式){
		code;
	} else {
		code;
	}
*/
class FlowPath01
{
	public static void main(String[] args){
		int num1 = 1;
		int num2 = 2;

		/*if(num1 - num2 > 0){
			System.out.println("num1比num2大");
		}
		if(num1 - num2 <= 0){
			System.out.println("num1不比num2大");
		}*/
		if(num1 - num2 > 0){
			System.out.println("num1比num2大");
		} else {
			System.out.println("num1不比num2大");
		}
	}
}

2、If else if 语句 

        格式:

if(条件表达式){

code

}else if(条件表达式){

code

}else if(条件表达式){

code

}else if(条件表达式){

code

}else {

code

}

注意:只要有一个符合条件,其余都不执行

        判断一个数字是正数负数还是0

import java.util.Scanner;
class FlowPath02
{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个数字");
		int num = scanner.nextInt();
		/*if (num < 0){
			System.out.println(num + "是负数");
		}else if(num == 0){
			System.out.println(num + "是0");
		}else {
			System.out.println(num + "是正数");
		}*/
		if (num < 0){
			System.out.println(num + "是负数");
		}else if(num == 0){
			System.out.println(num + "是0");
		}else if(num < -10){
			System.out.println("num小于-10");//输入-100运行结果是-100是负数而当前这条语句不执行(前面有符合条件的其余的都不执行)
		}else {
			System.out.println(num + "是正数");
		}
	}
}

3、If else 语句与三元运算符的比较

class FlowPath03
{
	public static void main(String[] args){
		int num1 = 1;
		int num2 = 2;
		int result = num1 > num2 ? num1 : num2;
		System.out.println("result = " + result);

		//定义一个接收的数据
		int max;
		if(num1 > num2){
			max = num1;
		}else{
			max = num2;
		}
		System.out.println("max = " + max);
		//--------------------------------------------
		if(num1 > num2){
			System.out.println("max = " + num1);
		}else{
			System.out.println("max = " + num2);
		}
	}
}

4、区间的使用

        判断一个数据是否属于一个区间,常规的数学格式 60 <= score < 70 非法

正确格式:score >= 60 && score < 70

import java.util.Scanner;
class FlowPath04
{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入一个成绩:");
		int score = scanner.nextInt();
		if(score >= 0 && score < 60){
			System.out.println("不及格");
		} else if(score >= 60 && score < 70){
			System.out.println("及格");
		}else if(score >= 70 && score < 80){
			System.out.println("一般");
		}else if(score >= 80 && score < 90){
			System.out.println("良好");
		}else if(score >= 90 && score <=100){
			System.out.println("优秀");
		}else {
			System.out.println("非法数据");
		}
	}
}

5、switch case 的使用

格式:

switch(数据){

case 常量1:

code 

break;

case 常量2:

code

break;

.......

default:

code

break;

}

注意事项:

1、使用的数据类型:byte short int char -------(int型)

enum(枚举) string ------字符串

2、case后必须是常量

3、规范:break必须加,除非极特殊情况

4、default可以用也可以不用

5、default位置无所谓 ----都是最后执行

6、default如果在最后,可以不使用break

import java.util.Scanner;
class FlowPath05
{
	public static void main(String[] args){
		/*String str1 = "ABC";
		String str2 = "DEF";
		String str = "ABC";
		switch(str){
			case str1:  -----------非法的
				System.out.println("ABC");
			break;
			case str2:
				System.out.println("DEF");
			break;
		}*/
		/*String str = "ABC";
		switch(str){
			case "ABC":
				System.out.println("ABC");
			break;
			case "DEF":
				System.out.println("DEF");
			break;
		}*/
		/*long l = 1L;
		switch((int)1){
			case 1:
				System.out.println("A");
			break;
			case 2:
				System.out.println("B");
			break;
		}*/
		Scanner scanner = new Scanner(System.in);
		int level = scanner.nextInt();
		switch(level){
			case 1:
				System.out.println("初级");
			break;
			case 2:
				System.out.println("中级");
			break;
			case 3:
				System.out.println("高级");
			break;
			default:
				System.out.println("非法数据");
			break;
		}
		char c = 'A';
		switch(c){
			case 'A':
				System.out.println("A");
			break;
			case 'B':
				System.out.println("B");
			break;
		}
	}
}

6、if else 与switch case 的比较

/**
	需求:根据输入的月份查找季节
	switch case 和if else 比较:
	1、if else ------更常用
	2、if else 适应性更广 ---- 区间判断,if else 更适合
	3、switch case 效率更高
	   移动端开发,比如游戏:选择少,效率要求高使用switch case
*/
import java.util.Scanner;
class FlowPath06
{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int month = scanner.nextInt();
		if(month >= 3 && month <= 5){
			System.out.println("春季");
		} else if(month >= 6 && month <= 8){
			System.out.println("夏季");
		} else if(month >= 9 && month <= 11){
			System.out.println("秋季");
		} else if(month >= 1 && month <= 2 || month ==12){
			System.out.println("冬季");
		} else {
			System.out.println("非法数据");
		}
		/*switch(month){
				case 1:
				System.out.println("冬季");
				break;
				case 2:
				System.out.println("冬季");
				break;
				case 3:
				System.out.println("春季");
				break;
				case 4:
				System.out.println("春季");
				break;
				case 5:
				System.out.println("春季");
				break;
				case 6:
				System.out.println("夏季");
				break;
				case 7:
				System.out.println("夏季");
				break;
				case 8:
				System.out.println("夏季");
				break;
				case 9:
				System.out.println("秋季");
				break;
				case 10:
				System.out.println("秋季");
				break;
				case 11:
				System.out.println("秋季");
				break;
				case 12:
				System.out.println("冬季");
				break;
				default:
				System.out.println("非法数据");
				break;     // -----方式一


				
				case 3:
				case 4:
				case 5:
				System.out.println("春季");
				break;
				case 6:
				case 7:
				case 8:
				System.out.println("夏季");
				break;
				case 9:
				case 10:
				case 11:
				System.out.println("秋季");
				break;
				case 12:
				case 1:
				case 2:
				System.out.println("冬季");
				break;
				default:
				System.out.println("非法数据");
				break;// ------方式二 

		}*/
	}
}


转载于:https://my.oschina.net/zhanghongjun289/blog/652002

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值