这是一个等级检查程序,用户应该输入0-100等级,并说明他们得到的等级。我看过其他地方但找不到任何东西。
提前致谢
import java.util.*;
public class grading{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Please input grade");
int grade = scan.nextInt();
if (grade > 100){
System.out.println("Sorry please put a number between 0-100");
}
else if (grade > 90){
grade = "A";
System.out.println("This student recieved " + grade " for his/her grade");
}
else if (grade > 75){
grade = "B";
System.out.println("This student recieved " + grade " for his/her grade");
}
else if (grade > 49){
grade = "C";
System.out.println("This student recieved " + grade " for his/her grade");
}
else{
grade = "F";
System.out.println("This student recieved " + grade " for his/her grade");
}
}
}
答案 0 :(得分:3)
在您的每个println
语句中,您都错过了+
与字符串最后部分之间的grade
连接运算符。
您似乎也在使用grade
两个不同的东西,即用户输入的整数和串联中使用的字符串。
后者可能应该是一个独特的变量,如:
int grade = scan.nextInt();
String strGrade;
:
else if (grade > 90){
strGrade = "A";
System.out.println("This student received " + strGrade + " for his/her grade");
}
另外,为了在输入有符号整数时的完整性,你应该也可以使第一个检查句柄为负数:
if ((grade < 0) || (grade > 100)) {
System.out.println("Sorry please put a number between 0-100");
}
此外,您可以重构每个条件块中的重复代码,从而使程序更具可读性:
import java.util.*;
public class Grading {
public static void main(String[] args) {
// Get the grade.
Scanner scan = new Scanner(System.in);
System.out.println("Please input grade");
int grade = scan.nextInt();
// Validate grade.
if ((grade < 0) || (grade > 100)) {
System.out.println("Sorry please put a number between 0-100");
} else {
// If valid, default to F, upgrade based on grade.
String strGrade = "F";
if (grade > 90) strGrade = "A";
else if (grade > 75) strGrade = "B";
else if (grade > 49) strGrade = "C";
// Output the results.
System.out.println("This student received " + strGrade + " for his/her grade");
}
}
}
答案 1 :(得分:0)
import java.util.*;
public class grading{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Please input grade");
int grade = scan.nextInt();
String gradeLevel = "";
if (grade > 100){
System.out.println("Sorry please put a number between 0-100");
}
else if (grade > 90){
gradeLevel = "A";
System.out.println("This student recieved " + gradeLevel + " for his/her grade");
}
else if (grade > 75){
gradeLevel = "B";
System.out.println("This student recieved " + gradeLevel + " for his/her grade");
}
else if (grade > 49){
gradeLevel = "C";
System.out.println("This student recieved " + gradeLevel + " for his/her grade");
}
else{
gradeLevel = "F";
System.out.println("This student recieved " + gradeLevel + " for his/her grade");
}
}
}
答案 2 :(得分:0)
在需要+
的字符串之间连接