Java跳过一行System.out.println();

时间:2016-06-19 07:37:41

标签: java

我需要有关Java代码的帮助。出于某种原因,它会跳过System.out.println("Username does not exist");。我几乎可以肯定我在某个地方搞砸了{}。帮助将不胜感激。

更新:

import java.util.Scanner;

class Bot {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Do you want to start the bot?");
    String startup = input.next();
    if (startup.equalsIgnoreCase("yes") || startup.equalsIgnoreCase("y")) {
        System.out.println("Okay, starting...");
        System.out.println("Please type your username");
        String user = input.next();
        if (user.equalsIgnoreCase("admin")) {
            System.out.println("Valid username. Please type your password.");
            String pass = input.next();
            if (pass.equalsIgnoreCase("admin") || pass.equalsIgnoreCase("root")) {
                System.out.println("Logging in...");
            } else if (!(pass.equalsIgnoreCase("admin") || pass.equalsIgnoreCase("root"))) {
                System.out.println("Wrong password.");
            } else {
                System.out.println("Username does not exist");
            }
            ;
        }
    } else if (startup.equalsIgnoreCase("no") || startup.equalsIgnoreCase("n")) {
        System.out.println("Okay, shutting down...");
    } else {
        System.out.println("Please answer with 'yes', 'y', 'no' or 'n', not '" + startup + "'.");
    }
}
};

原件:

class Bot {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Do you want to start the bot?");
        String startup = input.next();
        if (startup.equalsIgnoreCase("yes") || startup.equalsIgnoreCase("y")) {
            System.out.println("Okay, starting...");
            System.out.println("Please type your username");
            String user = input.next();
            if (user.equalsIgnoreCase("admin")) {
                System.out.println("Valid username. Please type your password.");
                String pass = input.next();
                if (pass.equalsIgnoreCase("admin") || pass.equalsIgnoreCase("root")) {
                    System.out.println("Logging in...");
                } else if (!(pass == "admin")) {
                    System.out.println("Wrong password.");
                } else if (!(user == "admin")) {
                    System.out.println("Username does not exist");
                }
                ;
            }
        } else if (startup.equalsIgnoreCase("no") || startup.equalsIgnoreCase("n")) {
            System.out.println("Okay, shutting down...");
        } else {
            System.out.println("Please answer with 'yes', 'y', 'no' or 'n', not '" + startup + "'.");
        }
    }
    };

1 个答案:

答案 0 :(得分:0)

} else if(!(user=="admin"))

您无法将字符串与==进行比较;你必须以与进一步做法相似的方式做到这一点:if(!user.equalsIgnoreCase("admin")) {

在这种情况下,简单的} else {会起作用。