gpt4 book ai didi

java - 如何从java方法返回多个字符串值

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

我无法同时返回用户名和通行证 String同时值。

    public static String executeLogin(String username, String pass) {
int totalAttempts = 5;

if (totalAttempts != 0) {
if (username == "Javaprogram" && (pass == "Legend1@")) {
System.out.println("Correct");
} else {
System.out.println("Incorrect Login");
totalAttempts--;
}
if (totalAttempts == 0) {
System.out.println("Maximum number of attempts exceeded");
}

}
return "username,pass";
}

最佳答案

注意:您应该将“总尝试次数”变量放在函数范围之外!

您可以返回字符串列表:

static int totalAttempts = 5;

public static String[] executeLogin(String username, String pass) {

if (totalAttempts != 0) {
if (username == "Javaprogram" && (pass == "Legend1@")) {
System.out.println("Correct");
} else {
System.out.println("Incorrect Login");
totalAttempts--;
}
if (totalAttempts == 0) {
System.out.println("Maximum number of attempts exceeded");
}

}

return new String[]{username, pass};
}

关于java - 如何从java方法返回多个字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58491689/

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