我正在尝试将生成的数字放入特定范围,但它会不断给我输出:
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的范围?
答案 0 :(得分: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,都要使用这些{}。
其次,在循环结束后打印。
答案 1 :(得分:0)
将调用移至System.out.println()
循环之外的for
将解决重复输出的问题。此外,您当前的if
和else 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]++;
答案 2 :(得分:0)
你真的应该在for循环中做所有这些吗?我不确定你想要完成什么,但一个疯狂的建议就是在循环之外将它打印出来。
现在您正在打印您为每个随机整数找到的内容。
答案 3 :(得分:0)
查看代码,看起来您希望每10次迭代打印出范围。如果是这样,请在if语句中移动print语句,如下所示。
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]++;
}
}
}
答案 4 :(得分:0)
您的统计信息需要像for循环一样:
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]);
}
}
同样对于if语句,我认为你打算使用nextNumber
而不是i
,因为i
是你的循环计数器