分别用if,switch循环语句进行百分制分数等级判断

一:使用if语句

import java.util.Scanner;

public class Test01 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入分数");
		int grade = sc.nextInt();
		if(grade>100||grade<0) {						//成绩不在0~100范围
			System.out.println("对不起,你输入的成绩有误");
		}else {
			if(grade>=90 && grade<=100) {
				System.out.println("成绩为优秀");
			}else if(grade>=80&&grade<=89){
				System.out.println("成绩为良好");
			}else if(grade>=70&&grade<=79){
				System.out.println("成绩为中等");
			}else if(grade>=60&&grade<=69){
				System.out.println("成绩为及格");
			}else{
				System.out.println("成绩为不及格");
			}
		}
		
	}
}

二:使用switch语句

package com.neusoft.homework;

import java.util.Scanner;

public class Test02 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入成绩");
		int grade = sc.nextInt();
		
		if(grade<0||grade>100) {
			System.out.println("对不起,你输入的成绩有误");
		}else {
			switch(grade/10) {						//分数除以10得到分数的十位数字
			case 10:										//100分与90~99分都为优秀
			case 9:
				System.out.println("优秀");
				break;
			case 8:
				System.out.println("良好");
				break;
			case 7:
				System.out.println("中等");
				break;
			case 6:
				System.out.println("及格");
				break;
			default:
				System.out.println("不及格");
			}
		}
	}
}

你可能感兴趣的:(java)