2017-12-27 1007 views
-2

我試圖編寫一個程序,讓您選擇,如果你想註冊或登錄,如果選擇作者明智,它會讓你知道,你只能選擇註冊或登錄... 我有一個問題,即程序有時會停留在循環中,這告訴我我輸入了錯誤的輸入,儘管我輸入了「登錄」並且無法退出該循環。 我得到的另外一個問題是登錄代碼: 我添加了user_id,它等於用戶count+1,我想檢查用戶和通過登錄的用戶是否都正確使用for循環的用戶,並檢查用戶輸入是否等於每個用戶的密碼user_id,我只是不知道該怎麼做我想也許給任何用戶的對象與計數,所以我可以檢查用戶一通過一個在我的for循環user_id.usernameuser_id.password.用戶名和密碼登錄java項目

import java.util.Scanner; 

public class users { 
    public String user_name; 
    public int user_id = 1; 
    private String password; 
    public static int count = 1; 
    public static String input; 

    public users(String Ruser, String Rpassword) { 

     this.user_id = count++; 
     this.user_name = Ruser; 
     this.password = Rpassword; 
     count++; 

     System.out.printf("User %s has been crated \n", Ruser); 
     System.out.printf("Enter 'login' to log in or 'register' to open another account"); 

    } 

    public static void login(String Luser, String Lpassword) { 
     for (int i = 1; i <= count; i++) { 
      System.out.printf("Enter 'login' to log in or 'register' to open another account"); 
      // user_id.users 
      // if(this.user_name) 
     } 
    } 

    public static void main(String[] args) { 
     Scanner scanner = new Scanner(System.in); 
     System.out.println("login"); 
     System.out.println("register"); 
     input = scanner.nextLine(); 


      while (input.equals("login")) { 

       System.out.println("username"); 
       String Luser = scanner.nextLine(); 
       System.out.println("Password"); 
       String Lpassword = scanner.nextLine(); 
       int a = count; 
       login(Luser, Lpassword); 
       System.out.println(""); 
       input = scanner.nextLine(); 
      } 
      while (input.equals("register")) { 

       System.out.println("username"); 
       String Ruser = scanner.nextLine(); 
       System.out.println("Password"); 
       String Rpassword = scanner.nextLine(); 
       users count = new users(Ruser, Rpassword); 
       System.out.println(""); 
       input = scanner.nextLine(); 
      } 
      while ((!input.equals("register")) || (!input.equals("login"))) { 
       System.out.println("invild option, chose login or regiser!"); 
       input = scanner.nextLine(); 


     } 
+0

你陷入循環的原因是因爲你輸入'register'或'login'的時候你動作了while循環,並且沒有破壞條件,因此它會一直持續下去。嘗試一些更簡單的if語句。 – f78xd23

+0

嘗試此問題作爲修復代碼的第一步。 https://stackoverflow.com/questions/5032356/using-scanner-nextline – f78xd23

回答

0

在main方法我會做這樣的事情:

public static void main(String[] args) { 
    Scanner scanner = new Scanner(System.in); 
    System.out.println("login"); 
    System.out.println("register"); 


    while(true) { 
     input = scanner.nextLine(); 
     switch(input) { 
     case "exit": 
      System.exit(0); 
      break; 
     case "login": 
      System.out.println("username"); 
      String Luser = scanner.nextLine(); 
      System.out.println("Password"); 
      String Lpassword = scanner.nextLine(); 
      int a = count; 
      login(Luser, Lpassword); 
      System.out.println(""); 
      //    input = scanner.nextLine(); 
      break; 
     case "register": 
      System.out.println("username"); 
      String Ruser = scanner.nextLine(); 
      System.out.println("Password"); 
      String Rpassword = scanner.nextLine(); 
      Users count = new Users(Ruser, Rpassword); 
      System.out.println(""); 
      //    input = scanner.nextLine(); 
      break; 
     default: 
      System.out.println("invild option, chose login, regiser or exit!"); 
      continue; 
     } 
    }