2016-06-23 72 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; 
    } 
相关问题