我正在处理我正在研究的计算机科学课程。我完成了编码但是我的Runner Class遇到了麻烦

时间:2018-03-23 23:15:19

标签: java arrays jgrasp

给定提供的数组,确定存在多少个指定大小的组。 对于阵列[1,1,1,2,2,2,3,3,3,4,5,6,7],有7组至少有一组,3组在 至少2和3组至少3.一组是一系列相同的值。 1 1 1是一组3,但它也是 一组1和2.要计为一组,所有值必须相同。 1 1 1是一组3因为那里 是连续3个。

示例输出: [3,3,3,3,3,9,4,4,4,5,5,5,5,6,7,7,7,8,8,8,8,8,8,8] ,8] 大小1计数== 7

尺寸2计数== 6

尺寸3计数== 5

4号计数== 3

5号计数== 2

大小6计数== 1

我的主要代码:

import static java.lang.System.*;
import java.util.Arrays;
import java.util.Scanner;

        import static java.lang.System.*;
    import java.util.Arrays;
    import java.util.Scanner;

    public class ArrayStats {
      int[] numArray;
      int number;

      public ArrayStats(int[] a) {
        setArray(a);
      }

      public void setArray(int[] a) {
        numArray = a;
      }

      public int getNumGroupsOfSize() {
        int cnt = 0;
        for (int i = 0; i < numArray.length - 1; i++) {
        if (numArray[i] == numArray[i + 1])
        cnt++;
        for (int j = 0; j <= 9; j++) {
          if (cnt == i)
            number = cnt;
          else if (cnt == 1)
            number = 1;
        }
      }
      return number;
    }

    public String toString() {
      return "size count" + " == " + getNumGroupsOfSize() + Arrays.toString(numArray);
    }
    }

我的跑步者代码:

public class ArrayStatsRunner
{
    public static void main(String args[])
    {
       int[] one = {3, 3, 3, 3, 3, 9, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8};
       ArrayStats test = new ArrayStats(one);
       System.out.println(test.toString());
       System.out.println("size 1 count == "+test.getNumGroupsOfSize(1));
       System.out.println("size 2 count == "+test.getNumGroupsOfSize(2));
       System.out.println("size 3 count == "+test.getNumGroupsOfSize(3));
       System.out.println("size 4 count == "+test.getNumGroupsOfSize(4));
       System.out.println("size 5 count == "+test.getNumGroupsOfSize(5));
       System.out.println("size 6 count == "+test.getNumGroupsOfSize(6));

        }
    }

1 个答案:

答案 0 :(得分:0)

此方法存在以下几个问题:

  public int getNumGroupsOfSize() {
int cnt = 0;
for (int x = 0; x < numArray.length - 1; x++) { 
if (numArray[x] == numArray[x + 1]); 
cnt++;
for (int y = 2; y <= 9; y++) { 
  if (cnt == y)
    number = cnt;
  else if (cnt == 1)
    number = 1;
}
  }
  return number;
}

这里只是一些问题: 1.让我们看看第二行:

for (int x = 0; x < numArray.length - 1; x++) 

x < numArray.length - 1会导致问题,因为您不会检查数组的最后一个索引。 旁注:使用字母i(索引)而不是x或y是自定义的。如果你在for循环中做循环,那么自定义就是:

     for (int i = 0; i < numArray.length - 1; i++)
        {
        for (int j = 0; j < numArray.length - 1; j++)
        {
        //some line of code
        }}
  1. 这行代码if (numArray[x] == numArray[x + 1]);将无效,因为您将;放在行的末尾。即使numArray[x] == numArray[x + 1]为真,它也不会cnt++;
  2. 请检查并学习此代码:

     public class Main {
    
    public static void main(String [] args)
    {
        int[] nums = {3, 3, 3, 3, 3, 9, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8};
        System.out.println(+getGroupSize(nums,9)); //prints 1
    
        System.out.println("size two== "+groupCount(nums,2)); //prints 6
        System.out.println("size three== "+groupCount(nums,3));//prints 5
    
    
        int[] nums2={1,1,1,2,2,2,3,3,3,4,5,6,7};
        System.out.println(+getGroupSize(nums2,1)); //prints 3
        System.out.println("size two== "+groupCount(nums2,2)); //prints 3
        System.out.println("size two== "+groupCount(nums2,3)); //prints 3
        System.out.println("size two== "+groupCount(nums2,5)); //prints 0
    
    }
    
    
    
    public static int getGroupSize(int[] array, int specificNumber  ) {
        /*This method prints the number of times a specific number exist in a array.
        example: if the input of specificNumber is 3. in this array:
        int[] nums = {3, 3, 3, 3, 3, 9, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8};
        the method will return 5.
        if the number is 9 is method will return 1. if number is 7 the method will return 3
        */
        int groupCount = 0; //counts the number of times a specific number exist in a array
        for (int i = 0; i <= array.length - 1; i++) { //this for loop will check every number in  array
            if (array[i] ==specificNumber) {
                groupCount++;// if the current number of the array is the specificNumber, then the count will do plus one
            }
        }
        return groupCount; //return the count
    }
    
    
    public static int groupCount(int[] array, int groupSize)
    {
        int groupCount=0;
        int currentGroup=array[0]; //initialize the current group to be the first group of the array
    
        if(getGroupSize(array, array[currentGroup])>=groupSize)
        { //check the size of the first group
            groupCount++;
        }
    
        for (int i = 0; i <= array.length - 1; i++) {
            if (array[i] !=currentGroup) { //checks if the current number is equal to the current group value
                if(getGroupSize(array, array[i])>=groupSize)
                {
                    groupCount++;
                }
                currentGroup=array[i]; // restart the currentGroup to be the current valume of the array
    
            }
        } //end of for loop
        return groupCount;
    }
    
    private static void print(int [] array) {
        for (int i = 1; i < 10; i++) {
            System.out.println("size " +i+" group:" +groupCount(array, i));
        }
    }
    

    }

相关问题