2013-05-20 66 views
0

嗨Java完全新手,這是我的第二個任務。我已經寫了使用數組列表彩票碼的方法,我有兩個問題:如何獲得ArrayList shuffle生成多個不完全相同的

  • 相同的混洗表產生用於請求每一行。
  • 這些行不打印。

public static void irishLottery() { 
    Scanner input = new Scanner(System.in); 
    double cost = 0; 
    System.out.println("How many lines of lottery up to 10 would you like?"); 
    int linesam = input.nextInt(); 

    ArrayList<Integer> numbers = new ArrayList<Integer>(); 
    //define ArrayList to hold Integer objects 

    for (int lottonos = 0; lottonos < 45; lottonos++) { 
     numbers.add(lottonos + 1); 
    } 
    Collections.shuffle(numbers); 

    System.out.print("Your lottery numbers are: "); 

    for (int lncounter = 1; lncounter <= linesam; lncounter++) { 
     for (int j = 0; j < 6; j++) { 
      System.out.println(numbers.get(j) + " "); 

     } 
    } 
} 
+3

2)也許'System.out.print',而不是'System.out.println' – boburShox

+0

這是給你同樣的名單,因爲你洗牌一次,是印刷同樣的重複。每次打印時需要再次洗牌。 – Ric

回答

2

對於第一個問題:要顯示一個新行每次改組名單:

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

對於第二個問題,不要使用println()一樣,但print(),因爲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 
} 

請注意,標準成語oping在Java中包括從0開始:

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 
} 
+0

謝謝,這很好,我理解循環的外部學到很多很多讚賞 – Ceara

相關問題