帮助不知道该怎么做,为什么我收到这个错误。
Score.java:44: cannot find symbol
symbol : class InvalidTestScore
location: class Score
catch (InvalidTestScore e)
^
1 error
// George Beazer
import javax.swing.*;
import javax.swing.JOptionPane;
public class Score
{
public static void main(String[] args)
{
int numberofTests = 0;
double[] grade = new double[numberofTests];
double startgrade = 0;
int x = 1 ;
String strInput;
// Get how many tests are used
strInput = JOptionPane.showInputDialog(null, "How many tests do you have? ");
numberofTests = Integer.parseInt(strInput);
grade = new double[(int) numberofTests];
do
{
for (int index = 0; index < grade.length; index++)
{
strInput = JOptionPane.showInputDialog(null, "Enter Test Score." + (index + 1));
grade[index] = Double.parseDouble(strInput);
if (grade[index] < 0 || grade[index] > 100 )
{
try
{
x=1;
}
catch (InvalidTestScore e)
{
e.printlnStackTrace();
System.out.println ("Choose a test score between 0 and 100");
}
}
else
{
x=2;
}
{
System.out.println ("Choose a test score between 0 and 100");
}
}
}
while (x==1);
for (int index = 0; index < grade.length; index++ )
{
startgrade += grade[index];
}
double average = startgrade/grade.length;
System.out.println("The average is: " + average);
}
}
答案 0 :(得分:3)
它告诉您无法找到班级InvalidTestScore
。你有import
编辑吗?
答案 1 :(得分:0)
您所在的软件包中是否定义了InvalidTestScore?如果不是,则需要为其添加导入。
答案 2 :(得分:0)
这也假设InvalidTestScore扩展了Exception或Throwable。你做到了吗?
我写得更像这样。看看它,看看你是否认为它更简单。请注意,Score类不仅仅是main方法的容器。 Java是一种面向对象的语言。尝试开始考虑如何将问题分解为对象:
package score;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
public class Score
{
private static final NumberFormat DEFAULT_FORMAT = new DecimalFormat("#.##");
private double grade;
public Score(double grade) throws InvalidScoreException
{
if ((grade < 0.0) || (grade > 100.0))
throw new InvalidScoreException(grade + " is invalid");
this.grade = grade;
}
public double getGrade() { return this.grade; }
public String toString()
{
return DEFAULT_FORMAT.format(this.grade);
}
public static void main(String[] args)
{
double sumOfScores = 0.0;
List<Score> scores = new ArrayList<Score>();
for (String arg : args)
{
try
{
double score = Double.valueOf(arg);
scores.add(new Score(score));
sumOfScores += score;
}
catch (InvalidScoreException e)
{
e.printStackTrace();
}
}
if (scores.size() > 0)
{
System.out.println("Scores : " + scores);
System.out.println("Sum of scores: " + sumOfScores);
System.out.println("# of scores : " + scores.size());
System.out.println("Average score: " + sumOfScores/scores.size());
}
}
}
class InvalidScoreException extends Exception
{
public InvalidScoreException()
{
super();
}
public InvalidScoreException(String message)
{
super(message);
}
public InvalidScoreException(String message, Throwable cause)
{
super(message, cause);
}
public InvalidScoreException(Throwable cause)
{
super(cause);
}
}
如果我使用以下命令参数运行此代码:
-40 0 1 2 3 4 5 6 7 8 9 10
我得到了这个结果:
C:\JDKs\jdk1.6.0_21\bin\java -Didea.launcher.port=7536 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 102.216\bin" -Dfile.encoding=windows-1252 com.intellij.rt.execution.application.AppMain score.Score -40 0 1 2 3 4 5 6 7 8 9 10
score.InvalidScoreException: -40.0 is invalid
at score.Score.<init>(Score.java:17)
Scores : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
at score.Score.main(Score.java:38)
Sum of scores: 55.0
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
# of scores : 11
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
Average score: 5.0
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
Process finished with exit code 0
答案 3 :(得分:0)
好。所以。你用这段代码试图解决的最难的问题是“如果有人试图输入一个超出界限的等级,会发生什么?”如果有人输入分数,比如说“-15”,那么这显然是一个错误!
我不确定你的作业是否要求你使用“例外”来处理错误。如果没有,那么我不会使用try {} / catch {}来解决这个问题,因为这样就太过分了。但首先让我们来看看你的代码:
if (grade[index] < 0 || grade[index] > 100 )
{
try
{
x=1;
}
catch (InvalidTestScore e)
{
e.printlnStackTrace();
System.out.println ("Choose a test score between 0 and 100");
}
}
else
{
x=2;
}
更具体地说:
try
{
x=1;
}
因此。这个块意味着什么?它说“嘿Java,尝试在这个try {}块中做这些东西。如果它抛出错误,那么跳转到catch {}块。”在这种情况下,try {}块内的东西是“x = 1”。问题变成了,“声明x = 1会抛出异常吗?”在这个程序中,不,它不会。程序将按照它所说的做 - 将值“1”赋给变量“x”。 此处的try / catch没有用处。它没有捕获真正的错误。
当然,真正的错误是用户是否输入了无效的数字。检查可能如下所示:
if (grade[index] < 0 || grade[index] > 100 )
{
System.out.println("Choose a test score between 0 and 100");
break;
}
这就是说如果成绩超出范围,则打印错误消息。 “打破;”意味着“退出循环” - 停止选择成绩。
现在你的实际错误。找不到“InvalidTestScore”。在Java中,如果要使用变量或对象,则必须先定义它。当您说“catch(InvalidTestScore e)”时,您告诉java“如果有错误,那么我们将抛出异常。有许多类型的异常,但我们正在寻找InvalidTestScore。”
嗯,Java不包含InvalidTestScore类型的异常。 Java附带NullPointerExceptions和ArrayIndexOutOfBound异常,但不是InvalidTestScore。 这意味着如果你想使用InvalidTestScore异常,你必须自己写一个!(如何做到超出了这个答案的范围。)
但你还没有写自己的。所以Java不知道在哪里找到它。 T * hus错误“找不到”符号。 *
所以,你的代码还有一些其他的问题 - 不需要在那里的循环等等。我建议你打开一个空白的.java文件,一次开始一行。首先写这么多代码:
strInput = JOptionPane.showInputDialog(null, "How many tests do you have? ");
numberofTests = Integer.parseInt(strInput);
System.out.println(numberofTests);
编译并运行程序。确保这3行符合您的预期。如果没有,那么让那部分完美运作。然后转到下一部分代码:
for (int index = 0; index < grade.length; index++) /* etc */
通过逐步构建程序,并通过尽可能频繁地编译,您将避免编写pagefull代码,只是为了找到难以追踪的令人沮丧的错误。
当然,你应该去拜访你的教授或TA的办公时间!这让TA感到高兴。 :)
祝你好运!