if语句匹配用户输入到字符串的答案

时间:2014-12-08 01:36:38

标签: java android

我在android studio上为一个课程项目做了一个单词shuffle app。我需要帮助了解如何获取用户输入并将其与正确的String答案相匹配。我尝试了一些方法并且做得不够。我尝试使用if(word.equals(userAnswer))语句,但很难理解它。如何编写文本输入/输出的if语句以匹配我在android studio中的答案?

(可选问题)也是公开无效OnClick(View v)一个好方法,还是我应该用别的东西?

任何帮助将不胜感激。提前谢谢!

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    private EditText userAnswer;
    private TextView answerOutput;
    private TextView scrambledWord;

    public void OnClick(View v){
        scrambledWord = (TextView) findViewById(R.id.scrambledWord);
        userAnswer = (EditText) findViewById(R.id.answerInput);
        answerOutput = (TextView) findViewById(R.id.answerOutput);           
        Button button = (Button) v;

        String word = "Animals"; // scan for word

        ArrayList<Character> chars = new ArrayList<Character>(word.length()); // gets array with length of word
        for ( char c : word.toCharArray() ) {
            chars.add(c);
        }
        Collections.shuffle(chars); //shuffles the characters
        char[] shuffled = new char[chars.size()];
        for ( int i = 0; i < shuffled.length; i++ ) {
            shuffled[i] = chars.get(i);
        }
        String shuffledWord = new String(shuffled);

        if (word.equals(userAnswer)){
            answerOutput.setText("Correct!!");
        } else {
            answerOutput.setText("Sorry try again.");
        }
    }

3 个答案:

答案 0 :(得分:0)

您是否已将按钮的onClickListener设置为MainActivity?

您的MainActivity也应该实现OnClickListener

答案 1 :(得分:0)

这将允许您确定它们是否相同

if(word.equalsIgnoreCase(userAnswer.getText().toString())) {
    answerOutput.setText("Correct");
}

但是,一般来说,你有一个更大的问题,除非它在你没有向我们展示的代码中。

在您的活动onCreate / onStart的某个地方,您希望使用它可能的任何视图初始化您的按钮。

Button checkAnswer = (Button) findViewById(//whatever your id is)

然后,您要设置按钮的onClick侦听器。使用您正在使用的方法,最终需要两件事。首先这个

public class MainActivity extends Activity implements View.OnClickListener {

然后你需要将onClick监听器设置为你的Button,可能在OnCreate中。

checkAnswer.setOnClickListener(this);

然后你的onClick看起来像

@Override
public void onClick(View v) {

    if (word.equals(userAnswer)){

        answerOutput.setText("Correct!!");

    }

    else {

        answerOutput.setText("Sorry try again.");
    }

}

扰乱单词等的逻辑可能不会在这里点击。

此外,如果您要设置多个内容,则单击侦听器将执行类似此操作

@Override
public void onClick(View v) {
     switch(v.getId()) {
         case(R.id.//whatever): {
               //dosomething
              break;
          }
     }
 }

您可以为要设置MainActivity的所有视图设置多个案例。

编辑:自您更新代码

public class MainActivity extends Activity implements View.OnClickListener {

private EditText userAnswer;
private TextView answerOutput;
private TextView scrambledWord;
private String word;
private String shuffledWord;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scrambledWord = (TextView) findViewById(R.id.scrambledWord);
    userAnswer = (EditText) findViewById(R.id.answerInput);
    answerOutput = (TextView) findViewById(R.id.answerOutput); 
    createWord();
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

private void createWord() {
    word = "Animals";
    ArrayList<Character> chars = new ArrayList<Character>(word.length()); // gets array with length of word
    for ( char c : word.toCharArray() ) {
        chars.add(c);
    }
    Collections.shuffle(chars); //shuffles the characters
    char[] shuffled = new char[chars.size()];
    for ( int i = 0; i < shuffled.length; i++ ) {
        shuffled[i] = chars.get(i);
    }
    shuffledWord = new String(shuffled);
    shuffledText.setText(shuffledWord);
}

@Override
public void OnClick(View v){
    if (word.equalsIgnoreCase(userAnswer.getText().toString())){
        answerOutput.setText("Correct!!");
    } else {
        answerOutput.setText("Sorry try again.");
    }
}

答案 2 :(得分:0)

您需要使用userAnswer.getText()来获得答案。您的userAnswer变量目前属于EditText类型,这意味着要检查word.equals(userAnswer)是否始终返回false,因为它们属于不同类型。相反,请尝试word.equals(userAnswer.getText())检查他们的答案是否等于原始单词。要检查他们的答案是否等于加扰字,请使用shuffledWord.equals(userAnswer.getText())