简体   繁体   English

JOptionPane中的多名称输入

[英]Multiple Name input in JOptionPane

I'm trying to create a for loop that allows the user to enter multiple player names (up to 6) and store each player name in a variable (playerone, playertwo etc;) and in between each prompt for a name the user should be asked if they would like to enter another player's name. 我正在尝试创建一个for循环,允许用户输入多个玩家名称(最多6个),并将每个玩家名称存储在变量中(playerone,playertwo等;),并且在每个提示输入名称的提示之间,用户应询问他们是否要输入其他玩家的名字。 I have written the for loop but can't figure how to assign each input to a string variable. 我已经编写了for循环,但无法弄清楚如何将每个输入分配给一个字符串变量。 If the user selects No in the dialog, it should break the for loop. 如果用户在对话框中选择“否”,则应中断for循环。

if (text.equals(play)) {
    {
        {
            String Playerone = JOptionPane.showInputDialog(null, "Enter Player 1:");
            textArea.append("Player 1: " + Playerone);

            for (i = 2; i <= 6; i++) {
                int n2 = JOptionPane.showConfirmDialog(null, "Enter another player?", "",
                        JOptionPane.YES_NO_OPTION);

                if (n2 == JOptionPane.YES_OPTION) {
                    JOptionPane.showInputDialog(null, "Enter Player " + i + "\n");
                    textArea.append("Player " + i + ":" + "\n");

                }
                if (n2 == JOptionPane.NO_OPTION) {

                }
            }

        }
    }
}
if (n2 == JOptionPane.YES_OPTION) {
    JOptionPane.showInputDialog(null, "Enter Player " + i + "\n");
    textArea.append("Player " + i + ":" + "\n");

}else 
if (n2 == JOptionPane.NO_OPTION) {
    break;
}

replace your ifs in the for loop with this and it should work. 以此替换for循环中的ifs,它应该可以工作。 you can't have dynamic variable names. 您不能使用动态变量名。 use any type of list or array for that 为此使用任何类型的列表或数组

Simply use break operator to out of loop. 只需使用break运算符即可退出循环。

Use a ArrayList to store all names in a variable . 使用ArrayList将所有名称存储在变量中。

Java ArrayList class uses a dynamic array for storing the elements. Java ArrayList类使用动态数组来存储元素。

ArrayList<String> userNames=new ArrayList<String>

if (n2 == JOptionPane.YES_OPTION) {
    String playerName = JOptionPane.showInputDialog(null,"Enter Player " + i + ":");
    userNames.add(playerName);
}else if(n2 == JOptionPane.NO_OPTION){ 
    break;
 } 

Iterate ArrayList in java 在Java中迭代ArrayList

int i=1; 
for(String name:userNames){
    System.out.println("Player"+i+" : "+name);
    i+=1;
}

JOptionPane.showInputDialog returns a string when the user presses ok, otherwise it will return null. 用户按下ok时, JOptionPane.showInputDialog返回一个字符串,否则它将返回null。

"I have written the for loop but can't figure how to assign each input to a string variable" “我已经编写了for循环,但无法弄清楚如何将每个输入分配给字符串变量”

String Playerone = JOptionPane.showInputDialog(null,"Enter Player 1:" );

I can see you know how to assign each value to a string variable, don't be unsure of your capabilities, unless you copied that code somewhere without understanding it. 我可以看到,您知道如何将每个值分配给字符串变量,请不要不确定您的功能,除非您在不了解代码的情况下将其复制到某个地方。 Since we have multiple players, I'm using a string array to store the input. 由于我们有多个玩家,所以我使用字符串数组来存储输入。

To answer your second question, to break out of your loop, simply type break; 要回答第二个问题,要打破循环,只需键入break;

In conclusion, we should have something like this: 总之,我们应该有以下内容:

String Playerone = JOptionPane.showInputDialog(null,"Enter Player 1:" );
textArea.append("Player 1: " + Playerone);

String[] playernames = new String[5];

for(i=2; i<=6;i++) {

    int n2 = JOptionPane.showConfirmDialog(null,"Enter another player?" ,"",JOptionPane.YES_NO_OPTION);

    if(n2 == JOptionPane.YES_OPTION) {
         playernames[i-2] = JOptionPane.showInputDialog(null, "Enter Player "+ i + "\n");
         textArea.append("Player " + i + ":" + "\n" );


    }
    else if(n2 == JOptionPane.NO_OPTION) {
           break;

    }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM