2013-10-05 105 views
0

我寫了一個撲克程序,它將撲克牌交給輸入的玩家數量,然後處理房卡。我想知道如何在最後詢問玩家是否想再玩一遍並將程序放入循環中。因此,如果他們輸入「是」,那麼程序將重新啓動,但如果他們輸入「否」,則程序結束。我應該怎麼做?詢問用戶是否想再次玩

import java.io.*; 

public class Dealer { 

    public static void main(String[] args) throws IOException { 

     BufferedReader in; 
     int x; 
     String playerx; 

     in = new BufferedReader(new InputStreamReader(System.in)); 
     System.out 
       .println("Welcome to the Casino! My name is Zack and I'm going to be dealing your table. How many players are playing?"); 
     playerx = in.readLine(); // user input for menu selection 
     x = Integer.valueOf(playerx).intValue(); 

     while (x >= 1 && x <= 24) { 

      // create a deck of 52 cards and suit and rank sets 
      String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" }; 
      String[] rank = { "2", "3", "4", "5", "6", "7", "8", "9", "10", 
        "Jack", "Queen", "King", "Ace" }; 

      // initialize variables 
      int suits = suit.length; 
      int ranks = rank.length; 
      int n = suits * ranks; 
      // counter (5 house cards and 2 cards per player entered) 
      int m = 5 + (x * 2); 

      // initialize deck 
      String[] deck = new String[n]; 
      for (int i = 0; i < ranks; i++) { 
       for (int j = 0; j < suits; j++) { 
        deck[suits * i + j] = rank[i] + " of " + suit[j]; 

       } 
      } 

      // create random 5 cards 
      for (int i = 0; i < m; i++) { 
       int r = i + (int) (Math.random() * (n - i)); 
       String t = deck[r]; 
       deck[r] = deck[i]; 
       deck[i] = t; 
      } 

      // print results 
      for (int i = 0; i < m; i++) { 
       System.out.println(deck[i]); 
      } 
     } 
    } 
} 

回答

1

一石二鳥:給予運行該程序的人在任何手牌發出之前退出的能力,以防他們不想參加比賽。你已經有了這個結構。

while(1) { 
    System.out.println("Welcome to ... How many players are playing (1-24) or enter 0 to exit?"); 
    x = Integer.valueOf(playerx).intValue(); 
    if(x == 0 || x >= 24) { 
     break; 
    } 
    // rest of your logic remains..... 
} 
0

我假設你以前沒有編程經驗嗎?我建議你閱讀太陽文檔發現here。在你考慮增加一個選項,再次播放,瞭解變量方法對象構造。 Youtube教程也可以幫助你,如果你是初學者,但不要只依賴他們。