简体   繁体   English

Java程序无法编译

[英]Java program won't compile

I'm making this program under jgrasp and I'm getting an error. 我正在jgrasp下制作此程序,但出现错误。 I have checked the spelling and the grammar of my program and it seems to be correct. 我检查了程序的拼写和语法,这似乎是正确的。 Please help me -- is there something I'm missing that's causing all my errors? 请帮助我-我缺少某些东西导致我所有的错误吗?

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);

    }
}

Here are the errors I am getting. 这是我遇到的错误。

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

On line 12, try changing 在第12行,尝试更改

double grade = new double[numberofTests];

to

double[] grade = new double[numberofTests];

All of the subsequent errors appear to be consequences of the compiler thinking that grade is a double instead of an array of them. 所有随后的错误似乎都是编译器认为gradedouble精度而不是数组的结果。 For example, the mention of "dereferencing" refers to indexing into an array; 例如,提到“解引用”是指索引到数组中; it's not reasonable to index into a scalar double . 索引成标量double不合理的。

The compiler is pretty helpful, it tells you what line you have an error on, and what the error is. 编译器非常有用,它会告诉您错误所在的行以及错误所在。

The first error is on line 12 and tells you that you've assigned an array reference to something that's not an array, you should change 第一个错误在第12行,告诉您已将数组引用分配给非数组对象,应更改

 double grade = new double[numberofTests];

to

double grade[] = new double[numberofTests];

The next one, on line 25 is similar. 第25行上的下一个类似。 The next 3 errors are because of the two previous ones since you declared grade as just a double but later on try to use it as an array. 接下来的3个错误是由于前两个错误,因为您将grade声明为double,但稍后尝试将其用作数组。

The error on line 39 Testscore.java:39: cannot find symbol symbol : class InvalidTestScore Means that the compiler cannot find your InvalidTestScore class - where is it ? 第39行Testscore.java:39: cannot find symbol symbol : class InvalidTestScore上的错误Testscore.java:39: cannot find symbol symbol : class InvalidTestScore意味着编译器找不到您的InvalidTestScore类-它在哪里? You should compiler that class as well. 您也应该编译该类。

The error on line 46 is that system.out.println should be System.out.println (capital S in System) 第46行的错误是system.out.println应该是System.out.println (System中的大写S)

The error on line 59 says that there is no variable named average You can define it as the result of the expression you have there, so just change average = startgrade/grade.length; 第59行的错误表明没有名为average变量。您可以将其定义为存在的表达式的结果,因此只需更改average = startgrade/grade.length; to double average = startgrade/grade.length; double average = startgrade/grade.length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM