📜  读取成绩并显示等效描述的Java程序

📅  最后修改于: 2022-05-13 01:55:02.479000             🧑  作者: Mango

读取成绩并显示等效描述的Java程序

问题陈述: 1到100的数字是随机的 给定等级作为用户的输入,然后将用户的输入等级与给定的案例进行匹配以生成所需的输出。

所以在技术上用这些方式机器得到

  • 接受用户的输入
  • 匹配等级使用等于 运算符(==)
  • 生成等效的描述作为输出

有几种方法,从蛮力(天真)到最优化。下面讨论很少:

  1. 使用 If-Else 方法
  2. 开关盒方法

使用以下方法分配等级

方法 1: If-Else语句

下面是上述方法的实现

Java
// Java Program to Read Grade & Displaying Equivalent Desc
 
// Importing Classes/Files
import java.util.*;
 
public class GFG {
 
    // Main Driver Method
    public static void main(String[] args)
    {
 
        // User is supposed to enter grade among them
        System.out.println(
            "Enter Grade varying from S,A,B,C,D");
        String grade = "A";
 
        // Checking whether grade == "S" or not
        if (grade == "S") {
            System.out.println(
                "Student has scored between 90 to 100");
        }
        // Checking whether grade == "A" or not
        else if (grade == "A") {
            System.out.println(
                "Student has scored between 80 to 90");
        }
        // Checking whether grade == "B" or not
        else if (grade == "B") {
            System.out.println(
                "Student has scored between 70 to 80");
        }
        // Checking whether grade == "C" or not
        else if (grade == "C") {
            System.out.println(
                "Student has scored between 60 to 70");
        }
        // Checking whether grade == "D" or not
        else if (grade == "D") {
            System.out.println(
                "Student has scored between 50 to 60");
        }
        else {
 
            // Printing message-user pressed some other key
            System.out.println(
                "The grade you entered is not valid!");
        }
    }
}


Java
// Java Program to Read a Grade & Display the Equivalent
 
// Importing Classes/Files
import java.util.*;
 
public class GFG {
 
    //  Main driver method
    public static void main(String args[])
    {
 
        // Random grade taken for consideration
        String grade = "W";
 
        // Using Switch-Case.
        switch (grade) {
            // Checking whether grade == "S" or not.
 
        case "S":
            System.out.println(
                "Student has scored between 90 to 100");
            break;
 
            // Checking whether grade == "A" or not.
        case "A":
            System.out.println(
                "Student has scored between 80 to 90");
            break;
 
            // Checking whether grade == "B" or not.
        case "B":
            System.out.println(
                "Student has scored between 70 to 80");
            break;
 
            // Checking whether grade == "C" or not.
        case "C":
            System.out.println(
                "Student has scored between 60 to 70");
            break;
 
            // Checking whether grade == "D" or not.
        case "D":
            System.out.println(
                "Student has scored between 50 to 60");
            break;
 
        default:
            System.out.println(
                "The grade you entered is not valid!");
        }
    }
}


输出
Enter Grade varying from S,A,B,C,D
Student has scored between 80 to 90

方法二:切换案例

下面是上述方法的实现

Java

// Java Program to Read a Grade & Display the Equivalent
 
// Importing Classes/Files
import java.util.*;
 
public class GFG {
 
    //  Main driver method
    public static void main(String args[])
    {
 
        // Random grade taken for consideration
        String grade = "W";
 
        // Using Switch-Case.
        switch (grade) {
            // Checking whether grade == "S" or not.
 
        case "S":
            System.out.println(
                "Student has scored between 90 to 100");
            break;
 
            // Checking whether grade == "A" or not.
        case "A":
            System.out.println(
                "Student has scored between 80 to 90");
            break;
 
            // Checking whether grade == "B" or not.
        case "B":
            System.out.println(
                "Student has scored between 70 to 80");
            break;
 
            // Checking whether grade == "C" or not.
        case "C":
            System.out.println(
                "Student has scored between 60 to 70");
            break;
 
            // Checking whether grade == "D" or not.
        case "D":
            System.out.println(
                "Student has scored between 50 to 60");
            break;
 
        default:
            System.out.println(
                "The grade you entered is not valid!");
        }
    }
}
输出
The grade you entered is not valid!