我們在一個項目上逐步開展工作。最後一個里程碑是將它分成以下幾部分:使用四種方法創建一個類:main方法,創建客戶信息的方法(void),創建用戶名和密碼的方法(void)以及最後一個登錄方法類型)。如何使用方法抽象來檢查userInput與登錄詳細信息?
我已經實現的登錄方法,轉移到執行loginDetails
的時候,我遇到了問題。我不明白我如何在無效方法中創建loginDetails
,並且能夠以不同的方法檢查它。我的loginDetail
方法沒有將值返回給main,並且我的變量對於方法而言是本地的。
我怎麼能在一個void方法創建用戶的詳細信息,並能夠創建一個單獨的方法登錄檢查?
這是我的代碼,我想用loginDetails();
中生成的細節作爲檢查的值而不是手動插入的字符串。
public class Customer {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
loginDetails("testtest", "testtest", "11111121111");
System.out.print(logIn("ABC", "212"));
}
public static void customerInformation() {
//Create customerInformation
}
public static void loginDetails(String firstName, String lastName, String number) {
String userName, password;
userName = firstName.charAt(0) + lastName.substring(0, 3);
password = lastName.substring(0, 3) + number.substring(7);
}
public static String logIn(String userName, String password) {
int count = 0;
while (count < 3) {
System.out.print("Input Username");
String inputUserName = input.nextLine();
System.out.print("Input password");
String inputPassword = input.nextLine();
if (inputUserName.equals(userName) && inputPassword.equals(password)) {
return "You are now logged in";
} else {
count++;
if (count < 3)
System.out.println("Wrong Username or password. Try again");
}
} //While
return "Try again in a few hours"; //Third try
} //logIn
}
你要麼使用實例變量或實際返回的東西。 –