【问题标题】:why won't my program accept int[] parameter in method when called in main?为什么我的程序在 main 中调用时不接受方法中的 int[] 参数?
【发布时间】:2020-09-29 06:42:12
【问题描述】:

由于某种原因,当我在 main 方法中调用它时尝试添加一个整数数组作为该方法的参数时,它无法将该参数识别为数组或其他东西,我不确定为什么会这样这。我这样称呼数组:has23([2,4])。

public static boolean has23(int[] n) {

        Boolean correct = null;

        while ((n.length == 2)) {
            for (int i : n) {
                Arrays.asList(n);
                if (Arrays.asList(n).contains(2) || Arrays.asList(n).contains(3)) {
                    correct = true;

                }
                else;
                correct = false;
            }
        }

        System.out.println(correct);
        return correct;

    }

【问题讨论】:

  • 那是 Python 语法。 Java 是:has23(new int[]{2,4]})

标签: java arrays main


【解决方案1】:

因为

has23([2, 4])

不是合法的 Java 语法。你可以这样做

has23(new int[] { 2, 4 })

相反。或者

int[] arr = { 2, 4 };
has23(arr);

不是

has23({2, 4});

【讨论】:

    猜你喜欢
    • 2019-05-28
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多