gpt4 book ai didi

java - 在 Java 中将用户输入存储为字符串

转载 作者:行者123 更新时间:2023-12-02 03:42:44 24 4
gpt4 key购买 nike

我一直在开发我的个人乒乓球游戏,并遇到了一个非常烦人的错误,但我无法修复。如果我运行游戏一切正常。游戏结束后,系统会提示您输入姓名。然后,该名称应存储为字符串。然后它会告诉你你的名字和你的分数。但它并没有发生这种情况,而是不存储用户输入,只是保持空白。有的请帮帮我!!!

@SuppressWarnings("resource")
public void replay() {

JFrame frame = new JFrame("input");
JOptionPane.showInputDialog(frame, "what is your name player 1? ");
String playerName1 = new String();
new Scanner(System.in);

JOptionPane.showConfirmDialog(null,
"Name: " + playerName1 + " Score: " + scorePlayer1 , "Player scores",
JOptionPane.DEFAULT_OPTION);

String playerName2 = new String();
JOptionPane.showInputDialog(frame, "what is your name player 2? ");

JOptionPane.showConfirmDialog(null,
"Name: " + playerName2 + " Score: " + scorePlayer2 , "Player scores",
JOptionPane.DEFAULT_OPTION);

int n = JOptionPane.showConfirmDialog(null,
"Would you like to play again?",
"Pong",
JOptionPane.YES_NO_OPTION);

if (n == 0){
setup();
} else {
System.exit(0);
}
}

最佳答案

您将“playerName”设置为空字符串,然后丢弃玩家的输入:

String playerName2 = new String();
JOptionPane.showInputDialog(frame, "what is your name player 2? ");

相反,将玩家的输入保存到playerName字符串中:

String playerName2 = JOptionPane.showInputDialog(frame, "what is your name player 2? ");

(这是玩家 2 的示例;只需为玩家 1 使用不同的变量(可能是playerName1)执行相同的操作即可)

关于java - 在 Java 中将用户输入存储为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36656738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com