我正在使用分配的特定 Java 代码块,目前在 1 页上我使用它超过 30 次。这是因为在每个代码块中,对于我进行的每次比较,所有数字都需要增加 1。(示例 1)有没有办法编写一些将使用中的数字增加 1 的代码(就像您通常使用增量代码一样)。如果是这样,这在调用资源时也是可能的(示例 2)具体代码块:// Comparison 1 if (Uinput1.equals(answer1)) { edit1.setBackgroundColor(Color.parseColor("#00FF00")); } else { edit1.setBackgroundColor(Color.parseColor("#FF0000")); }第二次使用等等。// Comparison 2 if (Uinput2.equals(answer2)) { edit2.setBackgroundColor(Color.parseColor("#00FF00")); } else { edit2.setBackgroundColor(Color.parseColor("#FF0000")); }声明资源等 private String answer1; private String answer2;` this.answer1 = getString(R.string.answer1); this.answer2 = getString(R.string.answer2);` this.answer1 = getString(R.string.answer1); this.answer2 = getString(R.string.answer2); this.edit1 = findViewById(R.id.edit1); this.edit2 = findViewById(R.id.edit2); String Uinput1 = edit1.getText().toString(); String Uinput2 = edit2.getText().toString();编辑:我在第一部分使用了这个:private String[] answers = {"example", "example"}
3 回答
汪汪一只猫
TA贡献1898条经验 获得超8个赞
很多时候,当您在变量名中看到数字(即 float var1)时,是使用数组来代替的好时机。
在这种情况下,您可以使用这样的数组:
private String[] answers = new String[2];
要么
private String[] answers = {"foo", "bar"}
然后,您可以像这样访问数组的每个索引:
System.out.println(answers[0]); //prints "foo"
或者,您可以使用 for 循环对数组的每个索引执行操作。
for(int i = 0; i < answers.length; i++){ System.out.println(answers[i]);}
弑天下
TA贡献1818条经验 获得超8个赞
您可以执行以下操作:
function(a,b,c){
if (a.equals(b)) {
c.setBackgroundColor(Color.parseColor("#00FF00"));
}
else {
c.setBackgroundColor(Color.parseColor("#FF0000")); }
}
添加回答
举报
0/150
提交
取消