0

我编写了一个简单的小程序来询问用户 ID、用户名和密码。到目前为止,我的用户 ID 有效,但用户名无效。我认为这与比较字符串有关,但唉,我不知道。

大多数重要的代码都位于 login.class 中。我正在使用存储在数组中的帐户对象,其中用户输入一个 ID,并且该帐户存储在等于该 ID 的位置(对于实例 Accounts[4] 将是一个帐户,Derp,并且用户通过输入 ID 来访问它4) 然后输入用户名,然后输入密码。我不希望进一步完善该计划而不是使其可操作,但始终欢迎提出建议。

public class Login {

static boolean IDisOK = false;
static boolean usernameisOK = false;
static boolean passwordisOK = false;

static Scanner input = new Scanner(System.in);
static Scanner input2 = new Scanner(System.in);

static Account test = new Account("guest", "blank", 0);
static Account[] accounts = new Account[1];

static int tmpID = -1;
static String tmpUsername = "has not been entered, weird";
static String tmpPassword = "has not been entered, weird";
static Account tmp;

public static void initAccounts() {
    accounts[0] = test;
}

public static void getaccID() { // Prompts and asks the user for and ID
    System.out.println("Please enter in your account ID");
    tmpID = input.nextInt();
    if (tmpID < accounts.length) {
        IDisOK = true;
    }//end of if statement

}//end of getaccID

public static void getaccUsername() { // Prompts and asks user for a username if      the ID was OK
    if (IDisOK == true) {
    System.out.println("Please enter the username linked to the ID: " + tmpID);
    tmpUsername = input2.nextLine();
    if (accounts[tmpID].username == tmpUsername) {
        usernameisOK = true;
        }//end of if statement
    } else {
        System.out.println("Please get a valid ID then come back.");
        System.exit(0);//end of if statement

    }
}//end of getaccUsername

public static void getaccPassword() { //Prompts and asks user for a password is the     username was OK
    System.out.println("Please enter the password set for this username: " +     accounts[tmpID].username);
    tmpPassword = input.nextLine();
    if (usernameisOK == true) {
        if (accounts[tmpID].password == tmpPassword) {
            passwordisOK = true;
            Main.setAuth(true);
        }
    }else{
        System.out.println("Please come back when you have a valid     username.");
        System.exit(0);

    }
}//end of getaccPassword

public static void signIn() {
    initAccounts();
    getaccID();
    getaccUsername();
    getaccPassword();



}//end of signIn method

}//end of class



public class Account {

String username = " ";
String password = " ";
int ID;



public Account(String userIn, String passwordIn, int idIn) {
    this.username = userIn;
    this.password = passwordIn;
    this.ID = idIn;
}//end of constructor



public String getUsername() {
    return this.username;
}
public String getPassword() {
    return this.password;
}
public int getID() {
    return this.ID;
}
public void setUsername(String input) {
    username = input;
}
public void setPassword(String input) {
    password = input;
}

}

public class Main {

static boolean hasbeensaved = false;
static boolean auth = false;
public static void main(String[] args) {
    Login.signIn();
    if (getAuth() == true) {





    }
4

2 回答 2

1

在这里,您正在比较引用的内存地址是否相同

 if (accounts[tmpID].username == tmpUsername)

应该

if (accounts[tmpID].username.equals(tmpUsername))

需要注意的是,Java 中的所有内容都是引用,除了原始数据类型。因此,如果您要比较对象值,则必须永远实现 equals 方法...

于 2013-09-04T02:34:10.673 回答
0

另一个经典的 == 和 .equals() 错过解释示例

== 比较两个引用变量是否指向同一个对象,在这里我们想要比较两个字符串对象是否具有相同的状态,因此使用 .equals() 如果两个字符串对象都具有返回布尔值 true相同的状态,否则返回false。

尝试这个:

public static void getaccUsername() { // Prompts and asks user for a username if          the ID was OK
    if (IDisOK == true) {
    System.out.println("Please enter the username linked to the ID: " + tmpID);
    tmpUsername = input2.nextLine();
    if (accounts[tmpID].username.equals( tmpUsername)) {// use .equals in string state comparision
        usernameisOK = true;
        }//end of if statement
    } else {
        System.out.println("Please get a valid ID then come back.");
        System.exit(0);//end of if statement

    }
}//end of getaccUsername
于 2013-09-04T02:36:25.767 回答