好的,所以目标是每行/行生成 6 个随机数。具有 x 行数(由用户通过 UserInput 设置)。每行必须有唯一的数字(非重复数字)。我很确定这些数字是唯一的,但是我似乎无法让它有多行,而且我一生都无法弄清楚是什么部分阻止了多行。
package rtg;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class Array {
public static void main(String[] args) {
String name;
int noTickets;
int[] numbers = new int[6];
Set<Integer> randomNumbers = new HashSet<>();
Random rand = new Random();
int ticketCount = 1;
System.out.println("Please input your name");
name = UserInput.readString();
System.out.println("Please input the number of tickets you want");
noTickets = UserInput.readInt();
System.out.println("___________________________________________\n___________________________________________");
System.out.println("___________________________________________\n___________________________________________");
System.out.println("Name: " +name+ "\nNumber of Tickets: " +noTickets+ "\nNumbers: ");
for (ticketCount = 1; ticketCount <= noTickets; ++ticketCount){
while (randomNumbers.size() < 6) {
randomNumbers.add(rand.nextInt(50) + 1);
}
int i = 0;
for (Integer n : randomNumbers) {
numbers[i++] = n;
}
System.out.print( Arrays.toString(numbers) + "\n");
}
}
}
编辑非常感谢大家,我终于到了那里,结果我把数组放在了错误的位置(它在 for 循环之外,所以只制作了一组随机数)现在修复它。下一个挑战;有一个比较程序来扫描 90 多组 6 个唯一数字,并比较它们中的任何一个是否匹配不同的集合(每行/集合 >.<)