06 Java语言基础语法 - 4

第四章 条件结果

条件语句 上

简单if语句

  1. 语法
    if、else属于条件 分支语句
if (条件){			// 条件为条件表达式或逻辑表达式
	语句1			// 条件=true时,条件成立,执行语句1
}else{
	语句2			// 条件=false时,条件不成立,执行语句2
}

在这里else可以省略
可在if、else中间嵌套if语句
2. 要点
程序流程有三个要点:

  • 顺序执行
  • 选择执行
  • 循环执行
    条件分支语句用于选择执行。
  1. 举例
public class IfDemo{
	public static void main(String[] args){
		int cash=500;
		// 最简单的if语句
		if(cash==400){
			System.out.println("一定在做梦");
			//当if语句块中只有一条指令时,可以省略大括号;
			//不推荐使用,尽量使用大括号。
		}
			System.out.println("哈哈");
		//在这种情况下,System.out.println("哈哈");已经不属于if语句的内容了
			
			if cash>=500{
				System.out.println("买别墅");
			}else{
				System.out.println("买公寓");
			}
			
			int a=10;
			int b=1;
			int c;
			if(a<b){
				c=a;
			}else{
				c=b;
			}
			System.out.println(c);

			// 条件表达式,它是一个简化的if ··· else语句
			c=(a<b)?a:b;	//若a<b成立,将a赋值给c,否则将b赋值给c
			System.out.println(c);

			if(cash>=500){
				if(currency=="$"){
					System.out.println("买独栋别墅");
				}else{
					System.out.println("买联排别墅");
				}
			}else{
				System.out.println("买公寓");
			}
	}
}

多重if、else语句

  1. 语法
if(条件1){
	语句1;
}
else if(条件2){
	语句2;
}else{
	语句3;
}
  1. 流程图
    在这里插入图片描述
  2. 要点
    else总是属于前面最近的,还没有对应else的if。
  3. 编码规范
    加大括号增加可读性。
public class IfDemo{
	public static void main(String[] args){
		int cash=500;
		
		// 多重if ... else语句
		if(cash>500){
			System.out.println("买在内环");
		}else if(cash>300){
			System.out.println("买在中环");
		}else if(cash>200){
			System.out.println("买在外环");
		}else if(cash>100){
			System.out.println("买在外环外");
		}else{
			System.out.println("努力赚钱");
		}
	}
}

条件语句 下

switch、case语句

  1. 语句
switch(表达式)				// switch计算表达式的值为常量
{
	case 常量1:
			语句1;
			break;
	case 常量2:
			语句2;
			break;
	default:				// 等于其他值:执行语句3;可省略
			语句3;
}
  1. 要点
  • case为XX为Tag
  • 表达式的值为整型数,不能为long型
  • break语句,通常是需要的
  • default可选
  1. 适用场合
  • 多重条件判断
  • 表达式结果取值是固定值而不是范围
import java.util.Scanner;
public class SwitchDemo{
	public static void main(String[] args){
		Scanner input=new Scanner(System.in);
		System.out.println("请输入1~5之间的一个数");
		int number=input.nextInt();
		if (number==1){
		}else if (number==2){
		}else if (number==3){
		}else if (number==4){
		}else if (number==5){
		}else{
		}
		
		//int number=1;
		//short number=1;
		//byte number='1';
		//String number="abc";
		
		switch(number){
			case 1:
				System.out.println("1");
				break;
			case '1':
				System.out.println("2");
				break;
			case "abc":
				System.out.println("3");
				break;
			case 4:
				System.out.println("4");
				break;
			case 5:
				System.out.println("5");
				break;
			default:
				System.out.println("error!");
				// break;
		}
	}
}

1. case后面所列出的常量不能重复。
2. break是可以省略的。
3. case的顺序是可以颠倒的,default可以放在任何位置,一般推荐放在最后。
4. switch是用来匹配常量的,能匹配的类型有byte,short,int,char,String,(jdk1.7版本才开始有的), 
jdk1.5 enum(后续会讲)5. switch和多重if ··· else语句比较,switch适合做等值判断,不适合做区间判断,作等值判断时
语法更简洁直观。		多重if语句功能比switch更全面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值