JAVA if、if-else、拓展的if-else语句

选择结构:执行路线分叉。
1.单if语句:执行某些步骤或者不执行它们。

if(布尔表达式){
	语句体
}

2.标准的if-else语句:在两套步骤方法中选择一种执行。

if(布尔表达式){
	语句体A
}
else{
	语句体B
}

3.扩展的if-else语句;在三套或者更多的步骤方法选择一种执行

if(条件判断1){
	语句体1
}
else if(条件判断2){
	语句体2
}else if(条件判断N){
	语句体N
}
else{
	语句体N+1
}

执行流程:
1.如果判断1成立,执行1,后续代码不再执行。
2.判断1不成立,跳过语句1,执行条件判断23.都不成立则else
/*
键盘输一个考试成绩:进行等级划分、
优秀:90-100
良好:80-89
中等:70-79
及格:60-69
不及格:60以下
*/
import java.util.Scanner;
public class Ex{
	public static void main(String[] args){
		System.out.print("请输入成绩:");
		Scanner sc = new Scanner(System.in);
		int score = sc.nextInt();
		if (score>=90 && score<=100)
		{
			System.out.println("优秀");
		}
		else if (score>=80 && score<=89)
		{
			System.out.println("良好");
		}
		else if (score>=70 && score<=79)
		{
			System.out.println("中等");
		}
		else if (score>=60 && score<=69)
		{
			System.out.println("及格");
		}
		else if (score>=0 && score<=59)
		{
			System.out.println("及格");
		}
		else
		{
			System.out.println("数据错误!请输入范围0-100.");
		}
	}
}
/*
键盘输入两个int数字,并求出其中最大值
*/
import java.util.Scanner;
public class Iftest{
	public static void main(String[] args){
		System.out.print("两个int数字比较大小");
		Scanner sc = new Scanner(System.in);
		
		System.out.print("输入第一个数:");
		int num1 = sc.nextInt();
		System.out.print("输入第二个数:");
		int num2 = sc.nextInt();
		
		if (num1 > num2){
			System.out.print("最大值" + num1);
		}
		else if( num1 >= num2)
			System.out.print("最大值"+ num1);
		else{
			System.out.print("最大值"+ num2);
		}
	}
}
/*
键盘输入三个int数字,求其最大值。

1.先把前两个比较,再跟第三个比
*/

import java.util.Scanner;
public class Iftest2{
	public static void main(String[] args){
		int a,b,c;
		int middle;
		int max;
		Scanner sc = new Scanner(System.in);
		System.out.print("输入一个int数字");
		a = sc.nextInt();
		System.out.print("输入一个int数字");
		b = sc.nextInt();
		System.out.print("输入一个int数字");
		c = sc.nextInt();
		
		if (a>b){
			middle = a;
			if (middle > c){
				max = middle;
				System.out.print("最大值为"+max);
			}
			else{
				max = c;
				System.out.print("最大值为"+max);
			}
		}
		else{
			middle = b;
			if (middle > c){
				max = middle;
				System.out.print("最大值为"+max);
			}
			else{
				max = c;
				System.out.print("最大值为"+max);
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值