为什么当我输入10个数字后运行这个程序时,第一个数字出现了100次? import java.util.Scanner; 公共类Ex7_3{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); System.out.println(“这个程序读取1到100之间的整数并统计每个整数的出现次数”); int[]计数=新的int[101]; System.out.println(“输入介于1和100之间的整数:”); int next=input.nextInt(); while(下一步!=0){ 如果(next>=1&&next

为什么当我输入10个数字后运行这个程序时,第一个数字出现了100次? import java.util.Scanner; 公共类Ex7_3{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); System.out.println(“这个程序读取1到100之间的整数并统计每个整数的出现次数”); int[]计数=新的int[101]; System.out.println(“输入介于1和100之间的整数:”); int next=input.nextInt(); while(下一步!=0){ 如果(next>=1&&next,java,Java,你在结尾的循环对我来说没有多大意义。你可能想要这样的东西 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 coun

你在结尾的循环对我来说没有多大意义。你可能想要这样的东西

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

        }
    }
}
}
扫描仪输入=新扫描仪(System.in);
System.out.println(“这个程序读取1到100之间的整数并统计每个整数的出现次数”);
int[]计数=新的int[101];
System.out.println(“输入介于1和100之间的整数:”);
int next=input.nextInt();
while(下一步!=0){

如果(next>=1&&nexth您是否尝试添加println()行以帮助调试您的家庭作业?您的代码有两个问题。您应该先填充数组,然后将所有10个数字添加到数组中。然后在该循环之后(在其外部)你应该有一个循环来检查从数组的第一个索引开始的每个数字,并将其与数组中的其他9个点进行比较,以查看它在数组中出现的次数。这可以通过一个内部循环来完成。我运行了2 5 6 5 4 3 23 43 2 0它给我2出现1次。2出现1次。5出现1次。2出现1次。2出现1次。5出现s 1次。6出现1次。2出现1次。5出现2次。6出现1次。2出现1次。4出现1次。5出现2次。6出现1次。2出现1次。3出现1次。4出现1次。5出现2次。6出现1次。2出现1次。3出现1次。4出现1次。5出现2。6出现1次。23出现1次mes.2发生1次。3发生1次。4发生1次。5发生2次。6发生1次。23发生1次。我运行了与您提供的相同的输入并获得了正确的输出。
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.");
    }
}