2014-09-19 62 views
-1

我想創建一個數字猜測程序,用戶輸入一個數字,程序會告訴您輸入是否太高,太低或正確。程序選擇0-100之間的隨機數。問題是,當我開始測試程序時,當我選擇1時,程序告訴我猜測過高。但是這是錯誤的,因爲程序只能做整數。如果用戶輸入的號碼不等於隨機數,則1必須太低。 有人可以看看我的代碼,看看是否有什麼奇怪的。數字猜測程序錯誤?

int guess; 
    int guess2; 
    int guess3; 
    int guess4; 
    int guess5; 
    int random = (int)Math.random() * 100; 
    Scanner input = new Scanner(System.in); 

    System.out.println("Try to guess a number between 0-100. You have five chances."); 
    guess = input.nextInt(); 
    if(guess >= 100 || guess <= 0){ 
     System.out.println("Error, that is not a number between 0-100."); 
    } else if(guess > random){ 
     System.out.println("The guess is too high."); 
    } else if(guess < random){ 
     System.out.println("The guess is too low."); 
    } else if(guess == random){ 
     System.out.println("YOU WIN"); 
    } else{ 
    } 

    System.out.println("Second guess."); 
    guess2 = input.nextInt(); 
    if(guess2 >= 100 || guess2 <= 0){ 
     System.out.println("Error, that is not a number between 0-100."); 
    } else if(guess2 > random){ 
     System.out.println("The guess is too high."); 
    } else if(guess2 < random){ 
     System.out.println("The guess is too low."); 
    } else if(guess2 == random){ 
     System.out.println("YOU WIN"); 
    } else{ 
    } 

    System.out.println("Third guess."); 
    guess3 = input.nextInt(); 
    if(guess3 >= 100 || guess3 <= 0){ 
     System.out.println("Error, that is not a number between 0-100."); 
    } else if(guess3 > random){ 
     System.out.println("The guess is too high."); 
    } else if(guess3 < random){ 
     System.out.println("The guess is too low."); 
    } else if(guess3 == random){ 
     System.out.println("YOU WIN"); 
    } else{ 
    } 

    System.out.println("Fourth guess."); 
    guess4 = input.nextInt(); 
    if(guess4 >= 100 || guess4 <= 0){ 
     System.out.println("Error, that is not a number between 0-100."); 
    } else if(guess4 > random){ 
     System.out.println("The guess is too high."); 
    } else if(guess4 < random){ 
     System.out.println("The guess is too low."); 
    } else if(guess4 == random){ 
     System.out.println("YOU WIN"); 
    } else{ 
    } 

    System.out.println("Last guess."); 
    guess5 = input.nextInt(); 
    if(guess5 >= 100 || guess5 <= 0){ 
     System.out.println("Error, that is not a number between 0-100."); 
    } else if(guess5 > random){ 
     System.out.println("The guess is too high."); 
    } else if(guess5 < random){ 
     System.out.println("The guess is too low."); 
    } else if(guess5 == random){ 
     System.out.println("YOU WIN"); 
    } else{ 
    } 

回答

2

它看起來不錯,但你需要改變這一行投:

int random = (int)Math.random() * 100; 

到:

int random = (int)(Math.random() * 100); 

目前您的隨機數總是0.爲什麼?因爲您正在將Math.random()轉換爲int類型,而在Java中則意味着小數點後的所有內容都會丟失。 (因此,0.85將變成0. 0 * 100 = 0)。通過在Math.random()乘以100之後添加我們施加的括號。

另外,請注意,您的隨機數可能爲0,但永遠不會100. Math.random()返回一個大於或等於0且小於1的數字。當您將其轉換爲int時,99.9999將變爲99.

如果您希望它真的是0-100 ,你會想要:

int random = (int)(Math.random() * 101); 

另外,你可能希望遊戲在玩家獲勝後終止。因此,改變這種:

System.out.println("YOU WIN"); 

這樣:

System.out.println("YOU WIN"); 
return; //end 

最後,你的空else子句永遠不會被調用。你有條件超出範圍,大於隨機,小於隨機,等於隨機。您可以安全地刪除該死的代碼。

+0

所以「return」在if語句中結束程序? – Nocturne 2014-09-19 04:33:08

+0

返回標記方法的結束。在這種情況下,你在程序的主要方法main()中,所以它會結束程序。 – nostromo 2014-09-19 04:33:46

+0

@nostromo你好嗎?只是友好的點你有沒有看到這條線?如果(guess2> = 100 || guess2 <= 0){它在0和100之間嗎? – 2014-09-19 04:36:01

0

變化int random = (int)Math.random() * 100;int random = (int)(Math.random() * 100);

+1

添加一些解釋到您的文章,這可能有助於他人迅速理解它。 – 2014-09-19 04:46:53

1

有2個主要問題與您的代碼:

首先

您未能投隨機變量馬上所以隨機值的值始終爲0。

合適的款式是:

int random = (int)(Math.random() * 100); 

你如果結構可怕:

例如:

if(guess >= 100 || guess <= 0){ 
  1. 你說猜測是且等於100
  2. 你說猜測是少且等於0
  3. 您使用||操作數,而不是&&檢查數量在0範圍100

試試這個:

if (guess <= 100 && guess >= 0) { 
     if (guess > random) { 
      System.out.println("The guess is too high."); 
     } else if (guess < random) { 
      System.out.println("The guess is too low."); 
     } else if (guess == random) { 
      System.out.println("YOU WIN"); 
     } 
    } else { 
     System.out.println("Error, that is not a number between 0-100."); 
    } 

說明:如果居興數量爲100和0之間,做以下。如果它不告訴用戶他或她不在該範圍內。

+0

所以「if」語句應該讓它在開始時再次定義,然後在聲明結尾處出現錯誤?我有點困惑,爲什麼 if(guess <= 100 && guess> = 0)是需要的,因爲程序員已經知道這一點。 – Nocturne 2014-09-19 04:25:33

+0

雅我改變了我的代碼,你知道Java中的循環概念嗎? – 2014-09-19 04:27:09

+0

好吧,我學到了一段時間和for循環,但老師從來沒有真正說過如何組織格式的任何事情。 – Nocturne 2014-09-19 04:27:52

0

你可以這樣做你的代碼。輕鬆你可以使用for循環

int guess; 
    int random = (int) (Math.random() * 100); 
    Scanner input = new Scanner(System.in); 

    System.out.println("Try to guess a number between 0-100. You have five chances."); 
    for (int i = 0; i < 5; i++) { 
     System.out.println("Chance " + (i + 1)); 
     guess = input.nextInt(); 
     if (guess >= 100 || guess <= 0) { 
      System.out.println("Error, that is not a number between 0-100."); 
     } else if (guess > random) { 
      System.out.println("The guess is too high."); 
     } else if (guess < random) { 
      System.out.println("The guess is too low."); 
     } else if (guess == random) { 
      System.out.println("YOU WIN"); 
      break; 
     } 
    } 
    System.out.println(random); 
+0

當你贏了,打破循環,否則你會重複所有的方式通過猜測... – MadProgrammer 2014-09-19 05:01:24

+0

謝謝你的建議 – 2014-09-19 06:24:24