2016-10-14 163 views
0

我需要帮助来找出我失踪的东西。我已经去了代码,我不明白为什么我得到这个错误。我对java很陌生,在学习语法时倾向于发生错误,请帮助我解决方法的返回问题。方法返回值

import javax.swing.JOptionPane; 

public class TestScores { 


public static void main(String[] args) 
{ 
    int[] testScores; 
    testScores = getTestScores(); 
    getAverage(testScores); 
    double average; 


    average = getAverage(); //*method getAverage in class TestScores cannot be 
          *applied to given types; 
          *required: int[] 
          *found: no arguments 
          *reason: actual and formal argument lists differ      
          */in length 

    displayScores(average); 

    System.out.println(testScores[0]); 
    System.out.println(testScores[1]); 
    System.out.println(testScores[2]); 
} 
     public static int[] getTestScores() 
      { 
    int[] scores = new int[3]; 


     for (int testValues = 0; testValues < scores.length; testValues++) 
      { 
       String input; 

       input = 
         JOptionPane.showInputDialog("What is the test score for number " + (testValues + 1) + " ? "); 


       while (input.isEmpty()) 
       { 
        input = JOptionPane.showInputDialog("Nothing was enterd for test " + (testValues + 1) + " please enter a value."); 
       } 

         scores[testValues] = Integer.parseInt(input); 


       while (scores[testValues] <= 0 || scores[testValues] > 100) 
        { 
         input = 
           JOptionPane.showInputDialog("The number " + scores[testValues] + " for test " + (testValues + 1) + " is invalid." 
                  + " Please enter a number in the range of 1 though 100"); 

         scores[testValues] = Integer.parseInt(input); 
        } 
    } 

    return scores; 

} 

     public static int[] getAverage(int[] input) 
     { 
      for (int avg = 0; avg < input.length; avg++) 
      { 
       int total; 
       double result; 

       total = input[avg]; 
       result = total/input.length; 
      } 
      return result; //cannot find symbol 
          *symbol: variable result 
          */location: class TestScores 
     } 

     public static void displayScores(double average) 
     { 
      JOptionPane.showMessageDialog(null, "The average is " + average + "."); 

      if (average <= 100 || average >= 90) 
      { 
       JOptionPane.showMessageDialog(null, "The letter grade is A.");     
      } 
      else if (average < 90 || average <= 80) 
      { 

       JOptionPane.showMessageDialog(null, "The letter grade is B."); 
      } 
      else if (average < 80 || average <= 70) 
      { 

       JOptionPane.showMessageDialog(null, "The letter grade is C."); 
      } 
      else if (average < 70 || average <= 60) 
      { 

       JOptionPane.showMessageDialog(null, "The letter grade is D."); 
      } 
     } 
    } 
+0

什么错误,你目前得到些什么? – hurnhu

+1

按照您需要将数组作为参数传递给getAverage方法的签名。更改此行 average = getAverage(); to average = getAverage(testScores); – Satya

+0

我做了一个错误即时通过查看值旁边的代码得到的评论 – Daimeon

回答

2

超出范围

{ 
    double result; 
    … 
} 
return result; //cannot find symbol 

您定义的花括号内的变量。这些大括号定义了一个代码块。该块有scope。在大括号之后,任何定义的变量都消失了(所有引用都被删除,成为garbage collection的候选)。当你说return result没有result

移动该定义外部大括号。

double result; 
{ 
    result = … 
    … 
} 
return result; //cannot find symbol 

参见:Java, What do curly braces mean by themselves?

+0

刚刚做了,现在我从变量结果中得到一个错误可能没有被初始化 – Daimeon

+0

这是一个错误还是一个警告?我怀疑是一个警告。您可以为变量设置默认值。或者告诉你的IDE忽略该规则。 'double result = 0;' –

+1

@Daimeon:那么你不能正确地创建代码。不要输入大量的代码然后测试它。自由协会代码编写和扔在墙上的垃圾,看看什么棍子不会工作。相反,如果你不能使用像NetBeans或Eclipse这样的现代IDE(它几乎可以立即警告你编译问题),那么你应该尽早并经常**编译你的代码**,最重要的是**直到当前的编译问题得到解决为止,不添加任何新的代码**。否则,你最终会遇到难以修复的错误。 –