声明一个布尔变量并使用一个callMethod

时间:2013-10-16 05:18:40

标签: java variables methods

非常感谢大家的帮助!我想出来并让它运行起来,我想我更喜欢让事情变得更加困难。

import javax.swing.JOptionPane;

public class PasswordManager{

private static String masterPassword = "secret3";

public static void main(String[] args){
        boolean mypass = false;
        String password = JOptionPane.showInputDialog(null, "Enter Password:");
        mypass = checkPassword(password);

        if (mypass == true)
            System.out.println("Your Password is Correct");
        else
            System.out.println("Your Password is incorrect");
}

private static boolean checkPassword(String password){
        if(password.equalsIgnoreCase(masterPassword))
            return true;
        else
            return false;
}
}

2 个答案:

答案 0 :(得分:1)

private static String thePassword = "nosecret";

public static void main(String[] args){

    //*** TO DO ***: Declare a variable here of "boolean" type.
    boolean mybool = false;

    String password = JOptionPane.showInputDialog(null, "Enter The Password:");


    //*** TO DO ***: Call the checkPassword method and pass it to the "password" variable
    // from above and assign the result to your boolean variable.
    mybool = checkPassword(password);


}

private static boolean checkPassword(String password){

    //I'm giving you part of this code to show you one way of comparing Strings.  This way ignores case,
    //which may not be appropriate for passwords, but it's a useful method for many other things.
    //Also, I'm giving you the "return true;" part to show you one way a boolean can be sent back...
    //simply as the words "true" or "false" ... notice in code they're not in quotes just like numbers.
    //*** TO DO ***: Add an "else" statement below that returns false.
    if(password.equalsIgnoreCase(masterPassword))
        return true;



}

答案 1 :(得分:0)

这可能对main方法

有帮助
boolean validPass = checkPassword(password);

checkPassword方法可以定义为

private static boolean checkPassword(String password) {
    if(password.equalsIgnoreCase(thePassword))
        return true;
    else
        return false;
}

其中thePassword是值为nosecret的全局变量