我是一名初学者,正在制作一个飞行棋板。 我正在制作一个下拉菜单选项 Pane ,其中玩家 #1 选择他想要的颜色(绿色、黄色、蓝色、红色)。 对于 2 号玩家,我希望他能够选择除 1 号玩家选择的颜色之外的所有颜色。
这是到目前为止我的代码(可能很糟糕,但我是第一次编程):
String player1;
String[] numberOfPlayers = {"two", "three", "four"};
String inputNumberOfPlayers = (String) JOptionPane.showInputDialog(null, "How many people are playing?", "Number of Players", JOptionPane.QUESTION_MESSAGE, null, numberOfPlayers, numberOfPlayers[0]);
if(inputNumberOfPlayers.equals("two")) {
String[] player1Color = {"green", "yellow", "blue", "red"};
String inputColorChoices1 = (String) JOptionPane.showInputDialog(null, "What color what you like to be", "Message to Player 1", JOptionPane.QUESTION_MESSAGE, null, player1Color, player1Color[0]);
player1 = inputColorChoices1;
String[] player2Color = {"green", "yellow", "blue", "red"};
//and then I don't know...
有人可以帮助我或告诉我在哪里可以找到正确的答案吗?
最佳答案
您可以使用可用颜色列表。然后,在用户选择颜色后,将其从可用列表中删除。例如:
List<String> availableColors = new ArrayList<String>(Arrays.asList(
"green", "yellow", "blue", "red"));
String player1Color = (String) JOptionPane.showInputDialog(
null, "What color what you like to be",
"Message to Player 1", JOptionPane.QUESTION_MESSAGE, null,
availableColors.toArray(), availableColors.get(0));
availableColors.remove(player1Color);
String player2Color = (String) JOptionPane.showInputDialog(
null, "What color what you like to be",
"Message to Player 2", JOptionPane.QUESTION_MESSAGE, null,
availableColors.toArray(), availableColors.get(0));
关于java - JOptionPane - 下拉菜单 : How to subtract an option that somebody has chosen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22426813/