Java 调用方法时出错
嘿,我试图调用一个方法“swapPairs(int[]nums)”,但是我得到了多个错误Java 调用方法时出错,java,arrays,eclipse,oop,Java,Arrays,Eclipse,Oop,嘿,我试图调用一个方法“swapPairs(int[]nums)”,但是我得到了多个错误 - Syntax error on token "{", delete this token - The method swapPairs(int[]) in the type ArrayMethods is not applicable for the arguments (int, int, int, int) - Syntax error on token "swapPairs", @ ex
- Syntax error on token "{", delete this token
- The method swapPairs(int[]) in the type ArrayMethods is not applicable
for the arguments (int, int, int, int)
- Syntax error on token "swapPairs", @ expected before this token
- Syntax error on token ";", @ expected after this token
- Syntax error on token "}", delete this token"
这是我的代码:
public class ArrayMethods {
public static void main(String[]args){
System.out.println(swapPairs({5,4,2,6}));
allLess({5,4,3}, {4,7,5});
}
public boolean allLess(int[] nums, int[] num){
int c=0;
if(nums.length==num.length){
for(int i=0; i<num.length; i++){
if(nums[i]<num[i])
return true;
}
}
return false;
}
public int[] swapPairs(int[] nums){
int[] x=new int[nums.length];
if(nums.length%2==0){
for(int i=0; i<nums.length; i++)
x[i]=nums[i+1];
return x;
}
else
for(int i=0; i<nums.length-1; i++)
x[i]=nums[i+1];
return x;
}
public void printArray(int[] nums){
for(int i=0; i<nums.length; i++)
System.out.println(nums[i]);
}
}
公共类数组方法{
公共静态void main(字符串[]args){
System.out.println(swapPairs({5,4,2,6}));
allLess({5,4,3},{4,7,5});
}
公共布尔allLess(int[]nums,int[]num){
int c=0;
if(nums.length==num.length){
对于(int i=0;i您不能从static
类访问非静态
成员
System.out.println(swapPairs({5,4,2,6})); // swapPairs() is non-static
allLess({5,4,3}, {4,7,5}); //allLess() is non-static
解决方案:
创建一个ArrayMethods
的实例,以访问swapPairs()
方法和allLess()
方法,或使这些方法静态
但是这里有更多的问题。你不能使用swapPairs({5,4,2,6})
你必须使用swapPairs(新的int[]{5,4,2,6})
单程
ArrayMethods arrayMethods = new ArrayMethods();
System.out.println(arrayMethods.swapPairs(new int[]{5, 4, 2, 6})); // *
arrayMethods.allLess(new int[]{5, 4, 3},new int[]{4, 7, 5});
请注意*
行。您正在显式调用toString()
。这不是一个好的做法
更多问题:
for (int i = 0; i < nums.length; i++)
x[i] = nums[i + 1]; // you will get ArrayIndexOutOfBoundsException
return x;
for(int i=0;i
当i=nums.length-1
时,nums[i+1]
将变为num[nums.length]
。现在数组中没有这样的索引。如果数组的大小为4
,则只有从0
到3
的索引
您可以将这些点数记入您的帐户,并更正这些错误。您无法从静态
类访问非静态
成员
System.out.println(swapPairs({5,4,2,6})); // swapPairs() is non-static
allLess({5,4,3}, {4,7,5}); //allLess() is non-static
解决方案:
创建一个ArrayMethods
的实例,以访问swapPairs()
方法和allLess()
方法,或使这些方法静态
但是这里有更多的问题。你不能使用swapPairs({5,4,2,6})
你必须使用swapPairs(新的int[]{5,4,2,6})
单程
ArrayMethods arrayMethods = new ArrayMethods();
System.out.println(arrayMethods.swapPairs(new int[]{5, 4, 2, 6})); // *
arrayMethods.allLess(new int[]{5, 4, 3},new int[]{4, 7, 5});
请注意*
行。您正在显式调用toString()
。这不是一个好的做法
更多问题:
for (int i = 0; i < nums.length; i++)
x[i] = nums[i + 1]; // you will get ArrayIndexOutOfBoundsException
return x;
for(int i=0;i
当i=nums.length-1
时,nums[i+1]
将变为num[nums.length]
。现在数组中没有这样的索引。如果数组的大小为4
,则只有从0
到3
的索引
您可以将这些点数记入您的帐户,并更正这些错误。您无法从静态
类访问非静态
成员
System.out.println(swapPairs({5,4,2,6})); // swapPairs() is non-static
allLess({5,4,3}, {4,7,5}); //allLess() is non-static
解决方案:
创建一个ArrayMethods
的实例,以访问swapPairs()
方法和allLess()
方法,或使这些方法静态
但是这里有更多的问题。你不能使用swapPairs({5,4,2,6})
你必须使用swapPairs(新的int[]{5,4,2,6})
单程
ArrayMethods arrayMethods = new ArrayMethods();
System.out.println(arrayMethods.swapPairs(new int[]{5, 4, 2, 6})); // *
arrayMethods.allLess(new int[]{5, 4, 3},new int[]{4, 7, 5});
请注意*
行。您正在显式调用toString()
。这不是一个好的做法
更多问题:
for (int i = 0; i < nums.length; i++)
x[i] = nums[i + 1]; // you will get ArrayIndexOutOfBoundsException
return x;
for(int i=0;i
当i=nums.length-1
时,nums[i+1]
将变为num[nums.length]
。现在数组中没有这样的索引。如果数组的大小为4
,则只有从0
到3
的索引
您可以将这些点数记入您的帐户,并更正这些错误。您无法从静态
类访问非静态
成员
System.out.println(swapPairs({5,4,2,6})); // swapPairs() is non-static
allLess({5,4,3}, {4,7,5}); //allLess() is non-static
解决方案:
创建一个ArrayMethods
的实例,以访问swapPairs()
方法和allLess()
方法,或使这些方法静态
但是这里有更多的问题。你不能使用swapPairs({5,4,2,6})
你必须使用swapPairs(新的int[]{5,4,2,6})
单程
ArrayMethods arrayMethods = new ArrayMethods();
System.out.println(arrayMethods.swapPairs(new int[]{5, 4, 2, 6})); // *
arrayMethods.allLess(new int[]{5, 4, 3},new int[]{4, 7, 5});
请注意*
行。您正在显式调用toString()
。这不是一个好的做法
更多问题:
for (int i = 0; i < nums.length; i++)
x[i] = nums[i + 1]; // you will get ArrayIndexOutOfBoundsException
return x;
for(int i=0;i
当i=nums.length-1
时,nums[i+1]
将变为num[nums.length]
。现在数组中没有这样的索引。如果数组的大小为4
,则只有从0
到3
的索引
您可以考虑这些要点,并纠正这些错误