2013-10-05 197 views
0

我寫了一個簡單的卡處理程序的代碼,你可以輸入正在玩的玩家的數量,它會爲他們每個加5(河流,翻牌等等)輸出兩張卡片讓m不能等於1?

我是想知道如何讓m不能等於1,這樣如果用戶輸入一個就會顯示錯誤。

另外,我想知道是否有任何方式,以便它更具有視覺吸引力分隔每個球員卡和5張牌,並給他們的標題騙「玩家1」,「翻牌」 ......

謝謝!

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

    BufferedReader in; 
    int x; 
    String playerx; 

    in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("How many players are playing?"); 
    playerx = in.readLine(); //user input for menu selection 
    x = Integer.valueOf(playerx).intValue(); 

    // 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

檢查,如果是1,如果它是拋出一個錯誤? –

+0

據我所見,'m == 1' IFF'x == - 2'('m = 5+(x * 2)'),但x是代表'playerX'的「int」那?) – avrahamcool

回答

0

你需要考慮如果播放器,然後輸入1會發生什麼。它應該再問一次嗎?

如果需要再次詢問,然後,使用一個循環,不斷再問,直到答案是沒有任何1更多:

x = 1; 
while(x == 1) { 
    System.out.println("How many players are playing?"); 
    playerx = in.readLine(); //user input for menu selection 
    x = Integer.valueOf(playerx).intValue(); 
}