为什么我会收到枚举错误?

时间:2012-02-10 02:00:01

标签: java

我需要在前面加上我不允许在课堂上使用IDE,我必须使用TextPad进行编译和运行。

我收到以下错误:

F:\Java\Lab 3\AveragesLab.java:63: error: class, interface, or enum expected
    public static double averageDbl (double[] arrayDbl)
                  ^
F:\Java\Lab 3\AveragesLab.java:66: error: class, interface, or enum expected
    double average = 0.0;
    ^
F:\Java\Lab 3\AveragesLab.java:68: error: class, interface, or enum expected
    for(int i = 0; i < array.length; i++)
    ^
F:\Java\Lab 3\AveragesLab.java:68: error: class, interface, or enum expected
    for(int i = 0; i < array.length; i++)
                   ^
F:\Java\Lab 3\AveragesLab.java:68: error: class, interface, or enum expected
    for(int i = 0; i < array.length; i++)
                                     ^
F:\Java\Lab 3\AveragesLab.java:71: error: class, interface, or enum expected
    }
    ^
F:\Java\Lab 3\AveragesLab.java:73: error: class, interface, or enum expected
    return average;
    ^
F:\Java\Lab 3\AveragesLab.java:74: error: class, interface, or enum expected
    }
    ^
8 errors

Tool completed with exit code 1

代码

import java.util.Scanner;

public class AveragesLab
{
 public static void main (String[] args)
  {

    // Create a scanner
    Scanner input = new Scanner(System.in);

    // Receive the integer values
    System.out.print ("In this exercise you will be asked to enter a series of 10 numbers, twice.\n The first set will be integers or whole numbers and the second set will be doubles or \n numbers with a decimal. Please press the enter key after each number.\n");
    System.out.print ("Enter 10 integers or whole numbers, for example 75: ");

        // Create the integer array
        int arrayInt [] = new int [10];
        for (int i = 0; i < arrayInt.length; i++)
        {
            arrayInt [i] = input.nextInt();
        }
        int averageInt = average (arrayInt);

        System.out.println ("\nThe average of the intger array is: "+averageInt);
        System.out.println ();
        System.out.println ();


        // Receive the double values
        System.out.println ("Enter 10 double amounts including decimals, for example 75.5: ");

        // Create the double array
        double arrayDbl [] = new double [10];
        for(int i = 0; i < arrayDbl.length; i++)
        {
            arrayDbl [i] = input.nextDouble();
        }
        double average = averageDbl (arrayDbl);
        System.out.printf ("%7.2f\nThe average of the doubles array is: "+average);
        System.out.println ();
        System.out.println ();
    }


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

错误低于此行

    public static double averageDbl (double [] arrayDbl)
    {
    double sum = 0.0;
    double average = 0.0;

    for(int i = 0; i < array.length; i++)
    {
        sum = sum + array [i];
    }
    average = sum / array.length;
    return average;
    }

}

4 个答案:

答案 0 :(得分:1)

}之前有一个结束public static double averageDbl太多了。我看到average有太多了。

由于该类已关闭,编译器需要另一个类/接口。

答案 1 :(得分:0)

array中的averageDbl var不存在。将三次出现变为arrayDbl

答案 2 :(得分:0)

假设您已经发布了所有代码并将其与“错误在此行之下”的注释分成两半,则您的括号不匹配。
因此,“public static double averageDbl(double [] arrayDbl)”在类外定义。

答案 3 :(得分:0)

这样更好:

package homework;

/**
 * AveragesLab description here
 * @author Michael
 * @link
 * @since 2/9/12 9:08 PM
 */

import java.util.Scanner;

public class AveragesLab {
    public static void main(String[] args) {

        // Create a scanner
        Scanner input = new Scanner(System.in);

        // Receive the integer values
        System.out.print("In this exercise you will be asked to enter a series of 10 numbers, twice.\n The first set will be integers or whole numbers and the second set will be doubles or \n numbers with a decimal. Please press the enter key after each number.\n");
        System.out.print("Enter 10 integers or whole numbers, for example 75: ");

        // Create the integer array
        int arrayInt[] = new int[10];
        for (int i = 0; i < arrayInt.length; i++) {
            arrayInt[i] = input.nextInt();
        }
        int averageInt = average(arrayInt);

        System.out.println("\nThe average of the intger array is: " + averageInt);
        System.out.println();
        System.out.println();


        // Receive the double values
        System.out.println("Enter 10 double amounts including decimals, for example 75.5: ");

        // Create the double array
        double arrayDbl[] = new double[10];
        for (int i = 0; i < arrayDbl.length; i++) {
            arrayDbl[i] = input.nextDouble();
        }
        double average = averageDbl(arrayDbl);
        System.out.printf("The average of the doubles array is %7.2f ", average);
        System.out.println();
        System.out.println();
    }


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


    public static double averageDbl(double[] arrayDbl) {
        double sum = 0.0;
        double average = 0.0;

        for (int i = 0; i < arrayDbl.length; i++) {
            sum = sum + arrayDbl[i];
        }
        average = sum / arrayDbl.length;
        return average;
    }

}