2016-03-23 67 views
0

我創建了一個小片段來說明我的問題。如果我要輸入其中一個用戶名,第一個檢查就可以確定用戶已經存在。但第二個輸入可以繞過檢查。我該怎麼做才能確保沒有人能繞過它?檢查散列表中是否已經存在一個值,並允許用戶輸入有效值(如果有)

public static void main(String[] args) { 
    HashMap<String, String> uID = new HashMap<>(); 
    Scanner scan = new Scanner(System.in); 
    String input = null; 
    uID.put("James", "25"); 
    uID.put("Jack", "25"); 
    uID.put("John", "25"); 

    System.out.println("Enter your username and age"); 

    input = scan.nextLine(); 
    if (uID.containsKey(input)) { 

     System.out.println("Username already exist, choose another username."); 
     input = scan.nextLine(); 
     uID.put(input, "25"); 

    } 
} 
+0

使用while循環,而不是'if' –

+0

如果用戶按照您的指示,那麼它永遠不會起作用,因爲'詹姆斯25'的(用戶名**和**的年齡,像你說的輸入)輸入時, 'input'的值將是'「James 25」',並且這不是地圖中的*鍵*。 – Andreas

+2

但是,積極的一點是,無論你說自己多大年紀,計算機都會認爲你是25歲。我的妻子喜歡這個想法。 – ajb

回答

3

一種if語句檢查單個條件,然後運行如果滿足該條件的代碼塊中的代碼。 A while聲明是下一個級別。它會檢查條件 - 在你的情況下,不管鍵是否在映射中 - 然後在條件爲真時運行代碼。運行代碼後,它會再次檢查條件,並會一直重複執行,直到它停止爲真。也就是說,它運行該塊內的代碼,而的條件爲真-其輸入已經在地圖中。

使用while loop檢查密鑰是否無效,並強制用戶輸入值直到輸入有效值。直到之後的被證實不更新地圖值或者

public static void main(String[] args) { 
    Map<String, String> uID = new HashMap<>(); 
    Scanner scan = new Scanner(System.in); 
    String input = null; 
    uID.put("James", "25"); 
    uID.put("Jack", "25"); 
    uID.put("John", "25"); 

    System.out.println("Enter your username and age"); 

    input = scan.nextLine(); 
    while (uID.containsKey(input)) { 

     System.out.println("Username already exist, choose another username."); 
     input = scan.nextLine(); 
    } 

    uID.put(input, "25"); 
} 
+0

似乎OP是對Java,解釋了一下幫助他。 –

+0

謝謝,我不知道while循環可以做檢查。 – newbieprogrammer

0

,您可以使用do-while循環。

do 
{ 
    System.out.println("Enter unique username and age"); 
    input = scan.nextLine(); 
}while(uID.containsKey(input)); 

uID.put(input, "25"); 

這裏我們要求用戶名,直到我們得到一個不存在的用戶名。

0

您需要先分割用戶名和年齡,然後再根據地圖進行檢查並存儲到地圖。

public static void main(String[] args) { 
      HashMap<String, String> uID = new HashMap<>(); 
      Scanner scan = new Scanner(System.in); 
      String input = null; 
      uID.put("James", "25"); 
      uID.put("Jack", "25"); 
      uID.put("John", "25"); 
      //prompt user to input details 
      System.out.println("Enter your username and age");   
      input = scan.nextLine(); 
      //split name and age 
      String arr[]=input.split("\\s"); 
      //check if name is already present in the map 
      while (uID.containsKey(arr[0])) { 
       //if already present,prompt user to reenter username alone 
       System.out.println("Username already exist, choose another username."); 
       //store the username to array's 0th index(as age is already present in 1st index) 
       arr[0] = scan.nextLine();     
      } 
      //if details not present,then store it to the map 
      uID.put(arr[0], arr[1]); 
      System.out.println("Your details saved.."); 
      System.out.println("uID="+uID); 
     } 
0

「如果」是一個時間條件檢查和執行語句中,如果條件滿足,這就是爲什麼你第一次檢查之後,第二個輸入超過它。 您可以更好地使用「while」進行連續的檢查過程並執行語句。

相關問題