我正在jgrasp下制作这个程序而且我收到了一个错误。我检查了我的程序的拼写和语法,似乎是正确的。请帮助我 - 是否有一些我错过的导致我所有错误的事情?
import javax.swing.*;
public class Testscore
{
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 = Double.parseDouble(strInput);
if (grade[index] < 0 || grade[index] > 100 )
{
try
{
throw new InvalidTestScore();
x=2;
}
catch (InvalidTestScore e)
{
e.printlnStackTrace();
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];
}
average = startgrade/grade.length;
System.out.print("The average is: " + average);
}
}
以下是我遇到的错误。
Testscore.java:12: incompatible types
found : double[]
required: double
double grade = new double[numberofTests];
^
Testscore.java:25: incompatible types
found : double[]
required: double
grade = new double[(int) numberofTests];
^
Testscore.java:30: double cannot be dereferenced
for (int index = 0; index < grade.length; index++)
^
Testscore.java:35: array required, but double found
if (grade[index] < 0 || grade[index] > 100 )
^
Testscore.java:35: array required, but double found
if (grade[index] < 0 || grade[index] > 100 )
^
Testscore.java:39: cannot find symbol
symbol : class InvalidTestScore
location: class Testscore
throw new InvalidTestScore();
^
Testscore.java:43: cannot find symbol
symbol : class InvalidTestScore
location: class Testscore
catch (InvalidTestScore e)
^
Testscore.java:46: package system does not exist
system.out.println ("Choose a test score between 0
and 100");
^
Testscore.java:53: double cannot be dereferenced
for (int index = 0; index < grade.length; index++ )
^
Testscore.java:56: array required, but double found
startgrade += grade[index];
^
Testscore.java:59: cannot find symbol
symbol : variable average
location: class Testscore
average = startgrade/grade.length;
^
Testscore.java:59: double cannot be dereferenced
average = startgrade/grade.length;
^
Testscore.java:61: cannot find symbol
symbol : variable average
location: class Testscore
System.out.print("The average is: " + average);
^
13 errors
答案 0 :(得分:6)
在第12行,尝试更改
double grade = new double[numberofTests];
到
double[] grade = new double[numberofTests];
所有后续错误似乎都是编译器认为grade
是double
而不是它们的数组的结果。例如,提到“解除引用”是指索引到数组中;索引标量double
是不合理的。
答案 1 :(得分:2)
编译器非常有用,它会告诉你哪个行有错误,以及错误是什么。
第一个错误是在第12行,并告诉您已经为不是数组的内容分配了数组引用,您应该更改
double grade = new double[numberofTests];
到
double grade[] = new double[numberofTests];
第25行的下一个是类似的。
接下来的3个错误是由于之前的两个错误,因为您将grade
声明为double,但稍后尝试将其用作数组。
第39行的错误Testscore.java:39: cannot find symbol
symbol : class InvalidTestScore
意味着编译器无法找到您的InvalidTestScore类 - 它在哪里?你也应该编译那个类。
第46行的错误是system.out.println
应为System.out.println
(系统中的资本S)
第59行的错误表示没有名为average
的变量您可以将其定义为您所在表达式的结果,因此只需将average = startgrade/grade.length;
更改为double average = startgrade/grade.length;