【发布时间】:2019-10-21 17:50:18
【问题描述】:
我无法同时返回用户名和传递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";
}
【问题讨论】:
-
与你的问题无关,这不是你compare strings in java的方式。
-
一个方法不能返回多个值(除非该方法被多次调用)。
-
这在 java 中是不可能的。而是创建一个对象(比如说
Credentials),它包含两个值并返回所述对象的实例。 -
另外,我想不出任何逻辑上的理由,你会想要返回用户名并传递......你正在将它们传递给方法......因此你已经在调用者中有它们?跨度>
-
Object 的子类或数组[]