我正在创建一个测验活动,当用户将他或她的答案输入EditText并按下“下一步”按钮时,它将生成下一个随机单词并检查您是对还是错。
但是,出于某种原因,即使我的代码能够同时检测到EditText和正确的单词答案,它也会向我返回“错误答案”代码。有人可以帮我吗?谢谢! (我现在还是新人......)
* 编辑:这是不是重复的问题人。我真的需要有人专门查看我的代码并告诉我到底出了什么问题,因为我不知道。通过查看其他人的问题,这不是一个可以解决的问题(对我来说无论如何),因为我还是很陌生。非常感谢。*
btnNext.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
EditText etGuess= (EditText) findViewById(R.id.editTextGuess);
etguess2 = etGuess.getText().toString();
//these two above were actually placed outside but no luck. didnt work here either
String etg2str = etguess2; // the users edittext input
String tgstr = theguess; // the correct answer, the guess is from outside
if (myCD > 0 && etg2str.toString() == tgstr.toString()){
myCD--; //this is a countdown of the number of qns
Toast.makeText(getApplicationContext(), "correct answer"+ myCD + rantitle + " now",
Toast.LENGTH_LONG).show(); // this has nvr appeared for me
savePreferences("countDown", myCD);
RandomTitle(); //calling for random word on next load
Intent iNext = new Intent(QuizActivity.this,
QuizActivity.class);
iNext.putExtra("dataR", rantitle); //inputting random word for next load
startActivityForResult(iNext, 1);
finish();
}
else if (myCD > 0 && etg2str.toString() != tgstr.toString()){
myCD--;
Toast.makeText(getApplicationContext(), "wrong answer " + myCD + rantitle + etguess2 + theguess + " now",
Toast.LENGTH_LONG).show();
//now this one above appears for me everytime yet even tho etguess2 and theguess appears the same, I get wrong number
savePreferences("countDown", myCD);
RandomTitle();
Intent iNext = new Intent(QuizActivity.this,
QuizActivity.class);
iNext.putExtra("dataR", rantitle);
startActivityForResult(iNext, 1);
finish();
}
else{
Intent iScore = new Intent(QuizActivity.this,
ScoreActivity.class);
startActivityForResult(iScore, 1);
myCD = 10;
savePreferences("countDown", myCD);
finish();
}
}
});
}
答案 0 :(得分:3)
你需要使用.equals()来比较字符串:
etg2str.toString().equals(tgstr.toString());
==
运算符用于比较基本类型,例如int
因此,对于您的不平等情况,您可以使用!操作者:
!etg2str.toString().equals(tgstr.toString());