2016-06-23 71 views
-4

這是一個Java測驗應用程序。我試圖將存儲爲變量(回答一)的答案文本與實際答案前期進行比較。我無法讓他們平等。你看到這段代碼有什麼問題嗎?If語句來檢查變量是否等於一個字符串 - Java

EditText questionOne = (EditText) findViewById(R.id.answer_one); 
    String answeredOne = questionOne.getText().toString(); 
    if (answeredOne == "Prophase") { 
     score = score + 1; 
    } 

在回答問題時,我總是收到零。

+2

'if(「Prophase」.equals(回答一個)){...}' –

+0

改爲使用.equals。 – ManoDestra

回答

3

您不能對字符串使用==。這將檢查參考而不是價值。使用.equals();

if (string.equals("otherstring") { 
    // Do something here 
} 
0

Android使用java,所以你可以使用.equals方法。

EditText questionOne = (EditText) findViewById(R.id.answer_one); 
    if (questionOne.getText().toString().equals("Prophase")) { 
     score = score + 1; 
    } 
相關問題