2016-08-29 33 views
0

我試圖創建一個隨機數生成程序,跟蹤玩家的勝,負,勝率,總獎金。該方案的邏輯是玩家每次獲得3次機會,並且計算機產生一個隨機數字,玩家需要猜測或者應該匹配。程序忽略「如果其他」,並打印一切

我試着用if if & else語句告訴用戶他是否需要在3個允許的猜測中猜出更高的數字或更低的數字。發生的事情是它完全忽略了條件並一次打印所有三次機會並結束遊戲。

在此的任何輸入將高度讚賞。

遊戲類:

import java.util.Scanner; 

public class Game { 

    Player player; 
    LuckyNumberGenerator lng; 

    public Game() { 
     player = new Player(); 
     lng = new LuckyNumberGenerator(); 
    } 

    public void eventLoop() { 
     Scanner scanner = new Scanner(System.in); 
     int choice = 0; 
     boolean exit = false; 
     while (!exit) { 
      System.out.println("Welcome to the Guessing Game"); 
      System.out.println("=============================="); 
      System.out.println("(1) Set Up New Player"); 
      System.out.println("(2) Play One Round"); 
      System.out.println("(3) Player Win Statistics"); 
      System.out.println("(4) Display Game Help"); 
      System.out.println("(5) Exit Game"); 
      System.out.println("Choose an option: "); 

      try { 
       choice = Integer.parseInt(scanner.nextLine()); 
       if (choice < 1 || choice > 5) { 
        System.err.println("Error : Choose an option between 1 and 5"); 
        choice = 0; 
       } 
      } catch (NumberFormatException e) { 
       System.err.println("Error : Choose an option between 1 and 5"); 
       choice = 0; 
      } 

      switch (choice) { 
       case 1: 
        createNewPlayer(scanner); 
        break; 
       case 2: 
        guessNumber(scanner); 
        break; 
       case 3: 
        printStatistics(); 
        break; 
       case 4: 
        printHelp(); 
        break; 
       case 5: 
        exit = true; 
      } 
     } 
     scanner.close(); 
    } 

    public void printHelp() { 
     System.out.println(" "); 
    } 

    public void printStatistics() { 
     try { 
      if (player.getName() == null || player.getName().trim().length() < 1) { 
       System.out.println("Player has not been set up!"); 
      } else { 
       System.out.println("Player statistics are: "); 
       System.out.println("Name: " + player.getName()); 
       System.out.println("Wins: " + player.getGamesWon()); 
       System.out.println("Losses: " + player.getGamesLost()); 
       System.out.println("Amount won so far: " + player.getTotalWinnings()); 
       System.out.println("Winnings percentage : " 
         + (((player.getGamesWon())/(player.getGamesWon() + player.getGamesLost())) * 100)); 
      } 
     } catch (ArithmeticException ae) { 
      System.out.println("wins and loss both are 0: divide by zero exception"); 
     } 
    } 

    public void guessNumber(Scanner scanner) { 
     int compGuess = lng.generate(); 
     int userGuess = 0; 
     int numAttempts = 0; 
     int cnum = lng.generateConsole(); 
     do { 
      try { 
       System.out.println("Guess a number between 1-100: "); 
       userGuess = Integer.parseInt(scanner.nextLine()); 
       if (userGuess < 1 || userGuess > 100) { 
        System.err.println("Error : your Guess must be between 1-100"); 
       } 
      } catch (Exception e) { 
       System.err.println("Error : your Guess must be between 1-100"); 
      } 
     } while (userGuess < 1 && userGuess > 100); 
     do { 
      if (userGuess > compGuess) { 
       System.out.println("Your Guess is: " + userGuess + "and the random number: " + compGuess); 
       System.out.println("Sorry, you need to go LOWER :"); 
      } else if (userGuess < compGuess) { 
       System.out.println("Your Guess is: " + userGuess + "and the random number: " + compGuess); 
       System.out.println("Sorry, you need to go HIGHER :"); 
      } 
      numAttempts++; 

      if (userGuess == compGuess) { 
       System.out.println("Lucky Number was : " + compGuess + "your guess was : " + userGuess); 
       System.out.println("Congratulations you won " + 10 + "$"); 
       player.setGamesWon(1); 
       player.setTotalWinnings(10); 
      } 

      if (userGuess != compGuess && (userGuess <= (compGuess + 5)) && (userGuess >= (compGuess - 5))) { 
       System.out.println("Lucky Number was : " + compGuess + "your FINAL guess was : " + userGuess); 
       System.out.println("Congratulations you won " + cnum + "$"); 
       player.setTotalWinnings(5); 
      } else if (userGuess != compGuess) { 
       System.out.println("Lucky Number was : " + compGuess + "your guess was : " + userGuess); 
       System.out.println("Sorry better Luck Next time"); 
       player.setGamesLost(1); 
       player.setTotalWinnings(-1); 
      } 
     } while (userGuess != compGuess && numAttempts < 3); 
    } 

    public void createNewPlayer(Scanner scanner) { 
     String name = null; 
     do { 
      try { 
       System.out.println("Enter the name of the player: "); 
       name = scanner.nextLine(); 
       if (name.isEmpty()) { 
        System.err.println("Name cannot be empty"); 
       } 
      } catch (Exception e) {} 
     } while (name.isEmpty()); 
     this.player = new Player(name); 
    } 

