Java代码未按计划运行

时间:2015-08-08 00:04:25

标签: java

这不是整个代码,只是问题区域。 (下面的完整代码)

if (passLength > 4) {
    System.out.println("Signup alost complete.");

    Random rand = new Random();

    int randm = rand.nextInt(100000) + 1;

    System.out.println(
            "To confirm you are not a robot, please enter this code: "
                    + randm);

    String code = userInput.next();

    if (code.equals(randm)) {
        System.out.println("Thank you, " + userName);
        System.out.println(
                "You may now login. Begin by entering your username");

        if (userInput.equals(userName)) {
            System.out.println("Now please enter you password");
        }

        // Where the rest of the program will go
    }

    else {
        System.out.println("The code entered is incorrect.");
    }

}

else {
    System.out.println("Invalid Password");
}

我正在制作一个用户开户的程序,然后登录。我遇到问题的部分是验证确保用户是人(他们显然是,但仍然)。创建用户名和密码后,我生成并打印一个随机的int,然后他们必须输回。

我的问题是程序总是跳到else语句,并打印"The code entered is incorrect."即使我完全输入它。

谁能告诉我自己做错了什么?

以下是整个代码,以防万一。

public static void main(String[] args) {

    System.out.println("Hi! To begin please choose your username.");
    System.out.println("To do this, please enter your username below. ");
    System.out.println("This name must be at least three characters.");

    Scanner userInput = new Scanner(System.in);

    String userName = userInput.next();

    int nameLength = userName.length();

    if (nameLength > 2) {

        System.out.println("Now please enter your password.");
        System.out
                .println("This password must be at lease five characters");

        String passWord = userInput.next();

        int passLength = passWord.length();

        if (passLength > 4) {
            System.out.println("Signup alost complete.");

            Random rand = new Random();

            int randm = rand.nextInt(100000) + 1;

            System.out.println(
                    "To confirm you are not a robot, please enter this code: "
                            + randm);

            String code = userInput.next();

            if (code.equals(randm)) {
                System.out.println("Thank you, " + userName);
                System.out.println(
                        "You may now login. Begin by entering your username");

                if (userInput.equals(userName)) {
                    System.out.println("Now please enter you password");
                }

                // Where the rest of the program will go
            }

            else {
                System.out.println("The code entered is incorrect.");
            }

        }

        else {
            System.out.println("Invalid Password");
        }

    }

    else {
        System.out.println("Invalid Username");

    }

}

3 个答案:

答案 0 :(得分:3)

你正在将一个字符串与一个整数进行比较,这显然是相同的。

LIKE

要解决此问题,您可以将整数转换为字符串并比较字符串(或其他方式)。

int randm = rand.nextInt(100000) + 1;
...
String code = userInput.next();
if  (code.equals(randm)) 

编辑:正如@Pshemo指出的那样,String randm = String.valueOfrand.nextInt(100000) + 1; ... String code = userInput.next(); if (code.equals(randm)) // Comparing string now 将完成工作,因为您将整数与int code = userInput.nextInt();进行比较。

答案 1 :(得分:1)

这是因为randm是int而code是String。因此代码if (code.equals(randm))总是会导致错误。

答案 2 :(得分:0)

您无法比较字符串和整数。 尝试将输入作为整数而不是String。 String code = userInput.next();

改为使用:

int code= userInput.nextInt();
if(code==randm)

或者您也可以将整数转换为String