2016-10-03 35 views
0

我們在一個項目上逐步開展工作。最後一個里程碑是將它分成以下幾部分:使用四種方法創建一個類: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 
} 
+1

你要麼使用實例變量或實際返回的東西。 –

回答

0
class Customer { 
    static Scanner input = new Scanner(System.in); 
    static String userName=null; 
    static String password=null; 
    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) { 

     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 
} 
+1

我有一種奇怪的感覺,那就是強制人們使用實例字段並在字段中存儲狀態以用於其他方法,因爲返回類型被限制爲無效。 –

+0

是啊..他可以通過實例字段也 – FallAndLearn

+0

@ArturBiesiadowski做到這一點 - 我相信是這樣,我們沒有努力遠遠不夠使用數組但對於上面的回答是有意義.. – pkv

相關問題