如何比较输出Java的If语句中的布尔值

时间:2018-05-04 02:11:02

标签: java if-statement boolean-expression

我正在创建一个用于比较临时用户名和密码的Java程序。我需要让用户知道他们的用户名和密码是否正确。如果“checkFinal”为true,我需要它来打印欢迎,否则打印不正确的信息。出于某种原因,我无法通过某种原因来比较布尔值。我也需要这个以不同的方法。我也愿意简化程序。所有帮助都是欢迎。提前致谢。

import java.util.Scanner;
public class program {
private static Scanner a;
private static String  inputusername;
private static Scanner b;
private static String  inputpassword;
private static String  validusername;
private static String  validpassword;
public static void main(String[] args) {
    compile();
}
public static void greeting() {
    System.out.println("Hello!");
    System.out.println("Note: All Things Are Case Sensitive!");
}
public static String questiona() {
    System.out.println("What Is Your Username?");
    a = new Scanner(System.in);
    inputusername = a.next();
    return inputusername;
}
public static String questionb() {
    System.out.println("What Is Your Password?");
    b = new Scanner(System.in);
    inputpassword = b.next();
    return inputpassword;
}
public static String username() {
    validusername = "username";
    return validusername;
}
public static String password() {
    validpassword = "password";
    return validusername;
}
private static boolean checkOne(String validusername, String inputusername) {
    boolean usernamecheck = false;
    if (validusername == inputusername) {
        usernamecheck = true;
    }
    return usernamecheck;
}
private static boolean checkTwo(String validpassword, String inputpassword) {
    boolean passwordcheck = false;
    if (validpassword == inputpassword) {
        passwordcheck = true;
    }
    return passwordcheck;
}
private static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) {
    boolean checkFinal = false;
    if (usernamecheck == true && passwordcheck == true) {
        checkFinal = true;
    } else {
        checkFinal = false;
    }
    return checkFinal;
}
public static void compile() {
    greeting();
    questiona();
    questionb();
    boolean usernamecheck = checkOne(validusername, inputusername);
    boolean passwordcheck = checkTwo(validpassword, inputpassword);
    checkFinal(usernamecheck, passwordcheck);
}
}

2 个答案:

答案 0 :(得分:2)

有很多地方需要更新。这是更新的代码。我根据检查结果在最后输出了一个真/假的字母。

import java.util.Scanner;
public class Password {
    private static Scanner a;
    private static String  inputusername;
    private static Scanner b;
    private static String  inputpassword;
    private static String  validusername;
    private static String  validpassword;
    public static void main(String[] args) {
        compile();
    }
    public static void greeting() {
        System.out.println("Hello!");
        System.out.println("Note: All Things Are Case Sensitive!");
    }
    public static String questiona() {
        System.out.println("What Is Your Username?");
        a = new Scanner(System.in);
        inputusername = a.next();
        return inputusername;
    }
    public static String questionb() {
        System.out.println("What Is Your Password?");
        b = new Scanner(System.in);
        inputpassword = b.next();
        return inputpassword;
    }
    public static String username() {
        validusername = "username";
        return validusername;
    }
    public static String password() {
        validpassword = "password";
        return validpassword;
    }
    private static boolean checkOne(String validusername, String inputusername) {
        boolean usernamecheck = false;
        if (username().equals(inputusername)) {
            usernamecheck = true;
        }
        return usernamecheck;
    }
    private static boolean checkTwo(String validpassword, String inputpassword) {
        boolean passwordcheck = false;
        if (password().equals(inputpassword)) {
            passwordcheck = true;
        }
        return passwordcheck;
    }
    private static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) {
        boolean checkFinal = false;
        if (usernamecheck && passwordcheck) {
            checkFinal = true;
        } else {
            checkFinal = false;
        }
        return checkFinal;
    }
    public static void compile() {
        greeting();
        questiona();
        questionb();
        boolean usernamecheck = checkOne(validusername, inputusername);
        //System.out.println(usernamecheck);
        boolean passwordcheck = checkTwo(validpassword, inputpassword);
        //System.out.println(passwordcheck);
        System.out.println(checkFinal(usernamecheck, passwordcheck));
    }
}

答案 1 :(得分:1)

您需要使用equals()来比较两个字符串。而且无需检查usernamecheck = true。请参阅下面的示例代码并相应修改。它会起作用。

    if (validpassword.equals(inputpassword)) {
            passwordcheck = true;
        }
 ...
 if (usernamecheck  && passwordcheck) {
        checkFinal = true;
    } else {
     ..
}