我为一个简单的卡片处理程序编写了一个代码,你可以输入正在播放的玩家数量,然后输出两张卡片加上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]);
}
}
}
答案 0 :(得分: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();
}