用户名检查错误

时间:2014-01-29 01:38:02

标签: java username

有人可以告诉我这有什么问题吗?我感觉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();
}

1 个答案:

答案 0 :(得分:0)

如果j到达for,您的j变量将不会执行下一个names.length循环迭代;当forj时,将发生上一次names.length - 1循环迭代,因此您的i++永远不会被执行,而i for循环也是无限循环。

无需检查是否j == names.length。只需让i for循环增量i的增量:

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

对于i的每个循环,j for循环会将j初始化为0。如果需要,可以将"Moving to next username" print语句移至j for循环结束后,但在i for循环结束之前。