2014-03-27 72 views
-4

我目前有一個登錄系統,只需要登錄號碼。這意味着用戶名,密碼和學生號碼(全部需要登錄)都只是數字。我正在尋找一種方法將用戶名和密碼更改爲單詞而不是數字。我將如何做到這一點?我嘗試過使用字符串,但無法從控制檯接受輸入。這是我的代碼;在整個登錄系統中將整數更改爲字符串

static Scanner console = new Scanner(System.in); 

public static void main(String[] args) { 

    final int UserID = 5555; 
    final int Password = 1234; 
    final int StudentNumber = 22334455; 


    int EnteredUserID; 
    int EnteredPassword; 
    int EnteredStudentNumber; 
    for (int s = 0; s <= 3; s++) { 
     if (s < 3) { 
      System.out.println("Enter your UserID to access lift;"); 
      EnteredUserID = console.nextInt(); 
      System.out.println("Your UserID is >>> " + EnteredUserID); 
      System.out.println("Enter your password to authenticate login;"); 
      EnteredPassword = console.nextInt(); 
      System.out.println("Password Entered is >>> " + EnteredPassword); 
      System.out.println("Enter your student number to finalise login and authentication;"); 
      EnteredStudentNumber = console.nextInt(); 
      System.out.println("Student Number Entered is >>> " + EnteredStudentNumber); 
      if (UserID == EnteredUserID && (Password == EnteredPassword) 
        && (StudentNumber == EnteredStudentNumber)) { 
       System.out.println("Athentication complete!"); 
       System.out.println("***Elevator access granted!***"); 
       System.out.println("Welcome..."); 
       Elevator a = new Elevator(); 
       Elevator.selectFloor(); 
       break; 

      } else { 
       System.out.println("Wrong UserID, Password or Student Number. Please try again."); 
      } 
     } else { 
      System.out.println("3 incorrect enteries detected. Access Denied!"); 
     } 
    } 
} 

我試圖改變爲字符串的用戶名和密碼,並使用console.nextLine()console.next(),但既不似乎工作和存儲輸入的內容。

+0

您是否還記得在比較「字符串」時將'=='更改爲'equals'? – ajb

+1

不要將'String'與'=='進行比較。你需要使用'equals()'。 – GriffeyDog

+1

您是否將nextInt()更改爲next或nextLine()? –

回答

0

更換intString

console.next();console.nextLine();

終於更換console.nextInt();,比較字符串時使用.equals()

UserID == EnteredUserID 

應該是:

UserID.equals(EnteredUserID) 
0

如果您尋找一個快速的方式將數字轉換爲字符串,然後只需做到以下幾點:

int number = 10; 
    String toText = ""+number; 

那是你的問題還是?

相關問題