-2

我正在制作一个应用程序,用户必须选择一个 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);

        }

    });
4

1 回答 1

4

您没有Arrays.asList拨打您认为的电话。您实际上是在创建 a List<int[]>,而不是List<Integer>您可能期望的 a are。(在 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>可以正常工作的。

于 2012-09-28T13:55:25.503 回答