在java eclipse中使用"for"生成100个随机数并将它们与用户值进行比较时出错
Error using "for" to generate 100 random numbers in java eclipse and compare them to the user value
我是 java 的新手,我遇到的问题是我总是得到 0 个大于用户值的随机数,而且我每次都应该得到不同数量的大于该值的随机数我运行程序
package chapterfinal;
import java.util.Scanner;
public class finalf {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
int greaterOrequal = 0;
int count = 0;
final int finalcount = 100;
System.out.println("Enter a value between 30 and 70: ");
int value = input.nextInt();
while (value > 70 || value < 30) {
System.out.println("Please try to enter a value between 30 and 70: ");
value = input.nextInt();
}
for (count = 0; count < finalcount; count++) {
int random = (int) Math.random() * 100;
if (random >= value)
greaterOrequal++;
}
System.out.println("There are " + greaterOrequal + " random numbers greater than " + value);
} }
“while”代码用于提示用户重新输入 30-70 之间的值,以防用户输入超出该范围的值。
for代码是生成100个随机数,分析有多少大于用户输入的值。
最后只打印有多少随机数大于该值的结果,并打印用户输入的值
我知道是一个非常简单的代码,我找不到它有什么问题!
这是我得到的结果:
Enter a value between 30 and 70:
80
Please try to enter a value between 30 and 70:
50
There are 0 random numbers greater than 50
它应该是这样的:
Enter a value between 30 and 70:
25
Please try to enter a value between 30 and 70:
50
There are 51 random numbers greater than 50
每次我 运行 程序时,我应该得到不同数量的大于用户值的随机数。
希望大家能帮帮我
不胜感激!!
int random = (int) Math.random() * 100;
这与您认为的解析方式不同。
那将:
[A] 生成一个介于 0 和 1 之间的数字。
[B] 将该数字转换为 int,因此,现在它始终为 0。
[C] 然后乘以 100。仍然是 0。
换句话说,解析为 ((int) Math.random()) * 100.
简单的修复方法是 (int)(Math.random() * 100)
,但是 请注意,这不是获取 0 到 99 之间的随机数的正确方法。
鸽巢原理适用:计算机并不神奇;您的程序实际上可以生成 X 个介于 0 和 1 之间的唯一数字。 X 很高,但不是无穷大。 (大约是 2^53)。
如果 X 不能被 100 整除(剧透:不能),那么 0 到 100 之间的数字会比其他数字更常被选中:它不均匀。
您不太可能注意到(影响很小),但这从根本上解释了公式被破坏的原因。
正确的做法如下:
Random r = new Random(); // do this only once.
int x = r.nextInt(100); // x can be 0, 99, or anything in between.
它也是更简洁的代码,因为没有公式,就更难弄乱括号。
我是 java 的新手,我遇到的问题是我总是得到 0 个大于用户值的随机数,而且我每次都应该得到不同数量的大于该值的随机数我运行程序
package chapterfinal;
import java.util.Scanner;
public class finalf {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
int greaterOrequal = 0;
int count = 0;
final int finalcount = 100;
System.out.println("Enter a value between 30 and 70: ");
int value = input.nextInt();
while (value > 70 || value < 30) {
System.out.println("Please try to enter a value between 30 and 70: ");
value = input.nextInt();
}
for (count = 0; count < finalcount; count++) {
int random = (int) Math.random() * 100;
if (random >= value)
greaterOrequal++;
}
System.out.println("There are " + greaterOrequal + " random numbers greater than " + value);
} }
“while”代码用于提示用户重新输入 30-70 之间的值,以防用户输入超出该范围的值。
for代码是生成100个随机数,分析有多少大于用户输入的值。
最后只打印有多少随机数大于该值的结果,并打印用户输入的值
我知道是一个非常简单的代码,我找不到它有什么问题!
这是我得到的结果:
Enter a value between 30 and 70:
80
Please try to enter a value between 30 and 70:
50
There are 0 random numbers greater than 50
它应该是这样的:
Enter a value between 30 and 70:
25
Please try to enter a value between 30 and 70:
50
There are 51 random numbers greater than 50
每次我 运行 程序时,我应该得到不同数量的大于用户值的随机数。
希望大家能帮帮我
不胜感激!!
int random = (int) Math.random() * 100;
这与您认为的解析方式不同。
那将:
[A] 生成一个介于 0 和 1 之间的数字。
[B] 将该数字转换为 int,因此,现在它始终为 0。
[C] 然后乘以 100。仍然是 0。
换句话说,解析为 ((int) Math.random()) * 100.
简单的修复方法是 (int)(Math.random() * 100)
,但是 请注意,这不是获取 0 到 99 之间的随机数的正确方法。
鸽巢原理适用:计算机并不神奇;您的程序实际上可以生成 X 个介于 0 和 1 之间的唯一数字。 X 很高,但不是无穷大。 (大约是 2^53)。
如果 X 不能被 100 整除(剧透:不能),那么 0 到 100 之间的数字会比其他数字更常被选中:它不均匀。
您不太可能注意到(影响很小),但这从根本上解释了公式被破坏的原因。
正确的做法如下:
Random r = new Random(); // do this only once.
int x = r.nextInt(100); // x can be 0, 99, or anything in between.
它也是更简洁的代码,因为没有公式,就更难弄乱括号。