方法java中的未识别错​​误

时间:2012-11-04 15:19:31

标签: java arrays class methods

我写了一个由两个班组成的小程序。我试图从第二类调用方法,但我得到一个错误。

错误是:

  

线程“main”中的异常java.lang.Error:未解析的编译   问题:方法getSum(int [])未定义类型   UserInteraction方法getAverage(int [])未定义类型   UserInteraction.main上的UserInteraction(UserInteraction.java:66)

这是第一堂课的代码:

import java.util.Scanner;

public class UserInteraction {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int choice = 0;
        String[] subjects = new String[10];
        int grades[] = new int[10];
        double sum = 0.0;
        Grades method = new Grades();

    do
    {
        System.out.println("1. Enter a course name and a grade");
        System.out.println("2. Display all grades");
        System.out.println("3. Calculate the average grade");
        System.out.println("4. Exit program");

        choice = scan.nextInt();

        if ( choice == 1 ) 
        {
            Scanner scansubjects = new Scanner(System.in);
            Scanner scangrades = new Scanner(System.in);

            System.out.println("Enter 10 subjects and their corresponding grades:");
            System.out.println();

            int i = 0;

            for( i = 0; i < 10; i++ )
            {
                System.out.println("Subject:");

                String temp = scansubjects.nextLine();
                subjects[i] = temp.toLowerCase();

                System.out.println("Grade:");

                grades[i] = scangrades.nextInt();

                if( i == ( subjects.length - 1 ) )
                {
                    System.out.println("Thank you!");
                    System.out.println();
                }
            }
        }


        if ( choice == 2 )
        {
            System.out.println("Subjects" + "\tGrades");
            System.out.println("---------------------");

            for(int p = 0; p < subjects.length; p++)
            {

                System.out.println(subjects[p] + "\t" + "\t" + grades[p]);
            }
        }

        if ( choice == 3 )
        {   
              System.out.println("Total of grades: " + getSum(grades));
              System.out.println("Count of grades: " + grades.length);
              System.out.println("Average of grades: " + getAverage(grades));
              System.out.println();
        }


    } while ( choice != 4);

    } 

 }

第二节课是:

public class Grades {

    public static double getAverage(int[] array)
    {
        int sum = 0;
        for(int i : array) sum += i;
        return ( ( double ) sum )/array.length;
}

    public static double getSum(int[] array)
    {
        int sum = 0;
        for (int i : array) 
        {
         sum += i;
        }
        return sum;
    }

    }

3 个答案:

答案 0 :(得分:1)

你不需要这个,因为它只有静态方法:

Grades method = new Grades(); // <-- delete this line

要调用静态方法,必须在其前面加上类名:

System.out.println("Total of grades: " + Grades.getSum(grades));
System.out.println("Count of grades: " + grades.length);
System.out.println("Average of grades: " + Grades.getAverage(grades));

答案 1 :(得分:0)

您的代码无法编译。不要尝试执行非编译代码。在执行代码之前修复所有编译错误。它们在Eclipse IDE的“Problems”视图中可见。

答案 2 :(得分:0)

您需要致电Grades.getSumGrades.getAverage以获得正确的结果。另外,请不要忘记在Grade类的开头使用import Grades;命令导入UserInteraction类。