gpt4 book ai didi

java - 代码输出 NULL 而不是值

转载 作者:行者123 更新时间:2023-12-01 18:51:36 24 4
gpt4 key购买 nike

代码假设打印用户输入的名称,但它只是打印空值而不是打印实际名称。我正在使用数组来存储名称值。

import javax.swing.JOptionPane;

class PrintName {
public static void main(String[] args) {

int nameCount = Integer.parseInt(JOptionPane.showInputDialog(null, " how many NAMES you want to enter?"));
String [] userFirstName = new String [nameCount];
String [] userLastName = new String [nameCount];
for (int i=0; i<nameCount; i++) {
fullName(userFirstName[i], userLastName[i]);
}

for (int i=0; i<nameCount; i++) {
JOptionPane.showMessageDialog(null, userFirstName[i] + "\n" + userLastName[i]);
}

public static void fullName (String firstName, String nLastName) {
firstName = JOptionPane.showInputDialog(null, "What's your first name?");
nLastName = JOptionPane.showInputDialog(null, "What's your last name?");
}

}

最佳答案

我认为发生这种情况是因为您更改了值而不是值指针,为此更改了 fullName 的公司:

public static void fullName (String[] firstName, String[] nLastName, int i) {
firstName[i] = JOptionPane.showInputDialog("What's your first name?");
nLastName[i] = JOptionPane.showInputDialog("What's your last name?");
}

现在您可以通过以下方式调用该函数:

fullName(userFirstName, userLastName, i);

我希望这对你有用:)

关于java - 代码输出 NULL 而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15715112/

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