【问题标题】:I am having trouble with a computer science program that I am working on. I finished the coding but I am having trouble with my Runner Class我正在研究的计算机科学程序有问题。我完成了编码,但我的 Runner Class 遇到了问题
【发布时间】:2018-09-02 17:03:37
【问题描述】:

给定一个提供的数组,确定存在多少个指定大小的组。 对于数组 [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 个 1。

示例输出: [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] 尺寸 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));

        }
    }

【问题讨论】:

  • 您的运行程序代码无法编译。该代码不可能产生该输出,因为您实际上并未在任何地方提供“大小”。请阅读:How to create a Minimal, Complete, and Verifiable example
  • 听起来更像是您希望我们为您完成代码,而不是自己尝试完整的解决方案。
  • 我尝试了很多次,都无法正常运行。
  • 您如何发布实际编译的尝试代码?代码实际尝试计算特定 size 的尝试?
  • 您传递给 ArrayStats 构造函数的参数“one”是什么?

标签: java arrays jgrasp


【解决方案1】:

这种方法有几个问题:

  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 &lt; numArray.length - 1 会导致问题,因为您不会检查数组的最后一个索引。 旁注:习惯使用字母 i(索引)而不是 x 或 y。如果您在 for 循环中执行 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++; 也不会这样做。

请检查并学习此代码:

 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));
    }
}

}

【讨论】:

  • 我已经更新了我的主代码。但是跑步者我有一个编译错误,上面写着“ArrayStatsRunner.java:14: error: method getNumGroupsOfSize in class ArrayStats cannot be applied to given types;”而且我不明白我做错了什么来得到这个错误。
  • 这意味着 getNumGroupsOfSize 需要一个参数,而您没有传递一个参数。看:stackoverflow.com/questions/13169606/…
猜你喜欢
  • 2013-09-02
  • 2023-03-27
  • 2016-06-27
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
相关资源
最近更新 更多