从方法返回值

时间:2016-10-14 02:36:09

标签: java swing

我需要帮助才能找出我所缺少的东西。我已经删除了代码,我不明白为什么我会收到此错误。我是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.");
            }
        }
    }

1 个答案:

答案 0 :(得分: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?

相关问题