2013-09-27 58 views
0

我想創建一個抽獎程序,它創建四個隨機數字,每個數字介於0和9之間(包括0和9)。這個程序要求用戶猜測四個數字和每個用戶猜測到4張隨機數進行比較,並顯示在贏得消息:java中的隨機數字無法正常工作

No matches 0 points 
Any one digit matching 5 points 
Any two digits matching 100 points 
Any three digits matching 2,000 points 
All four digits matching 1,000,000 points 

我的程序運行,但它也有一些邏輯錯誤。例如,輸出應該是:

Random numbers:2 3 3 4 
Guess numbers: 1 2 5 7-->1 matching digit 
Guess numbers: 3 5 7 3-->2 matching digits 
Guess numbers: 3 3 3 1-->2 matching digits 
Guess numbers: 3 3 3 3-->2 matching digits 
public class Lottery 
{   
    public static void main(String[] args) { 
     final int LIMIT=10; 
     int totalCount=0; 
     int totalPoint; 
     Random random=new Random(); //creating object of random class  
     Scanner input=new Scanner(System.in);//creating object of scanner class  
     //declaring two arrays 
     int[] guessNumber= new int[4]; 
     int[] randomNumber=new int[4];  

for(int i=0;i<4;i++) 
    {    

    randomNumber[i]=random.nextInt(LIMIT);//returns value between 0 to 9(inclusive)  
    }  

for(int i=0;i<4;i++) 
    { 
    System.out.println("Enter your first guess number from 0 to 9:"); 
    guessNumber[i]=input.nextInt(); 
    } 



for (int i = 0; i < 4; i++) 
    { 
    for (int j = 0; j < 4; j++) 
     { 

     if (randomNumber[i] == guessNumber[j]) 
      { 
     ++totalCount; 
     break; 
     } 
     } 
    }   

    if(totalCount == 1) 
    { 
     totalPoint=5; 
     } 
    else if(totalCount == 2) 
    { 
     totalPoint=100; 
     } 
    else if(totalCount == 3) 
     { 
     totalPoint=2000; 
     } 
    else if(totalCount == 4) 
     { 
     totalPoint=100000; 
     }  
    else 
     { 
     totalPoint=0; 
     } 


//dispalying points 
    System.out.println("You have earned " +totalPoint+ "points!!"); 
      } 
     } 
+5

它是如何「不正常工作? – Saggio

+0

試着正確地格式化你的代碼(大括號,意向),它使得它很容易閱讀。 – leonbloy

+0

有一個在線代碼格式化程序 – 2013-09-27 20:24:45

回答

0

我看來像的問題是,一旦你匹配一個特定的數字,你應該從流通中被移除,使多種猜測相同的價值不算作匹配。一種解決方案是對randomNumber使用ArrayList,並在匹配時將其刪除。如果您將您的// declaring two arrays評論和totalPoint作業替換爲以下內容,那麼它似乎按照您的示例進行了請求。

int guess; 
ArrayList<Integer> randomNumber = new ArrayList<Integer>(); 

for (int i = 0; i < 4; i++) { 
    randomNumber.add(random.nextInt(LIMIT)); 
} 

for (int i = 0; i < 4; i++) { 
    System.out.print("Enter your guess number from 0 to 9: "); 
    guess = input.nextInt(); 
    for (int j = 0; j < randomNumber.size(); ++j) { 
     if (randomNumber.get(j) == guess) { 
     ++totalCount; 
     randomNumber.remove(j); 
     break; 
     } 
    } 
} 
input.close(); 

請注意,我選擇來處理每一個猜測,因爲它是閱讀,而不是將它們存儲在陣列中,以後再處理它們。

0

它看起來像你的整體邏輯是罰款。 如果我理解正確,你只需要一個不同的輸出。 爲你的輸出使用這樣的東西。

String guessNumbers = ""; 
String randomNumbers = ""; 
for (int i = 0; i < 4; i++){ 
    randomNumbers += randomNumber[i]+" "; 
    guessNumbers += guessNumber[i]+" "; 
} 
System.out.println("Random numbers: "+randomNumbers); 
System.out.println("Guess numbers: "+guessNumbers+" --> # of matching digits "+totalCount); 
System.out.println("You have earned " +totalPoint+ " points!!"); 

這就是現在的打印效果:

Random numbers: 2 9 7 4 
Guess numbers: 7 4 5 2 --> # of matching digits 3 
You have earned 2000 points!!