0

我正在尝试将生成的数字放入特定范围,但它不断给我这个输出:

200 Generated numbers, 10 per line: 
99 Numbers from 0-20: 1
Numbers from 20-40: 0
Numbers from 40-60: 0
Numbers from 60-80: 0
Numbers from 80-100: 0
18 Numbers from 0-20: 2
Numbers from 20-40: 0
Numbers from 40-60: 0
Numbers from 60-80: 0
Numbers from 80-100: 0
56 Numbers from 0-20: 3

这是我当前的代码:

import java.util.Random;

public class RandomStats2 {

    public static void main(String[] args) {
        Random random = new Random ();
        int[] num = new int [5];
        System.out.println("200 Generated numbers, 10 per line: ");
        int numbersOnThisLine = 0;

        for(int i = 0; i < 200; i++){
            int nextNumber = random.nextInt(101);
            System.out.print(nextNumber + " ");
            numbersOnThisLine++;    

            if (numbersOnThisLine == 10){
                System.out.print("\n");
                numbersOnThisLine = 0;
            }

            if(i <=21) num[0]++;
            else if(i <=41) num[1]++;
            else if(i <=61) num[2]++;
            else if(i <=81) num[3]++;
            else if(i <=100) num[4]++;
            System.out.println("Numbers from 0-20: " + num[0]);
            System.out.println("Numbers from 20-40: " + num[1]);
            System.out.println("Numbers from 40-60: " + num[2]);
            System.out.println("Numbers from 60-80: " + num[3]);
            System.out.println("Numbers from 80-100: " + num[4]);               
        }   
    }
}

我怎样才能解决这个问题?另外,如何在 else if 语句中设置 41-60 之类的范围?

4

5 回答 5

1

你想检查你 nextNumber 所以我想你想要这个:

if(nextNumber <21){ num[0]++;}
else if(nextNumber <41){ num[1]++;}
else if(nextNumber <61){ num[2]++;}
else if(nextNumber <81){ num[3]++;}
else if(nextNumber <101){ num[4]++;}

此外,每当您使用 if-else 时,请使用这些 {}。

其次,在循环结束后打印。

于 2013-11-05T01:24:25.697 回答
0

将调用移到循环System.out.println()之外for将解决重复输出的问题。此外,您的当前ifelse if语句没有检查正确的值。尝试这个:

if (nextNumber <= 20) num[0]++;
else if (nextNumber <= 40) num[1]++;
else if (nextNumber <= 60) num[2]++;
else if (nextNumber <= 80) num[3]++;
else if (nextNumber <= 100) num[4]++;
于 2013-11-05T01:24:30.700 回答
0

您真的应该在 for 循环中完成所有这些工作吗?我不确定您要完成什么,但一个疯狂的建议是将其打印在循环之外。

现在,您正在打印为每个随机整数找到的内容。

于 2013-11-05T01:25:20.517 回答
0

查看您的代码,您似乎希望每 10 次迭代打印一次范围。如果是这样,请将您的打印语句移动到 if 语句中,如下所示。

import java.util.Random;

public class RandomStats2 {

    public static void main(String[] args) {
        Random random = new Random ();
        int[] num = new int [5];
        System.out.println("200 Generated numbers, 10 per line: ");
        int numbersOnThisLine = 0;

        for(int i = 0; i < 200; i++){
            int nextNumber = random.nextInt(101);
            System.out.print(nextNumber + " ");
            numbersOnThisLine++;    

            if (numbersOnThisLine == 10){
                System.out.print("\n");
                numbersOnThisLine = 0;
                System.out.println("Numbers from 0-20: " + num[0]); 
                System.out.println("Numbers from 20-40: " + num[1]);
                System.out.println("Numbers from 40-60: " + num[2]);
                System.out.println("Numbers from 60-80: " + num[3]);
                System.out.println("Numbers from 80-100: " + num[4]);
            }

            if(i <=21) num[0]++;
            else if(i <=41) num[1]++;
            else if(i <=61) num[2]++;
            else if(i <=81) num[3]++;
            else if(i <=100) num[4]++;            
        }   
    }
}
于 2013-11-05T01:26:56.707 回答
0

您的统计信息需要像这样脱离 for 循环:

import java.util.Random;

公共类 RandomStats2 {

public static void main(String[] args) {
    Random random = new Random ();
    int[] num = new int [5];
    System.out.println("200 Generated numbers, 10 per line: ");
    int numbersOnThisLine = 0;

    for(int i = 0; i < 200; i++){
        int nextNumber = random.nextInt(101);
        System.out.print(nextNumber + " ");
        numbersOnThisLine++;    

        if (numbersOnThisLine == 10){
            System.out.print("\n");
            numbersOnThisLine = 0;
        }

        if(i <=21) num[0]++;
        else if(i <=41) num[1]++;
        else if(i <=61) num[2]++;
        else if(i <=81) num[3]++;
        else if(i <=100) num[4]++;              
    }   
    System.out.println("Numbers from 0-20: " + num[0]);
    System.out.println("Numbers from 20-40: " + num[1]);
    System.out.println("Numbers from 40-60: " + num[2]);
    System.out.println("Numbers from 60-80: " + num[3]);
    System.out.println("Numbers from 80-100: " + num[4]); 
}

}

对于 if 语句,我认为您打算使用nextNumber而不是i,因为i您是循环计数器

于 2013-11-05T01:27:03.560 回答