读取成绩并显示等效描述的Java程序
问题陈述: 1到100的数字是随机的 给定等级作为用户的输入,然后将用户的输入等级与给定的案例进行匹配以生成所需的输出。
Real life Example
Consider a number on a scale of 1 to 100 are written on a piece of paper slips. Now person starts picking slips one by one and starts organizing them in such a manner that he starts putting slips in 10 different boxes prior deciding making 10 different pots in such a manner slips numbering 1-10 in ‘pot A’, 11-20 in ‘pot B’ and so on till labeling and inserting slips till all the slips are organized in labeled pots. He did this in order to easily figure out numbers written on slips.
所以在技术上用这些方式机器得到
- 接受用户的输入
- 匹配等级使用等于 运算符(==)
- 生成等效的描述作为输出
有几种方法,从蛮力(天真)到最优化。下面讨论很少:
- 使用 If-Else 方法
- 开关盒方法
使用以下方法分配等级
方法 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!