【发布时间】:2018-08-30 11:13:18
【问题描述】:
我有代码将来自EditText 的用户输入与正确的翻译(String[] answers)进行比较。现在我写了一个if/else 声明,但我试图把它放在一个for 循环中。否则我需要写超过 30 条语句。只是有几个数字不同。
现在我想出了以下“基础”:
for(int i = 0; i < answers.length; i++){ if (Uinput[].equals(answers[])) { edit1.setBackgroundColor(Color.parseColor("#00FF00")); } else { edit1.setBackgroundColor(Color.parseColor("#FF0000")); } }
我需要在括号([])之间放什么,因为它不允许它为空,以及进一步的任何建议或更改?提前致谢。
我目前使用的代码示例:
private String[] answers = {"daylight", "task"};
private EditText edit1;
this.edit1 = findViewById(R.id.edit1);
String Uinput[] = {edit1.getText().toString()};
// Comparison 1
if (Uinput[0].equals(answers[0])) {
edit1.setBackgroundColor(Color.parseColor("#00FF00"));
}
else {
edit1.setBackgroundColor(Color.parseColor("#FF0000"));
}
【问题讨论】:
-
遵循 Java 命名约定:变量名总是以小写字母开头。
Uinput应该是uinput。 -
在我的代码中更改了它。谢谢!
标签: java android for-loop if-statement