import java.util.Scanner;
public class EP55Out
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter a grade: ");
String letter = in.next();
// getNumericGrade(grade);
}
public static void getNumericGrade(double grade)
{
int spacer = 0;
String first = letter.substring(0,1);
String second = letter.substring(1,2);
if (first.equalsIgnoreCase("a"))
grade = 4.0;
else if (first.equalsIgnoreCase("b"))
grade = 3.0;
else if (first.equalsIgnoreCase("c"))
grade = 2.0;
else if (first.equalsIgnoreCase("d"))
grade = 1.0;
else if (first.equalsIgnoreCase("f"))
grade = 0;
else grade = 9;
if (second.equalsIgnoreCase("+"))
grade += 0.3;
else if (second.equalsIgnoreCase("-"))
grade -= 0.3;
else
grade += 9;
System.out.println(grade);
}
}
我不知道你们是否明白我的意思,但我不断得到“等级”不存在的错误,即使它是在主要定义的。我知道它与“无效”有关但我不知道是什么。有人可以帮忙吗?
答案 0 :(得分:1)
所以你正在学习Java不是吗?
我会试着解释你做错了什么。
import java.util.Scanner;
public class EP55Out {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Please enter a grade: ");
String letter = in.next();
//You need to give letter to the method
getNumericGrade(letter);
}
//AND the method should need String and not double
public static void getNumericGrade(String letter){
//After this you need a real double value - lets call is grade;
double grade = 0;
int spacer = 0;
String first = letter.substring(0,1);
String second = letter.substring(1,2);
//You cant to numeric stuff with a String- thats why we use our double here
if (first.equalsIgnoreCase("a"))
grade = 4.0;
else if (first.equalsIgnoreCase("b"))
grade = 3.0;
else if (first.equalsIgnoreCase("c"))
grade = 2.0;
else if (first.equalsIgnoreCase("d"))
grade = 1.0;
else if (first.equalsIgnoreCase("f"))
grade = 0;
else grade = 9;
if (second.equalsIgnoreCase("+"))
grade += 0.3;
else if (second.equalsIgnoreCase("-"))
grade -= 0.3;
else
grade += 9;
System.out.println(grade+"");
}
}
你错过了变量的重点...... 如果在方法中定义变量 - 它只能在THE DEFINING METHOD中看到!
所以 public static void getNumericGrade(String letter) 不知道有一个字母变量 public static void main(String [] args)
希望我能帮助你;)
答案 1 :(得分:0)
如果我理解正确,并且如果你在你评论的那一行上得到错误......
那么“等级”确实不存在,变量的名称是"letter"
可以尝试运行此代码并查看它是否有效。
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter a grade: ");
String letter = in.next();
getNumericGrade(letter);
}
public static void getNumericGrade(String letter)
{
int spacer = 0;
double grade;
String first = letter.substring(0,1);
String second = letter.substring(1,2);
if (first.equalsIgnoreCase("a"))
grade = 4.0;
else if (first.equalsIgnoreCase("b"))
grade = 3.0;
else if (first.equalsIgnoreCase("c"))
grade = 2.0;
else if (first.equalsIgnoreCase("d"))
grade = 1.0;
else if (first.equalsIgnoreCase("f"))
grade = 0;
else grade = 9;
if (second.equalsIgnoreCase("+"))
grade += 0.3;
else if (second.equalsIgnoreCase("-"))
grade -= 0.3;
else
grade += 9;
System.out.println(grade);
}
}
答案 2 :(得分:0)
grade
。您要找的是letter
,
String letter = in.next();
getNumericGrade(letter);
但是,这不会解决您的问题,因为getNumericGrade
方法要求您传递一个double。我认为(基于方法名称),你真的希望getNumericGrade
接受一个String,并返回一个double。我会尝试更多地参考你的教科书或教程,因为你还没有掌握方法我还没有想到。
特别是,void
表示方法不返回值。 void
可以替换为任何类型,例如int
或String
甚至SomeClass
。在同一行中,括号内是指定方法将接受的参数的位置。它们可以是任何类型,您可以将它们命名为任何类型,它们不必与传递给方法调用的变量名称相对应。所以,如果我有一个方法getAverageGrade
接受一个String
参数(比如一个学生的名字)并返回一个double
,这是该学生当前的平均成绩,那么方法签名,或定义看起来像这样
public double getAverageGrade(String studentName)
希望这会让你开始走正确的道路。
答案 3 :(得分:0)
我认为你应该这样做
import java.util.Scanner;
public class EP55Out
{
String letter = "";
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter a grade: ");
letter = in.next();
getNumericGrade();
}
public static void getNumericGrade()
{
if(letter.length()<=0)
return;
int spacer = 0;
String first = letter.substring(0,1);
String second = letter.substring(1,2);
if (first.equalsIgnoreCase("a"))
grade = 4.0;
else if (first.equalsIgnoreCase("b"))
grade = 3.0;
else if (first.equalsIgnoreCase("c"))
grade = 2.0;
else if (first.equalsIgnoreCase("d"))
grade = 1.0;
else if (first.equalsIgnoreCase("f"))
grade = 0;
else grade = 9;
if (second.equalsIgnoreCase("+"))
grade += 0.3;
else if (second.equalsIgnoreCase("-"))
grade -= 0.3;
else
grade += 9;
System.out.println(grade);
}
}