我试图打印出一段简单的代码,但我似乎无法传递该方法的数组变量。很抱歉,如果这是显而易见的事情,我只是从Java开始。我得到了“MyClass类型中的方法asd不适用于参数(int,int)”
public int asd(int[] nums) {
int count = 0;
// Note: iterate to length-1, so can use i+1 in the loop
for (int i=0; i < (nums.length-1); i++) {
if (nums[i] == 6) {
if (nums[i+1] == 6 || nums[i+1] == 7) {
count++;
}
}
}
return count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(asd(1,22,3,4,2,2,2));
}
发布于 2019-06-19 08:49:31
该方法需要单个参数。您正尝试将每个整数作为单独的参数进行传递。
将值包含在{}
中,并为其添加前缀new int[]
以创建数组文字:
System.out.println(asd(new int[] {1,22,3,4,2,2,2}));
https://stackoverflow.com/questions/56546695
复制相似问题