编写方法来测试java中的平均成绩

时间:2016-04-21 01:34:19

标签: java

我正在编写家庭作业的代码而且我被卡住了。

作业是编写一个程序,以获得用户输入的10个等级的平均值。

我相信我得到了大部分内容,但在averageGrades方法中,当我尝试声明average时,它不会让我。

这就是我所拥有的:

public class Averagegrades 
{
    public int min, max;
    double average; 

    public static void main (String [] args)
    {

        double sum = 0;
        Scanner scan = new Scanner(System.in);
        int [] gradesarray = new int[10];

        System.out.println("Please enter the 10 test grades: ");

        for (int i = 0; i < gradesarray.length; i++)
        {            
            gradesarray[i] = scan.nextInt();

            if(gradesarray[i] > 100)
            {
                System.out.println("Please enter a number within 0 - 100: ");
                gradesarray[i] = scan.nextInt();
            }
        }

        System.out.println("These are the grades you entered: " +
            Arrays.toString(gradesarray));
        System.out.println("You have entered " + gradesarray.length + " grades");

        for(double num : gradesarray)
        {
            sum = sum + num;
        }

        System.out.println("This is the sum " + sum);
    }

    public double averageGrades()
    {
        average = sum / (double) gradesarray.length;
    }

2 个答案:

答案 0 :(得分:3)

所以这会让你宣布平均值。问题是方法averageGrades找不到sum或gradearray,因为这两个变量是在main方法中声明的,因此对方法不可见。

https://management.azure.com/subscriptions/subscription-id/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachine/myVM/metrics?api-version=2016-03-30&$filter=%28name.value%20eq%20%27CpuTime%27%20

替代你也可以做...

package test;

import java.util.Arrays;
import java.util.Scanner;

public class Averagegrades 
{
public int min, max;
double average; 
static double sum;
static int[] gradesarray;

public static void main (String [] args)
{
    sum = 0;
    Scanner scan = new Scanner(System.in);
    gradesarray = new int[10];
    System.out.println("Please enter the 10 test grades: ");

    for (int i = 0; i < gradesarray.length; i++)
    {            
        gradesarray[i] = scan.nextInt();
        if(gradesarray[i] > 100)
        {
            System.out.println("Please enter a number within 0 - 100: ");
            gradesarray[i] = scan.nextInt();
        }
    }
    System.out.println("These are the grades you entered: "+
            Arrays.toString(gradesarray));
    System.out.println("You have entered "+gradesarray.length+" grades");

    for(double num : gradesarray)
    {
        sum = sum + num;
    }
    System.out.println("This is the sum "+sum);
}

public double averageGrades()
{
    average = sum / (double) gradesarray.length;
    return average ;
}

然后在调用方法时......

public double averageGrades(int sum, int[] gradesarray)
{
  average = sum / (double) gradesarray.length; 
  return average ;
}

通过这样做,你不必在主方法之外移动sum和gradesarray声明。

希望这有帮助!

答案 1 :(得分:0)

sum变量的范围仅限于main方法,但是您在averageGrade方法中使用sum变量,该方法位于main方法之外。

要解决此问题,请声明sum变量,使其范围涵盖整个类。声明 min max average 变量旁边的sum变量。

整数数组对象出现完全相同的问题。我建议你阅读 Variable Scope

有关变量范围的更多信息,请查看:http://www.java2s.com/Tutorial/Java/0020__Language/VariableScope.htm