Java 如何让ArrayList shuffle生成多个不完全相同的

Java 如何让ArrayList shuffle生成多个不完全相同的,java,Java,嗨,Java新手,这是我的第二个任务。我已经使用数组列表为彩票代码编写了一个方法,我有两个问题: 对于请求的每一行,都会生成相同的无序列表 这些行是向下打印的,没有交叉 publicstaticvoidirishlotking(){ 扫描仪输入=新扫描仪(System.in); 双倍成本=0; System.out.println(“您希望最多10行彩票中有多少行?”); int linesam=input.nextInt(); ArrayList编号=新的ArrayList(); //定

嗨,Java新手,这是我的第二个任务。我已经使用数组列表为彩票代码编写了一个方法,我有两个问题:

  • 对于请求的每一行,都会生成相同的无序列表
  • 这些行是向下打印的,没有交叉

publicstaticvoidirishlotking(){
扫描仪输入=新扫描仪(System.in);
双倍成本=0;
System.out.println(“您希望最多10行彩票中有多少行?”);
int linesam=input.nextInt();
ArrayList编号=新的ArrayList();
//定义ArrayList以保存整数对象
for(int-lottonos=0;lottonos<45;lottonos++){
数字。添加(lottonos+1);
}
收藏。洗牌(数字);
系统输出打印(“您的彩票号码是:”);

对于(int lncounter=1;lncounter对于第一个问题:每次要显示新行时,都要重新排列列表:

for (int lncounter = 1; lncounter <= linesam; lncounter++) {  
    Collections.shuffle(numbers);
    for (int j = 0; j < 6; j++) {
        System.out.println( numbers.get(j) + " ");
    }
}
for (int lncounter = 1; lncounter <= linesam; lncounter++) {  
    Collections.shuffle(numbers);
    for (int j = 0; j < 6; j++) {
        System.out.print(numbers.get(j) + " ");
    }
    System.out.println(); // new line before next line
}

for(int lncounter=1;lncounter 2)可能是
System.out.print
而不是
System.out.println
它会给你相同的列表,因为你洗牌了一次并且重复打印相同的列表。每次打印时你都需要再次洗牌。谢谢你,这非常有效,我非常理解外部循环
for (int lncounter = 1; lncounter <= linesam; lncounter++) {  
    Collections.shuffle(numbers);
    for (int j = 0; j < 6; j++) {
        System.out.print(numbers.get(j) + " ");
    }
    System.out.println(); // new line before next line
}
for (int lncounter = 0; lncounter < linesam; lncounter++) {  
    Collections.shuffle(numbers);
    for (int j = 0; j < 6; j++) {
        System.out.print(numbers.get(j) + " ");
    }
    System.out.println(); // new line before next line
}