有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从方法返回值

我需要帮助来找出我遗漏了什么。我已经看过了代码,我不明白为什么会出现这个错误。我是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) 个答案

  1. # 1 楼答案

    超出范围

    {
       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?