[英]Java program won't compile
我正在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
在第12行,嘗試更改
double grade = new double[numberofTests];
至
double[] grade = new double[numberofTests];
所有隨后的錯誤似乎都是編譯器認為grade
是double
精度而不是數組的結果。 例如,提到“解引用”是指索引到數組中; 索引成標量double
不合理的。
編譯器非常有用,它會告訴您錯誤所在的行以及錯誤所在。
第一個錯誤在第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
上的錯誤Testscore.java:39: cannot find symbol symbol : class InvalidTestScore
意味着編譯器找不到您的InvalidTestScore類-它在哪里? 您也應該編譯該類。
第46行的錯誤是system.out.println
應該是System.out.println
(System中的大寫S)
第59行的錯誤表明沒有名為average
變量。您可以將其定義為存在的表達式的結果,因此只需更改average = startgrade/grade.length;
double average = startgrade/grade.length;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.