我应该怎么做才能执行此代码?我试图创建一个新类并用作驱动程序,但是它不起作用,并且错误消息为cannot find count。我感到困惑。有人可以帮我吗?

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

public class Lab7Part1
{
  Scanner kb = new Scanner(System.in);
  private static int[] a = {7, 8, 8, 3, 4, 9, 8, 7};
  int sum=0;
  int n=0;
  double avg = sum/a.length;
  int last=-1;
  int max=0;
  int min=Integer.MAX_VALUE;
  int indexOfMax=-1;
  public int count()
  {
    for(int n:a){
      System.out.println(n);
      System.out.println(",");
    }//use a for-each statement to compute the number of values
    System.out.println("The number of elements in int[] a is: " + n);
    return n;
  }
  public int sum()
  {
    for(int n=0; n<a.length; n++){
      sum += a[n];
    }
    System.out.println("The sum of elements in int[] a is: " + sum);
    return sum;
  }
  public double average()
  {
    System.out.println("The avg of elements in int[] a is: " + avg);
    return avg;
  }
  public int findLast(int key)
  {
    System.out.println("Enter an integer.");
    int input = kb.nextInt();
    for (int i=0; i<a.length; i++){
      if(a[i]==input){
        key = i;
        System.out.println("The last index of " + input + " is: " + key);
      }
      else
        System.out.println("The last index of " + input + " is: -1");
    }
    return key;
  }

最佳答案

您应该在类中添加一个主方法,该方法将创建类的实例。

public static void main (String [] args)
{
    Lab7Part1 lab = new Lab7Part1();
    int cnt = lab.count();
    ...
}

关于java - 要执行此操作怎么办?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27223966/

10-09 07:42