0

我为一个简单的发牌程序编写了一个代码,您可以在其中输入正在玩的玩家数量,它会为每个人输出两张牌加上 5(河牌、翻牌等)

我想知道如何使 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]);

    }        

}

}

4

1 回答 1

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();
}
于 2013-10-05T21:43:00.020 回答