2014-01-18 147 views
-3

如何製作一個循環,詢問用戶是否想購買更多的門票,以及如何讓隨機數字生成器製作唯一的數字。如果你能幫助或告訴我的網站,發現我怎麼能做到這一點,將是巨大的多張門票Java彩票計劃

謝謝

import java.util.Scanner; 
import java.util.Arrays; 
import java.util.Random; 

public class PowerBallm { 
    //declaring the main method 
    public static void main(String[] args) 
    { 
    //prompt user to enter amount of ticket to buy between 1 and 5 
    System.out.print("How many tickets would you like to purchase ? "); 
    Scanner amount = new Scanner (System.in); 
    double ticket_amount; 
    ticket_amount = amount.nextInt(); 
    while (ticket_amount > 5 || ticket_amount <= 0) 
    { 
     System.out.println("You can only purchase up to FIVE tickets, please try again"); 
     while (!amount.hasNextInt()) 
     { 
     amount.next(); 
     } 
     ticket_amount = amount.nextInt(); 
    } 
    //creating an Array of 5 numbers 
    int group1[] = new int[5]; 

    for (int ticketNo = 0; ticketNo < ticket_amount; ticketNo++) 
    { 
     for(int i = 0; i < group1.length; i++) 
     { 
     group1[i] = 1 + (int) (Math.random() * 56); 
     }  

     //sort the elements 
     //Arrays.sort(group1); 

     //group2 number 
     int group2 = 1 + (int) (Math.random()*46); 

     //print the ticket numbers numbers 
     System.out.println("Your group1 ticket numbers are " + Arrays.toString(group1) + " your group2 ticket number is " + group2);  
    } 
    } 
} 
+0

程序打算做什麼? – Christian

+0

創建一個強力球程序,詢問用戶他們想要購買的票數,然後ramdonly爲票證生成5個不同的數字@Christian – user3208537

+0

您確實需要專注於正確地設置代碼的格式。這非常難以閱讀,並且會阻止許多用戶甚至看到它,更不用說實際上在研究它。 – csmckelvey

回答

1

除了檢查票的一個無效的號碼,你的代碼不做任何處理輸入數。您需要將代碼創建並在for循環中打印票證。

+0

我該如何讓它做些什麼? @lincb – user3208537

+0

上面的答案正好說明了我的意思,並且應該在沒有進一步編輯的情況下工作。 – lincb

1

您可以將您生成票證內容的位置代碼包裝到您在另一個循環中顯示這些數字的位置。

for (int ticketNo = 0; ticketNo < ticket_amount; ticketNo++) 
{ 
    for(int i = 0; i < group1.length; i++) 
    { 
     group1[i] = 1 + (int) (Math.random() * 56); 
    }  

    //sort the elements 
    Arrays.sort(group1); 

    //group2 number 
    int group2 = 1 + (int) (Math.random()*46); 

    //print the ticket numbers numbers 
    System.out.println("Your group1 ticket numbers are " + Arrays.toString(group1) 
     + " your group2 ticket number is " + group2); 
} 
+0

我是java新手,你能解釋一下嗎? @PakkuDon – user3208537

+1

@ user3208537:我可以試試。你不知道哪部分? – PakkuDon

+0

nvm非常感謝你@PakkuDon我會告訴你,如果我面臨更多的問題 – user3208537