程序中需要的数组辅助

时间:2015-11-26 17:33:25

标签: java arrays

所以我在课堂上为一个作业制作一个程序。我们正在使用数组。基本上,你必须在课堂上有三个属性:玩家数量(我制造了五个),一个数组,其中包含每个玩家的击球数量,以及每个玩家的命中数。

我的目标是提示使用输入五个玩家中每个玩家的击球次数和命中次数。然后计算击球率。然后我检查一下这位球员是否已经成为全明星队。然后我继续为所有玩家的总点击值(这个部分有问题)。最后,我使用排序算法将总命中数设置为最小值。

这是代码。我把客户端放在同一个类中。

    import java.util.Scanner;

    public class Test
    {
      static int numberOfPlayers = 5;
       int [] numberOfHits;
       int [] numberOfAtBats;
       int battingAverage;
       int [] totalHits;

       public Test(int [] numberOfHits, int [] numberOfAtBats)
        {

        this.numberOfAtBats = numberOfAtBats;
        this.numberOfHits = numberOfHits;
        battingAverage = CalcBattingAverageInd(numberOfHits, numberOfAtBats);
        System.out.println("Batting average for this player is " + battingAverage);//print batting average for that player
        CheckForAllStar(battingAverage);//Call check for allstar method
        totalHits = TotalHits(numberOfHits);//Evertime I add a "+" symbol, it responds with an error, so I can't add more to the total

        }
        //Manipulator method
        public int CalcBattingAverageInd(int [] numberOfHits, int [] numberOfAtBats)
        {
        int battingAverage = numberOfAtBats.length/numberOfHits.length;    
        return battingAverage;
        }
        public int [] TotalHits(int [] totalHits)
        {
        totalHits = new int[numberOfHits.length];
        return totalHits;
        }
        public void CheckForAllStar(int battingAverage)
        {
         this.battingAverage = battingAverage;
            if (battingAverage >= 3)
                System.out.println("Your an all-star!");
            else
                System.out.println("Sorry, you didn't make the all-star team");
        }
        public void Sort(int [] totalHits)//Selection Sort method the total Hits
        {
        int temp;
        int max;

        for (int i = 0; i < totalHits.length; i++)
        {
         max = indexOfLargestElement(totalHits, totalHits.length);   

         temp = totalHits[max];
         totalHits[max] = totalHits[totalHits.length - i - 1];
         totalHits[totalHits.length-i-1] = temp;
        }

        }       
      public static int indexOfLargestElement(int [] array, int size)
      {
      int index = 0;
      for(int i = 0; i < size; i++ )
      {
      if (array[i] > array[index])    
          index = i;
      }
      return index;
      }
       public static void main(String args[])
      {
          int check = 0;
          Scanner scan = new Scanner(System.in);
           int  [] inputHits = {};
          int  [] inputAtBats = {};
          while(check < numberOfPlayers)
          {
      System.out.println("There are " + Test.numberOfPlayers + " players");
      for(int i = 0; i < 1; i++)
        {
        System.out.println("Enter the number of at-bat attempts: ");
        inputAtBats[i] = scan.nextInt();
        System.out.println("Now enter the number of Hits: ");
        inputHits[i] = scan.nextInt();
        Test(inputHits, inputAtBats);//Error here: Cannot find symbol
        }
      ++check;
          }
      }
    }

我使用int类型的所有变量和数组,因为我还在学习数组数据类型之间的转换。我在代码中评论我遇到了麻烦。我没有数组制作了相同的程序,运行正常。但由于某种原因,我无法使用数组运行它。任何帮助和纠正都会很棒。谢谢。

1 个答案:

答案 0 :(得分:1)

Test(inputHits, inputAtBats);//Error here: Cannot find symbol

你不能使用这个静态调用(因为你没有像这样的静态方法),但你有一个这样的类,所以为什么不...

Test t = new Test(inputHits, inputAtBats);