    public static void main() { 
     Game game = new Game(); 
     game.eventLoop(); 
    } 

} 
+1

我非常懷疑它忽略了'else if',這是一個邏輯上的缺陷;)同樣格式化你的代碼會使它更容易閱讀。 – SomeJavaGuy

+0

感謝您的輸入凱文。這確實是一個邏輯缺陷,但並沒有完全弄清楚我出錯的地方。我是Java新手,這是我的第一個程序。我嘗試了一些可能性,但似乎沒有工作。任何想法都會很棒。抱歉格式不正確。 –

回答

2
do { 
     try { 
      System.out.println("Guess a number between 1-100: "); 
      userGuess = Integer.parseInt(scanner.nextLine()); 
      if (userGuess < 1 || userGuess > 100) { 
       System.err.println("Error : your Guess must be between 1-100"); 
      } 
     } catch (Exception e) { 
      System.err.println("Error : your Guess must be between 1-100"); 
     } 
    } while(userGuess < 1 && userGuess > 100);//incorrect 

//correct condition -> while(userGuess < 1 || userGuess > 100); 

一個數字不能小於1且大於100。

編輯1:在你的第二環以下兩個條件

1) if (userGuess != compGuess && (userGuess <= (compGuess + 5)) 
      && (userGuess >= (compGuess - 5))) 

2)else if (userGuess != compGuess) 
如果玩家猜的次數超過嘗試的次數,

應該只進行評估,因此這兩個如有條件,應在循環外寫。

而且你需要在第一循環的同時保持用戶輸入1-100之間有效,而你的第二個while循環將是它裏面。

最終代碼會是這個樣子。

do { 

     do { 
      try { 
       System.out.println("Guess a number between 1-100: "); 
       userGuess = Integer.parseInt(sc.nextLine()); 
       if (userGuess < 1 || userGuess > 100) { 
        System.err 
          .println("Error : your Guess must be between 1-100"); 
       } 
      } catch (Exception e) { 
       System.err 
         .println("Error : your Guess must be between 1-100"); 
      } 
     } while (userGuess < 1 || userGuess > 100); 

     System.out.println(" " + userGuess); 

     if (userGuess > compGuess) { 
      System.out.println("Your Guess is: " + userGuess 
        + "and the random number: " + compGuess); 
      System.out.println("Sorry, you need to go LOWER :"); 

     } 
     if (userGuess < compGuess) { 
      System.out.println("Your Guess is: " + userGuess 
        + "and the random number: " + compGuess); 
      System.out.println("Sorry, you need to go HIGHER :"); 
      System.out.println("if 1"); 
     } 
     numAttempts++; 

     if (userGuess == compGuess) { 
      System.out.println("Lucky Number was : " + compGuess 
        + "your guess was : " + userGuess); 
      System.out.println("Congratulations you won " + 10 + "$"); 
      // player.setGamesWon(1); 
      // player.setTotalWinnings(10); 

     } 

    } while (userGuess != compGuess & numAttempts < 3); 

    if (userGuess != compGuess && (userGuess <= (compGuess + 5)) 
      || (userGuess >= (compGuess - 5))) { 
     System.out.println("Lucky Number was : " + compGuess 
       + " your FINAL guess was : " + userGuess); 
     // System.out.println("Congratulations you won " + cnum + "$"); 
     // player.setTotalWinnings(5); 

    } else if (userGuess != compGuess) { 
     System.out.println("Lucky Number was : " + compGuess 
       + "your guess was : " + userGuess); 
     System.out.println("Sorry better Luck Next time"); 
     // player.setGamesLost(1); 
     // player.setTotalWinnings(-1); 

    } 

} 
+0

感謝。我完全刪除了條件。問題在於第二個while while循環。 –

+0

修改答案1秒 – Arthas

+0

@ArvindSuryanarayana檢查我的答案,如果它可以幫助你。 – Arthas

0
while(userGuess < 1 && userGuess > 100); 
     do{ 

這包含錯誤。當userGuess在1到100之間時,循環應該運行,你已經排除了它。

應該

while(userGuess >= 1 && userGuess <= 100); 
     do{ 
+0

謝謝。但這只是第一個循環,而且運行良好。這是我遇到的第二個問題。 –

+0

原諒我懶得去嘗試自己,但是即使它有分號,它也能工作嗎? – byxor

+0

啊,沒關係。 「while」與它下面的「do」完全無關。 – byxor

1

嘗試把第一DO-而在第二個DO-而在頂部。

+0

試過,但仍然是同樣的問題。它在第一次猜測後打印出所有內容。 –

+0

你可以發佈輸出嗎?和你的輸入 – Karprog

+0

選擇選項2>輸入4>出現:「你的猜測是4,隨機數是48」>「你需要更高」>「幸運數字是48,你的猜測是4」>「對不起下次更好運「。 –

0

狀況需要改變一個條件。因爲要循環,只有當用戶輸入的東西比1低於或高於100

while(userGuess < 1 && userGuess > 100); ===> while(userGuess < 1 || userGuess > 100); 

同樣,另一需要改變,以OR

// Issue with logic 
if (userGuess != compGuess && (userGuess <= (compGuess + 5)) && 
       (userGuess >= (compGuess - 5))) { 

// Corrected Code 
if (userGuess != compGuess && (userGuess <= (compGuess + 5)) || 
       (userGuess >= (compGuess - 5))) {