2011-12-07 68 views
-4

我正在做一個Java程序(使用一個類內的類),並且我被困在了我的遊戲菜單中的一個點上。遊戲在「情況2」結束後,我希望程序返回菜單而不是退出。我已經刪除了我的DieGame中的任何退出語句,其他類顯然沒有任何退出語句。如果您需要更多的說明以幫助請隨時提問!如何在遊戲結束後讓程序繼續菜單

import java.io.IOException; 
import java.util.Scanner; 

public class Game1 { 
    public static void main(String[] args) throws IOException { 
    Scanner in = new Scanner (System.in); 
    DieGame game = new DieGame(); 
    Date2 date = new Date2(); 
    Time time = new Time(); 
    int gameMenuChoice; 

    do { 
     System.out.println("\nEnter player information: Press 1 "); 
     System.out.println("Play the game: Press 2 "); 
     System.out.println("Update top player information: Press 3 "); 
     System.out.println("Display top player: Press 4 "); 
     System.out.println("Quit: Press 5 "); 
     System.out.print ("Enter choice: "); 

     gameMenuChoice = in.nextInt(); 

     switch (gameMenuChoice) { 
     case 1: game.playerName(); 
      date.setYear(); 
      date.setMonths(); 
      date.setDay(); 
      time.setHours(); 
      time.setMinutes(); 
      time.setSeconds(); 
      time.timeOfDayCode(); 
      break; 

     case 2: 
      game.playGame(); 
      break; 

     case 3: 
     case 4: 
      game.displayName(); 
      date.displayDate(); 
      time.displayTime(); 
      break; 

     case 5: 
      System.out.print("\nThank you for playing..Goodbye"); 
      System.exit(0); 
      break; 

     default:System.err.print("PLEASE ENTER A NUMBER BETWEEN 1-5\n"); 
     } 
    } while (gameMenuChoice != 5); 
    } 
} 

import java.util.*; 

public class DieGame { 

    private String playername; 
    private int pot; 

    public DieGame() { 
     playername = new String(); 
    } 

    public String playerName() { 

     Scanner in = new Scanner (System.in); 
     System.out.println("\nEnter your name: "); 
     playername = in.nextLine(); 

     return playername;} 




    public void playGame() { 

    Scanner input = new Scanner (System.in); 
    Random randomNumbers = new Random(); 


    int pot = 100; 
    int bet = 1;  
    int die1, die2; 
    int sum; 

    while(bet != 0) { 
     System.out.print("\nCurrent pot is $" + pot + " enter your bet amount (0 to quit):$"); 
     bet = input.nextInt(); 

     if(bet == 0){ 
      System.out.print("\nYou end the game with $" + pot); 
      } 

     else { 
      while (bet > pot || bet <0){ 
       if (bet > pot){ 
        System.err.print("Invalid wager. Please enter new bet:$"); 
        bet = input.nextInt(); } 

        else if (bet < 0){ 
         System.err.print("Invalid wager. Please enter new bet:$"); 
         bet = input.nextInt();} 
      } 


      die1 = randomNumbers.nextInt(6) + 1; // first die 
      die2 = randomNumbers.nextInt(6) + 1; // second die 

      sum = die1 +die2; //sum of die values 

      System.out.printf("You rolled %d and %d " , die1, die2); 

      if(die1 == die2){ 
       pot = (bet*2) + pot; 
       System.out.println("\nYou win double!!"); 
      } 

      else if(sum <= 5 || sum == 10 || sum == 12){ 
       System.out.println("\nYou stay even!"); 
      } 

      else if(sum == 7 || sum == 11){ 
       pot = pot + bet; 
       System.out.println("\nYou win!"); 
      } 

      else if(sum == 6 || sum == 8 || sum == 9){ 
       pot = pot - bet; 
       System.out.println("\nYou lose!"); 
      } 

      if (pot == 0){ 
       System.out.print("\nYou end the game with $0"); 

      } 

     } 
    } 
} 
    public void displayName() { 
     System.out.println("\nTop player is: " + playername +" with a score of:$" + pot); 
     } 

} // end class DieGame1 
+0

呃...當你這樣做時,不要調用'System.exit(0)',並且寫更多的代碼讓你回到菜單。 –

+0

我不知道你是否注意到我說過「我已經從DieGame中刪除了任何退出聲明,而其他班級顯然沒有任何退出聲明。」 – PrgmRNoob

+1

這裏你沒有問題。繼續編寫代碼並返回一個具體問題。 –

回答

0

不指定什麼失敗了,但我的猜測是,右後

gameMenuChoice = in.nextInt(); 

你應該添加

in.nextLine(); 

否則,當你」我會再次進入循環,剩餘的\n會被掃描儀讀取而不是您的新號碼。新號碼是掃描儀,但在下一個插槽中,接着是另一個\n