如何在 if 语句中将对象与用户输入进行比较
How to compare objects to user input in an if statement
我正在尝试将用户输入(用户名和密码)与用户对象(例如 user1、user5)进行比较,以查看它们是否匹配,如果匹配,则用户将被定向到另一个 activity 因为他们将能够成功登录,如果没有,将显示一条消息。
我创建了一个名为 User 的 class,我调用它来获取用户名和密码。我认为 if 语句可以通过使用 .equal 来比较它们,但是当我尝试该应用程序时,无论输入是否正确,它都会显示详细信息不正确的消息。它就像是完全忽略了我所有的 if 语句,除了最后一个。谁能告诉我是否有更好的方法来实现这一点,或者我的代码是否有问题?错误没有出现在任何地方。
用户class:
public class User {
// Instance variables
static String userName;
static String password;
static String favColor;
// User constructor
public User(String initUserName, String initPassword, String initFavColor) {
userName = initUserName;
password = initPassword;
favColor = initFavColor;
}
// Getter method to return userName
public String getUserName(){
return userName;
}
public String getPassword(){
return password;
}
public String getFavColor(){
return favColor;
}
主要Activity:
logInBt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
User user1 = new User("Jason", "Sword", "Red");
User user2 = new User("Billy", "Dinosaur", "Blue");
User user3 = new User("Zack", "Elephant", "Black");
User user4 = new User("Trini", "Tiger", "Yellow");
User user5 = new User("Kimberly", "Bird", "Pink");
String userET = userEditText.getText().toString();
String userPassword = passwordEditText.getText().toString();
if(userET.equals(user1.getUserName()) & userPassword.equals(user1.getPassword())){
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
} else if(userET.equals(user2.getUserName()) && userPassword.equals(user2.getPassword())){
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
}else {
Toast.makeText(getApplicationContext(), "Incorrect details. Please try again", Toast.LENGTH_SHORT).show();
}
}
});
对象的属性不能是静态类型,
静态值在应用程序正在使用时被保留,
因此,每次创建对象实例时,它都会获取一个新值
解决方案:
public class User {
//Instance variables
private String userName;
private String password;
private String favColor;
// User constructor
public User(String userName, String password, String favColor) {
this.userName = userName;
this.password = password;
this.favColor = favColor;
}
// Getter method to return userName
public String getUserName(){
return userName;
}
public String getPassword(){
return password;
}
public String getFavColor(){
return favColor;
}
}
我可以推荐你阅读 static variables
我正在尝试将用户输入(用户名和密码)与用户对象(例如 user1、user5)进行比较,以查看它们是否匹配,如果匹配,则用户将被定向到另一个 activity 因为他们将能够成功登录,如果没有,将显示一条消息。 我创建了一个名为 User 的 class,我调用它来获取用户名和密码。我认为 if 语句可以通过使用 .equal 来比较它们,但是当我尝试该应用程序时,无论输入是否正确,它都会显示详细信息不正确的消息。它就像是完全忽略了我所有的 if 语句,除了最后一个。谁能告诉我是否有更好的方法来实现这一点,或者我的代码是否有问题?错误没有出现在任何地方。
用户class:
public class User {
// Instance variables
static String userName;
static String password;
static String favColor;
// User constructor
public User(String initUserName, String initPassword, String initFavColor) {
userName = initUserName;
password = initPassword;
favColor = initFavColor;
}
// Getter method to return userName
public String getUserName(){
return userName;
}
public String getPassword(){
return password;
}
public String getFavColor(){
return favColor;
}
主要Activity:
logInBt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
User user1 = new User("Jason", "Sword", "Red");
User user2 = new User("Billy", "Dinosaur", "Blue");
User user3 = new User("Zack", "Elephant", "Black");
User user4 = new User("Trini", "Tiger", "Yellow");
User user5 = new User("Kimberly", "Bird", "Pink");
String userET = userEditText.getText().toString();
String userPassword = passwordEditText.getText().toString();
if(userET.equals(user1.getUserName()) & userPassword.equals(user1.getPassword())){
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
} else if(userET.equals(user2.getUserName()) && userPassword.equals(user2.getPassword())){
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
}else {
Toast.makeText(getApplicationContext(), "Incorrect details. Please try again", Toast.LENGTH_SHORT).show();
}
}
});
对象的属性不能是静态类型, 静态值在应用程序正在使用时被保留, 因此,每次创建对象实例时,它都会获取一个新值
解决方案:public class User {
//Instance variables
private String userName;
private String password;
private String favColor;
// User constructor
public User(String userName, String password, String favColor) {
this.userName = userName;
this.password = password;
this.favColor = favColor;
}
// Getter method to return userName
public String getUserName(){
return userName;
}
public String getPassword(){
return password;
}
public String getFavColor(){
return favColor;
}
}
我可以推荐你阅读 static variables