我写了这个程序,将数字等级转换成字母等级,但我一直得到同样的错误。有人能帮我弄清楚我做错了吗?
import static java.lang.System.*;
import java.util.Scanner;
public class Grade
{
private int numGrade;
public Grade()
{
Grade test;
}
public void setGrade(int grade)
{
numGrade = grade;
if (grade >= 90)
{
System.out.println("A");
}
{
System.out.println("B");
}
public String getLetterGrade( ) {
String letGrade="A";
if (grade>= 90)
{
return letGrade;
}
public String toString(){
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
答案 0 :(得分:1)
使用的语法不正确。
附件是进行转换的示例代码
public class Grade {
private int numGrade;
public void setGrade(int grade) {
numGrade = grade;
if (grade >= 90) {
System.out.println("A");
} else {
System.out.println("B");
}
}
public String getLetterGrade() {
String letGrade = "B";
if (numGrade >= 90) {
return "A";
}
return letGrade;
}
public String toString() {
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
答案 1 :(得分:1)
似乎你试图从许多不同的位置攻击同样的东西。 首先让我们从将数字等级转换为字母等级开始,因此在参与输入之前,首先询问用户希望转换的等级类型
char choise;
choise = reader.nextChar(); //ask for N or L for numerical or letter
接下来我将展示一个字母到数字转换的示例代码
public int getNGrade(char grade)
{
if (grade == 'A')
return 90;
else if (grade == 'B')
return 80; //and so on
}
同样的方法可以用于数字到字母的转换
在主类中你调用函数:
charGrade = reader.nextChar();
System.out.println("Your grade in numbers is " + getNGrade(charGrade));
我猜这是你的意思,希望我能提供帮助。
答案 2 :(得分:1)
您的代码可以是这样且完美无缺:
public class Grade {
private int numGrade;
public Grade(int grade) {
numGrade = grade;
}
public int getGrade() {
return numGrade;
}
public void setGrade(int grade) {
numGrade = grade;
}
public String getLetterGrade() {
if(numGrade <0 || numGrade > 100) throw new IllegalArgumentException("No such a grade!");
else if(numGrade>=90) return "A";
else if (numGrade >= 80) return "B";
else if(numGrade >= 70) return "C";
else if(numGrade >= 60) return "D";
else return "F";
}
public String toString(){
return numGrade + " is a " + getLetterGrade() + "\n";
}
}
您可以在同一个类中包含main方法或创建一个单独的类进行测试:
public static void main(String[] args) {
Grade g = new Grade(75); //you can enter the grade manually or simply using a Scanner object
System.out.println(g);
}
答案 3 :(得分:-2)
import java.util.Scanner;
class Tutorial {
public static void main(String args[]){
Scanner input = new Scanner(System.in); // calling Scanner method
String restart = "Y"; //initialising the restart variable
while (restart.equals("Y")) // testing the conditon(if Y is equals then it continues)
{
int grade;
System.out.println("WELCOME TO ABD GRADING SYSTEM.");
System.out.println("Enter your Score(between 1 - 100) : "); // Displaying a message on screen
grade = input.nextInt(); // Accept Input from the user
if(grade<=39)
System.out.println("Your grade is F9");
else if(grade==40 || grade<=49)
System.out.println("Your grade is D7");
else if(grade==50 || grade<=59)
System.out.println("Your grade is C6");
else if(grade==60 || grade<=69)
System.out.println("Your grade is C5");
else if(grade==70 || grade<=79)
System.out.println("Your grade is B2");
else if(grade==80 || grade<=100)
System.out.println("Your grade is A1");
else
{
System.out.println("Input Correct score between (1 - 100).");
}
System.out.println("THANK YOU.");
System.out.println("Would you like to Calculate again? Y/N ");
restart = input.next();
}
}
}