/ *这是一个在线测试的问题,其中学生编写一个功能,其答案由我的“正确功能”验证。对学生来说是隐藏的。我想比较main方法中两个函数的结果。 * /
import java.util.Arrays;
class SortArr
{
static int[] arr = new int[10];
public int[] sortin(int[] ans)
{
Arrays.sort(ans);
System.out.println(Arrays.toString(ans));
return ans;
}
public int[] correctfunction(int[] sol)
{
Arrays.sort(sol);
System.out.println(Arrays.toString(sol));
return sol;
}
public static void main(String[] args)
{
arr = new int[] {4,8,3,15,2,21,6,19,11,7};
SortArr ob=new SortArr();
ob.correctfunction(arr);
ob.sortin(arr);
if(Arrays.equals(ob.sol == ob.ans)) //non-static method //equals(Object) cannot be referenced from a static context
//variable ob of type SortArr: cannot find symbol
System.out.println("correct");
else
System.out.println("incorrect");
}
}
答案 0 :(得分:0)
首先Arrays.equals(parameter1, parameter2)
它需要两个参数,你所做的是完全错误的。要解决这个问题,请参阅下面的代码
public static void main(String[] args)
{
arr = new int[] {4,8,3,15,2,21,6,19,11,7};
SortArr ob=new SortArr();
int[] newSol = ob.correctfunction(arr);
int[] newAns = ob.sortin(arr);
if(Arrays.equals(newSol, newAns))
System.out.println("correct");
else
System.out.println("incorrect");
}
答案 1 :(得分:0)
该函数返回一个数组。因此,当您调用该方法时,将返回的数组保存在某个数组变量中! 我已对您的代码进行了必要的更改。希望他们为你工作。
import java.util.Arrays;
class SortArr
{
int arr1[];
int arr2[];
static int[] arr = new int[10];
public int[] sortin(int[] ans)
{
Arrays.sort(ans);
System.out.println(Arrays.toString(ans));
return ans;
}
public int[] correctfunction(int[] sol)
{
Arrays.sort(sol);
System.out.println(Arrays.toString(sol));
return sol;
}
public static void main(String[] args)
{
SortArr s = new SortArr(); //make an object of your class
arr = new int[] {4,8,3,15,2,21,6,19,11,7};
SortArr ob=new SortArr();
s.arr1 = ob.correctfunction(arr); // save the returned array
s.arr2 =ob.sortin(arr); // save the returned array
if(s.arr1 == s.arr2)
System.out.print("correct");
else
System.out.print("incorrect");
}
}