我正在构建一个测验应用程序,我从用户那里获取两个文本输入并使用if else语句进行比较,但代码正在跳过" if和if"真正的部分,只有部分执行else。
public void checkAnswer_condition_q2(View view) {
EditText blank_one = (EditText)findViewById(R.id.fill_condition1);
EditText blank_two = (EditText)findViewById(R.id.fill_condition2);
if(btn_continue.getText() == "Continue"){
Intent intent = new Intent(condition_q2.this, loops.class);
startActivity(intent);
this.finish();
}
if(blank_one.getText().toString() == "if" && blank_two.getText().toString() =="else"){
Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
btn_continue.setText("Continue");
}
else if(blank_one.getText().toString() != "if" || blank_two.getText().toString() !="else"){
Toast.makeText(this, "Try Again", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
使用equals
比较字符串内容而非==
,您也忘记在btn_continue
中调用toString():
if(btn_continue.getText().toString().equals("Continue")){
Intent intent = new Intent(condition_q2.this, loops.class);
startActivity(intent);
this.finish();
}
if(blank_one.getText().toString().equals("if") && blank_two.getText().toString().equals("else")){
Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
btn_continue.setText("Continue");
}
else if(!blank_one.getText().toString().equals("if") || !blank_two.getText().toString().equals("else")){
Toast.makeText(this, "Try Again", Toast.LENGTH_SHORT).show();
}