0

非常感谢大家的帮助!我想通了,让它运行起来,我想我喜欢让事情变得比实际更困难。

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;
}
}
4

2 回答 2

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;



}
于 2013-10-16T05:21:14.670 回答
0

也许这可能有助于main方法

boolean validPass = checkPassword(password);

checkPassword方法可以定义为

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

thePassword值为nosecret的全局变量在哪里

于 2013-10-16T05:28:33.760 回答