当我运行这个程序时,我得到了这些错误:
Testscore.java:26: class expected
grade = double.parseDouble(strInput);
^
Testscore.java:26: ';' expected
grade = double.parseDouble(strInput);
^
Testscore.java:26: not a statement
grade = double.parseDouble(strInput);
^
Testscore.java:26: ';' expected
grade = double.parseDouble(strInput);
^
4 errors
我有double.parseDouble(strInput);
正确吗?
import javax.swing.*;
import java.lang.IllegalArgumentException;
public class Testscore
{
public static void main(String[] args)
{
int numberofTests = 0;
double grade = new double[numberofTests];
double startgrade = 0;
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];
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();
}
catch (InvalidTestScore e)
{
e.printlnStackTrace();
}
}
}
for (int index = 0; index < grade.length; index++ )
{
startgrade += grade[index];
}
average = startgrade/grade.length;
System.out.print("The average is: " + average);
}
}
答案 0 :(得分:6)
Double
,资本D
请注意double
和Double
之间的区别。 “小”双是原始类型。另一个是班级 - java.lang.Double
。您可以在类上调用类似parseDouble(..)
的方法,而不是原语。 “big”double也称为“包装类”,因为它将原始类型包装到类中。