我正在创建一个应用程序,用户必须选择一个4位数字,这将与随机选择的隐藏4位数字进行比较,但是当我运行代码时应该检查我的数组以进行比较所选数字和随机数' Arrays.asList()。contains())'似乎没有提到它正在检查的数组确实具有它正在检查的值,有什么建议吗?
比较两个变量的代码: -
guess.v1 = code.int1;
guess.v2 = code.int2;
guess.v3 = code.int3;
guess.v4 = code.int4;
int[] guess_list = { guess.v1, guess.v2, guess.v3, guess.v4 };
if (Arrays.asList(guess_list).contains(home.value1)) {
if (code.int1 == home.value1) {
X1.setText("V");
guess.c1 = GuessStatus.V;
} else {
X1.setText("S");
guess.c1 = GuessStatus.S;
}
} else {
X1.setText("X");
guess.c1 = GuessStatus.X;
}
生成随机数的代码: -
Code.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent openCode = new Intent(b, code.class);
// adventure_time checks whether there is a saved game already,
// if 1, saved game,
adventure_time = 0;
// random number generation LET THE NUMBER GAMES BEGIN///
Random a1 = new Random();
random1 = new ArrayList<Integer>();
check.fudge = 0;
for (int index = 0; index < 4; index++) {
random1.add(a1.nextInt(5) + 1);
Log.v("MM", "" + random1.get(index));
}
value1 = random1.get(0);
value2 = random1.get(1);
value3 = random1.get(2);
value4 = random1.get(3);
startActivity(openCode);
}
});
答案 0 :(得分:4)
您没有拨打您认为自己的Arrays.asList
电话。您实际创建了List<int[]>
,而不是List<Integer>
您可能正在期待。 (Java中没有List<int>
这样的类型,因为它不支持基本类型的泛型。)
最简单的解决方法是改变这个:
int[] guess_list = { guess.v1, guess.v2, guess.v3, guess.v4 };
到此:
Integer[] guess_list = { guess.v1, guess.v2, guess.v3, guess.v4 };
然后,您最终会创建一个适当的List<Integer>
。