我正在开发一个程序,使用 Math.random 方法显示 1 到 42 之间的 7 个随机数,同时检查生成的 2 个数字是否相同。到目前为止,我让它工作的方法是创建 7 个不同的变量并检查每个变量以确保它不等于前一个变量,这是极其低效的!请看下面:
import javax.swing.JOptionPane;
public class LottoDrawEdited {
public static void main(String[] args) {
int no1 = 0, no2 = 0, no3 = 0, no4 = 0, no5 = 0, no6 = 0, no7 = 0;
String Numbers = "";
no1 = (int)(Math.random()*42 + 1);
Numbers += no1 + " ";
no2 = (int)(Math.random()*42 + 1);
do{
if(no2 == no1){
no2 = (int)(Math.random()*42 + 1);
}
}while(no2 == no1);
Numbers += no2 + " ";
no3 = (int)(Math.random()*42 + 1);
do{
if(no3 == no1 || no3 == no2){
no3 = (int)(Math.random()*42 + 1);
}
}while(no3 == no1 || no3 == no2);
Numbers += no3 + " ";
no4 = (int)(Math.random()*42 + 1);
do{
if(no4 == no1 || no4 == no2 || no4 == no3){
no4 = (int)(Math.random()*42 + 1);
}
}while(no4 == no1 || no4 == no2 || no4 == no3);
Numbers += no4 + " ";
no5 = (int)(Math.random()*42 + 1);
do{
if(no5 == no1 || no5 == no2 || no5 == no3 || no5 == no4){
no5 = (int)(Math.random()*42 + 1);
}
}while(no5 == no1 || no5 == no2 || no5 == no3 || no5 == no4);
Numbers += no5 + " ";
no6 = (int)(Math.random()*42 + 1);
do{
if(no6 == no1 || no6 == no2 || no6 == no3 || no6 == no4 || no6 == no5){
no6 = (int)(Math.random()*42 + 1);
}
}while(no6 == no1 || no6 == no2 || no6 == no3 || no6 == no4 || no6 == no5);
Numbers += no6 + " ";
no7 = (int)(Math.random()*42 + 1);
do{
if(no7 == no1 || no7 == no2 || no7 == no3 || no7 == no4 || no7 == no5 || no7 == no6){
no7 = (int)(Math.random()*42 + 1);
}
}while(no7 == no1 || no7 == no2 || no7 == no3 || no7 == no4 || no7 == no5 || no7 == no6);
Numbers += no7 + " ";
JOptionPane.showMessageDialog(null,"The Lotto Numbers Are:\n" + Numbers,"Lotto Draw",JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}
正如你所看到的,它看起来很糟糕。在我开始这样做之前,我试图弄清楚我是否可以:
- 只有 1 个变量
- 生成第一个变量
- 将第一个变量附加到 JTextArea
- 清除第一个变量
- 生成第二个变量
- 以某种方式检查它是否与已附加到 JTextArea 上的第一个变量匹配?
这种方式会更有效,并且需要的代码会少得多,但我无法用任何其他更有效的解决方案来解决这个问题。更有效地执行此操作的最佳方法是什么?在使用 JTextArea 时,我上面的逻辑是否有意义?
感谢您的宝贵时间!
最佳答案
您可以将计算出的每个值放入 List
或 Set
中,然后检查它以查看您是否已经使用过,如果使用过,则计算一个新值。
类似的东西......
List<Integer> values = new ArrayList<>(25);
//...
int value = -1;
do {
value = (int)(Math.random()*42 + 1);
} while (values.contains(value));
values.add(value);
textArea.append(Integer.toString(value) + "\n");
...例如...
您可以将数字的生成包装到一个方法中以使其更容易......
public int getNextNumber() {
int value = -1;
do {
value = (int)(Math.random()*42 + 1);
} while (values.contains(value));
values.add(value);
return value;
}
这假设您已经创建了值
列表
和实例字段...
看看Collections Trail了解更多详情
已更新
如果使用 List
太复杂或不允许,您可以使用数组来滚动自己的功能,例如......
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
new Test();
}
private int[] values = new int[6];
private int count = 0;
public Test() {
for (int index = 0; index < values.length; index++) {
System.out.println(getNextNumber());
}
}
public int getNextNumber() {
int value = -1;
if (count < values.length) {
do {
value = (int) (Math.random() * 42 + 1);
} while (contains(value));
values[count] = value;
count++;
}
return value;
}
protected boolean contains(int value) {
Arrays.sort(values);
return Arrays.binarySearch(values, value) >= 0;
}
}
这变得有点复杂,因为您需要防范 ArrayIndexOutOfBoundsException
s
现在,如果 Arrays.sort
和 Arrays.binarySearch
太复杂或不允许,您可以将其更改为...
private boolean contains(int value) {
boolean contains = false;
for (int check : values) {
if (value == check) {
break;
}
}
return contains;
}
这将完成相同的工作...
关于java - 使用 Math.random 显示随机数并验证该值是否已附加到 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205180/