if...else if多分支语句/控制台输入成绩/switch...case语句

if...else if多分支语句

控制台输入成绩


package day03;
import java.util.Scanner;
public class IfElseDemo {

	public static void main(String[] args) {

		//运用控制台输入成绩;
		System.out.println("输入成绩:");
		Scanner console=new Scanner(System.in);
		double score = console.nextDouble();
		System.out.print("等级为:");//print不换行
		if (score <= 100 && score >= 90) {
			System.out.println("优秀");
		} else if (score < 90 && score >= 80) {
			System.out.println("良好");
		} else if (score < 80 && score >= 70) {
			System.out.println("中等");
		} else if (score < 70 && score >= 60) {
			System.out.println("及格");
		} else  { 		//该行代码if (score < 60)可省略;
			System.out.println("不及格!");
		
		}	//if...else if多分支语句

	}

}


switch...case语句

控制台输入成绩


package day03;
import java.util.Scanner;
public class SwitchDemo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner console=new Scanner(System.in);
		System.out.print("输入分数:");
		double score =console.nextDouble(); //分数可能为小数,声明为double
		String level = "不及格";// 需要初始化变量,level等级/水平/级别
		switch ((int)(score / 10)) { // 表达式为整数,对score/10的结果转换为int类型
		case 10 : case 9:  //或
			level = "优秀";
			break;// 10或9
		case 8:
			level = "良好";
			break;
		case 7:
			level = "中等";
			break;
		case 6:
			level = "及格";
			break;
		// 此处省略default:level= "不及格";(没有对level初始化时,不能省略;)
		}
		System.out.println(level);
	}

}














你可能感兴趣的:(Java基础部分)