简体   繁体   English

Java代码未按计划运行

[英]Java code not running as planned

This isn't the entire code, just the problem area. 这不是整个代码,只是问题所在。 (Full code below) (下面的完整代码)

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");
}

I am making a program where the user makes an account, then later signs in. The part I am having trouble with is a verification to ensure the user is human (they obviously are, but still). 我正在制作一个程序,用户在该程序中创建一个帐户,然后登录。我遇到的问题是验证,以确保用户是人(显然是,但仍然)。 After creating their username and password, I generate and print a random int, and they have to type it back. 创建他们的用户名和密码后,我生成并打印一个随机int,他们必须将其键入回来。

My problem is that the program always skips to the else statement, and prints "The code entered is incorrect." 我的问题是程序总是跳到else语句,并显示"The code entered is incorrect." Even when I enter it perfectly. 即使我输入完美。

Can anyone tell me what I'm doing wrong? 谁能告诉我我在做什么错?

Below is the entire code, just in case. 下面是完整的代码,以防万一。

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");

    }

}

You are comparing a String with an integer, which can't be the same obviously. 您正在将String与整数进行比较,这显然不能相同。

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

To solve the problem you could convert the integer to a String and compare the strings (or the other way). 要解决该问题,您可以将整数转换为String并比较字符串(或以其他方式)。

String randm = String.valueOfrand.nextInt(100000) + 1;
...
String code = userInput.next();
if  (code.equals(randm)) // Comparing string now

Edit : As @Pshemo pointed out, int code = userInput.nextInt(); 编辑:正如@Pshemo指出的那样, int code = userInput.nextInt(); will do the work given that you compare the integers with == . 假设您将整数与==进行比较,它将完成工作。

It's because randm is int and code is String. 这是因为randm是int而代码是String。 So the code if (code.equals(randm)) always results to false. 因此,代码if (code.equals(randm))总是结果为false。

You cannot compare a string and an integer. 您不能比较字符串和整数。 Trying taking the input as an integer instead of a String. 尝试将输入作为整数而不是字符串。 String code = userInput.next();

Instead use: 而是使用:

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

Or you could convert the integer to a String as well 或者您也可以将整数转换为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM