2014-01-29 72 views
0

有人可以告訴我這有什麼問題嗎?我有一種感覺,j++不能增加,因此離開j在0,但我不知道。用戶名檢查錯誤

我在大約2分鐘內就使用了這個課程。它有什麼問題?這是我的代碼:

public static String[] names = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" }; 
//A to Z 
public static String[] check = { "a","zz" };//Purposely errors a and z, runs the whole check to make sure it works 

public static void usernameCheck() { 
    int errorCount = 0; 
    for(int i = 0; i < check.length;) { 
     for(int j = 0; j < names.length; j++) { 
      if(check[i].toLowerCase().equals(names[j].toLowerCase())) { 
       System.out.println("Username " + check[i] + " is already in use"); 
       System.out.println(i + " and " + j);//Just a check if i or j is increasing at all 
       errorCount++; 
      } 
      if(j == names.length) { 
       System.out.println("Moving to next username"); 
       j = 0; 
       i++; 
      } 
     } 

    } 
    if(errorCount > 0) { 
     System.out.println(errorCount + " Errors Occured"); 
     System.out.println("Please consider revising usernames and try again."); 
    }else 
     System.out.println("Run Successful, no errors have occured."); 
} 

public static void main (String[] args) { 
    usernameCheck(); 
} 

回答

0

j變量,如果j達到names.length,將不執行其下for循環迭代;當jnames.length - 1時,將會發生最後的for循環迭代,因此您的i++將永遠不會執行,並且您的ifor循環是無限循環。

有沒有必要檢查j == names.length。只是讓上i的增量循環增量i

for(int i = 0; i < check.length; i++) { 

併爲i每個循環中,j for循環將初始化j0。如果需要,可以將"Moving to next username"打印語句移至j for循環結束之後,但在for循環的i結束之前。

+0

我的目的是保持0直到j完成長度,我已經增加了j完全讀取後重置爲0,因爲我想檢查每個名稱,然後移動到下一個檢查。 – Abszol