簡體   English   中英

Java代碼未按計划運行

[英]Java code not running as planned

這不是整個代碼,只是問題所在。 (下面的完整代碼)

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

    }

}

您正在將String與整數進行比較,這顯然不能相同。

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

要解決該問題,您可以將整數轉換為String並比較字符串(或以其他方式)。

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

編輯:正如@Pshemo指出的那樣, int code = userInput.nextInt(); 假設您將整數與==進行比較,它將完成工作。

這是因為randm是int而代碼是String。 因此,代碼if (code.equals(randm))總是結果為false。

您不能比較字符串和整數。 嘗試將輸入作為整數而不是字符串。 String code = userInput.next();

而是使用:

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

或者您也可以將整數轉換為字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM