사용자가 EditText에 응답을 입력하고 '다음'버튼을 누르면 다음 임의의 단어가 생성되고 사용자가 정확한지 또는 올바른지 확인합니다. 잘못된.문자열 비교가 작동하지 않습니다
그러나 어떤 이유로 인해 코드가 EditText와 올바른 단어 응답을 모두 감지 할 수 있다고해도 '잘못된 대답'코드가 나에게 반환됩니다. 누구든지 제발 도와 주실 래요? 감사! (나는 아직도 이것에 새로운 ...)
* 편집 : 이것은 가 아니라 중복 질문 사람입니다. 나는 심각하게 누군가가 내 코드를 구체적으로보고 정확히 무엇이 잘못되었는지를 알 필요가 없기 때문에 나에게 말해야한다. 나는 다른 사람들의 질문을 보아서 해결할 수있는 일반적인 질문이 아니다. 왜냐하면 나는 아직도 이것에 대해 매우 새로운 질문을하고 있기 때문이다. .
etg2str.toString().equals(tgstr.toString());
==
운영자가 예를 들어 같은 기본 유형을 비교이기 때문에
을 int로 : 감사합니다 많은 * 당신이 .equals를 사용할 필요가
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();
}
}
});
}
참조 : 문자열 비교에 대한 http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – pedromss
사용 .equals() 메소드. – Wajahat