Java代码复习 - 程序结构之分支语句练习

import java.util.Scanner;

public class If {
		public static void main(String[] args){
		    fun1();
		    //fun2();
			//fun3();
			//fun4();
	}
		public static void fun1(){
		//if 分支
		//if(条件){代码;} 如果条件为真或成立,则执行代码,否则不执行
		//输入两个数a,b如果a大于b,则输出a+b的和,如果a不大于b则无任何输出
		System.out.println("请输入两个数");
		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
		int b = input.nextInt();
		if(a>b){
			System.out.println(a+b);
		}
		}
		public static void fun2(){
			//if...else分支 
			//if(条件){代码1;}else{代码;}   如果条件成立,则执行代码1;条件不成立则执行代码2
			//输入分数,60分及以上,输出恭喜,通过,60以下输出对不起
			System.out.println("Please Enter Your Score:");
			Scanner input = new Scanner(System.in);
			int c = input.nextInt();
			if(c>=60){
				System.out.println("Congratulations!!!Passed!!!");
			}
			else{
				System.out.println("Sorry...	Ready?");
			}
		}
		public static void fun3(){
			//if...else if...else if...else.....分支
			//if(条件1){代码1}else if(条件2){代码2}else if(条件3){代码3}else{代码4}.......
			//如果条件1成立,执行代码1、否则,代码2成立,执行代码2、否则代码3成立,执行代码3、否则执行代码4.。。。。。。
			//输入分数,80-100 输出A,70-80输出B,60-70输出C,60以下输出不及格
			System.out.println("请输入您的分数:");
			Scanner input = new Scanner(System.in);
			int d = input.nextInt();
				if(d<=100&d>=80){System.out.println('A');}
			else
				if(d<80&d>=70){System.out.println('B');}
			else
				if(d<70&d>=60){System.out.println('C');}
			else
				if(d<60&d>=0){System.out.println("不及格");}
			else
			{System.out.println("请输入有效分数");}
		}
		public static void fun4(){
			//switch...case...分支
			//switch(值){case 1:代码1;break;case 2:代码2;break;case 3:代码3;break;......default:代码n;}
			//1.代码执行的时候,条件满足,遇到break停止(跳出)
			// 2.default不管放到哪里,值都先跟 case 后面的值比较,不满足条件才执行default语句,建议放在最后
			//3.当所有条件都不满足,才会执行 default中的语句
			//4.没有遇到break,直到执行到switch中的大括号为止
			//输入课程号输出对应课程,否则输出自习课
			System.out.println("请输入1至4的整数:");
			Scanner input = new Scanner(System.in);
			int e = input.nextInt();
			switch(e){
			case 1:
				System.out.println("数学课");
			break;	
			case 2:
				System.out.println("语文课");
			break;
			case 3:
				System.out.println("英语课");
			break;
			case 4:
				System.out.println("体育课");
			break;
			default:
				System.out.println("自习课");	
		}
		}		
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值