【问题标题】:JOptionPane - Drop Down: How to subtract an option that somebody has chosen?JOptionPane - 下拉:如何减去某人选择的选项?
【发布时间】:2014-03-15 16:55:09
【问题描述】:

我是一个初学者和java,正在制作一个ludo板。 我正在制作一个下拉菜单JOptionPane,其中玩家#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... 

谁能帮助我或告诉我在哪里可以找到正确的答案?

【问题讨论】:

  • 您的示例缺少括号 ("}")。

标签: java swing drop-down-menu joptionpane


【解决方案1】:

您可以使用可用颜色列表。然后在用户选择一种颜色后,将其从可用列表中删除。例如:

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));

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    相关资源
    最近更新 更多