gpt4 book ai didi

java - 变量未从输入对话框获取字符串

转载 作者:行者123 更新时间:2023-12-01 04:12:46 41 4
gpt4 key购买 nike

我正在尝试使用方法 charEdit 获取对话框的输入,该方法应该更改输入,但变量最后仍然等于 null。

public String showInputDialog(String stringy)
{
String input = JOptionPane.showInputDialog(this,stringy);
if(input == null || input.isEmpty())
{
input = showInputDialog(stringy);
}
return input;
}

public void charEdit(Checkbox chara,String account,String password){
chara.setLabel(showInputDialog("Character Name?"));
account=(showInputDialog("Account Name?"));
password=(showInputDialog("Account Password?"));
chara.setEnabled(true);
}

public void menuItemSelected(MenuItem menuObj){
if (menuObj==help){
messageBox("Edit character info and then click the login button");
}
else if (menuObj==charOneEdit){
charEdit(characterOne,charAArray[0],charPArray[0]);
}
}

为什么变量characterOne不保留它的值?

最佳答案

你的问题是字符串是不可变的。所以调用这个方法:

public void charEdit(Checkbox chara,String account,String password){
chara.setLabel(showInputDialog("Character Name?"));
account=(showInputDialog("Account Name?"));
password=(showInputDialog("Account Password?"));
chara.setEnabled(true);
}

不会更改传递给该方法的参数引用的字符串。

换句话说,调用此:

String acct = "";
String password = ""; // passwords should not be held in Strings by the way
charEdit(something, acct, password);

不会更改帐户或密码。

一种解决方案是使用类字段并更改它们。另一种方法是传入对包含帐户和密码字段的对象的引用,然后更改其字段的状态。

关于java - 变量未从输入对话框获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19754070/

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