如何计算数组中的数字?

时间:2017-04-14 21:10:49

标签: java arrays if-statement while-loop

我认为唯一的问题是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");
    }
  }
}

4 个答案:

答案 0 :(得分:1)

首先,替换

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

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

该方法增加计数数组中用户输入位置处的数字,这样数组就保持数字出现在特定索引中的次数,例如,计数[3]确定3次发生的频率 一旦输入0,while循环中的i!= 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时出现的频率

答案 1 :(得分:1)

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

此代码获取用户输入的数字,并将其计数加1。请注意,这仅适用,因为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&#34;响应&#34;

答案 2 :(得分:0)

尝试替换

 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之间元素的各个频率

答案 3 :(得分:0)

尝试:

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

    }

}