2011-02-02 8 views
-1

이 프로그램을 jgrasp로 만들고 있는데 오류가 있습니다. 내 프로그램의 철자법과 문법을 검사했는데 올바른 것으로 보입니다. 제발 도와주세요 - 내가 놓친 뭔가가 그게 내 모든 오류를 일으키는거야?Java 프로그램이 컴파일되지 않습니다

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

    } 
} 

여기에 오류가 있습니다.

라인 (12)에
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 
+3

당신은 그 프로그램이 아니지만 그냥? 이것은 심각한 것 같습니다 ... 질문에서 "과제"태그를 잊지 않았습니까? –

+1

변경해야합니다. 그렇지 않으면 항상 그렇지 않습니다. – kcoppock

답변

6

, gradedouble 대신 배열이라고 생각 컴파일러의 결과로 나타납니다

double[] grade = new double[numberofTests]; 

에 이후의 모든 오류를

double grade = new double[numberofTests]; 

을 변경해보십시오 그들의. 예를 들어 "역 참조"는 배열로 인덱싱하는 것을 의미합니다. 스칼라 double에 색인을 지정하는 것이 타당하지 않습니다.

+0

좋은 캐치 ..... –

2

컴파일러는 매우 유용합니다. 오류가 발생한 행과 오류가 무엇인지 알려줍니다.

첫 번째 오류는 라인 (12)에 당신은 배열이 아닌 무언가에 배열 참조, 당신은

double grade[] = new double[numberofTests]; 

다음 하나에

double grade = new double[numberofTests]; 

을 변경해야 할당했습니다 있음을 알려줍니다 , 25 행은 비슷합니다. grade을 double로 선언했지만 나중에 배열로 사용하려고 시도했기 때문에 다음 3 개의 오류는 이전 두 오류 때문입니다.

39 번째 줄의 오류 Testscore.java:39: cannot find symbol symbol : class InvalidTestScore 컴파일러가 InvalidTestScore 클래스를 찾을 수 없다는 것을 의미합니다 - 어디입니까? 그 클래스도 컴파일해야합니다.

라인 (46)에 오류가 system.out.printlnSystem.out.println (시스템에서 자본 S)해야한다는 것입니다

라인 59에 오류가 더 average라는 이름의 변수 당신은 당신이 가지고있는 식의 결과로 정의 할 수는 없다는 것을 말한다 거기, 그래서 그냥 변경 average = startgrade/grade.length;double average = startgrade/grade.length;

관련 문제