【问题标题】:How to count numbers in an array?如何计算数组中的数字?
【发布时间】:2017-04-14 21:10:49
【问题描述】:

我认为我唯一的问题是 displayCounts 方法。我试图让计数数组仅在计数 > 0 时显示计数。计数 > 1 也是如此。所以输入将类似于:2 5 6 5 4 3 4 0。(0 将结束输入)输出将如下所示: - 2 发生 1 次 - 3 次出现 1 次 - 4 出现 2 次 - 5 出现 2 次 - 6 发生 1 次。

public class CountNumbersInArray {
  public static void main(String[] args) {
    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter the integers between 1 and 100: ");

    int[] counts = new int[101];

    int i = 0;
    while (i < counts.length) {
      counts[i] = input.nextInt();
      i++;
    }
    displayCounts(counts);
  }

  public static void displayCounts(int[] counts) {
    int i = 0;
    i++;
    if (counts.length > 0) {
      System.out.println(counts[i] + " occurs " + i + " time");
    } 
    if (counts.length > 1) {
      System.out.println(counts[i] + " occurs " + i + " times");
    }
  }
}

【问题讨论】:

  • 试试这样:在输入的位置增加数组的int,这样你就可以计算一个数字被输入的频率,然后调用一个输出方法,它打印一行,如果索引 i 处的值大于 0。

标签: java arrays if-statement while-loop


【解决方案1】:

首先,替换

while (i < counts.length) {
  counts[i] = input.nextInt();
  i++;
}

i = input.nextInt();
while(i != 0){
  counts[i]++;
  i = input.nextInt();
}

该方法增加用户输入在 counts 数组中的位置的数字,这样数组就会保存数字在特定索引中出现的次数,例如counts[3] 保存 3 出现的频率。
while 循环中的 i != 0 会在输入 0 后立即中断循环并转到下一行的 displayCounts 方法。

基于此,我们现在创建一个新的 displayCounts 方法:

public static void displayCounts(int[] counts){
  for(int i = 0; i < counts.length; i++){
    if(counts[i] > 0){
      System.out.println(i + " occurs " + counts[i] + " times");
    }
  }
}

该方法现在打印一个数字在其计数大于 0 时出现的频率

【讨论】:

    【解决方案2】:
    import java.util.Scanner; // Why not import this class?
    
    public class CountNumbersInArray {
      public static void main(String[] args) {
        Scanner input = new Scanner(System.in).useDelimiter("\\D+");
        System.out.print("Enter the integers between 1 and 100: ");
    
        int[] counts = new int[101]; // So the last index is 100
    
        for(int i=0; i < counts.length; i++){
          int number = input.nextInt();
          if(number == 0) break;
          if(number >= 1 && number <= 100) counts[number]++;
          else System.out.println("Your number is outside the boundaries");
        }
    
        displayCounts(counts);
      }
    
      public static void displayCounts(int[] counts){
        for(int i = 1; i < counts.length; i++) System.out.println("Number "+i+" occurs "+ counts[i]+ (counts[i] != 1 ? "times" : "time"));
      }
    }
    

    这段代码应该可以工作,我所做的是:
    1.

    for(int i=0; i < counts.length; i++){
      int number = input.nextInt();
      if(number >= 1 && number <= 100) counts[number]++;
      else System.out.println("Your number is outside the boundaries");
    }
    

    此代码获取用户输入的数字,并将其计数加一。请注意,这只有效,因为 Integer 的默认值为 0
    2.

      public static void displayCounts(int[] counts){
        for(int i = 1; i < counts.length; i++) 
         System.out.println("Number "+i+" occurs "+ counts[i]+ (counts[i] > 1 ? "times" : "time"));
      }
    

    这段代码写得更好。您的代码部分错误是您有 2 个 if 语句,而不是 1 个 if 和 1 个 else if。这样,当计数大于 1 时,您就有 2 个“响应”

    【讨论】:

    • 这将要求用户提供 101 个号码。当用户输入0 时,它不会停止,如问题所述(“0 将结束输入”)。尽管您通过检查数字范围对其进行了改进,但如果用户输入非整数值,它仍然会崩溃。
    • 好的,刚刚编辑,所以当用户输入字符串时它不会崩溃。
    • 哦,我喜欢使用分隔符:忽略除数字以外的所有内容。以前没见过那个。你得到一个+1。 --- 为了获得更好的结果,您可能希望在 displayCounts 循环内添加一个 if (counts[i] &gt; 0),例如如果用户输入1 1 2 0,则不应打印3 occurs 0 times, 4 occurs 0 times, ..., 100 occurs 0 times
    • 嗯,取决于偏好。不知道操作员想要什么
    • 是的,但是当用户输入两个值时打印 98 行“负”行,看起来不太友好,是吗?对我来说,不打印太多输出以致相关部分滚出屏幕和/或淹没在大量输出中只是常识。
    【解决方案3】:

    试试:

    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.print("Enter the integers between 1 and 100: ");
    
        int[] counts = new int[101];
        Arrays.fill(counts, 0);
    
        int i = 0;
        int inputValue;
        while (i < counts.length) {
            inputValue = input.nextInt();
            counts[inputValue]++;
            i++;
        }
        displayCounts(counts);
    }
    
    public static void displayCounts(int[] counts) {
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] > 0) {
                if (counts[i] == 1) {
                    System.out.println(i + " occurs " + counts[i] + " time");
                }else {
                    System.out.println(i + " occurs " + counts[i] + " times");
                }
            }
    
        }
    
    }
    

    【讨论】:

    • 不应该在 displayCounts 方法中使用 else if 吗?
    • 可以,使用else效率更高
    • 这与效率无关,您的代码会为一个 i 输出 2 个结果
    • 现在检查一下,应该没问题
    • 这段代码有很多东西: Array.fill(counts,0) 是不必要的(整数的默认值为 0)和 if (counts[i] > 0) 不需要完全有道理。
    【解决方案4】:

    尝试替换

     while (i < counts.length) {
      counts[i] = input.nextInt();
      i++;
    }
    

     int in = input.nextInt();
     while (in != 0) {
       if(in > 0 && in <101){
        counts[in]++;
       } else {
         System.out.println("Please enter values between 1-100. Enter 0 to 
      stop");
      }
     in = input.nextInt();
    }
    

    然后遍历整个计数数组以获得 0 到 100 之间元素的各个频率

    【讨论】:

    • 忽略用户输入的值如何解决任何问题?
    • 我的错..现在检查
    • 好的,否决票已删除,但这只是解决了许多代码问题之一。
    • 这仍然会要求用户提供 101 个号码。当用户输入0 时,它不会停止,如问题所述(“0 将结束输入”)。尽管您通过检查数字范围对其进行了改进,但如果用户输入非整数值,它仍然会崩溃。
    • 如果您正确缩进代码会更具可读性。最好将检查反向检查到if (in == 0),这样你1)检查用户输入的值,2)不需要else。为什么用户输入 101 个数字后它会停止?
    猜你喜欢
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    相关资源
    最近更新 更多