import java.util.Scanner;
public class Ex7_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("This program that reads the integers between 1 and 100 and counts the occurrences of each");
int [] counts = new int [101];
System.out.println("Enter the integers between 1 and 100:");
int next = input.nextInt();
while (next != 0){
if(next >= 1 && next<= 100)
counts [next]++;
next = input.nextInt();
for(int i =0; i< counts.length;i++)
for(int j = 0; j <= 100; j++)
if(counts[next] > 1){
System.out.println(next + "occurs" + j + "times");
}
}
}
}
解决方案应该是这样的吗?
输入1到100之间的整数:2 5 6 5 4 3 23 43 2 0 2发生2次 3发生1次 4发生1次 5次发生2次 6次发生1次 23次发生1次 43次出现
答案 0 :(得分:0)
你的循环对我来说没有多大意义。你可能想要这样的东西
Scanner input = new Scanner(System.in);
System.out.println("This program that reads the integers between 1 and 100 and counts the occurrences of each");
int [] counts = new int [101];
System.out.println("Enter the integers between 1 and 100:");
int next = input.nextInt();
while (next != 0){
if(next >= 1 && next<= 100)
counts [next]++;
next = input.nextInt();
}
// Here's the change
for (int i = 0; i < counts.length; i++) {
if (counts[i] > 0) {
System.out.println(i + " occurs " + counts[i] + " times.");
}
}