java - Java - 调用方法
问题描述
所以我创建了 3 种不同的方法,它们都返回一个整数,但是,当我尝试在我的“public static void”方法中调用这些方法时,它会出现一个我不明白的错误。
“菜单中的 findRange (int[]) 不能应用于 ()
import java.util.Arrays;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
System.out.println("Please enter:");
System.out.println("1 to find the range of scores");
System.out.println("2 to find the maximum score");
System.out.println("3 to find the second highest score");
Scanner scanner = new Scanner(System.in);
int users_choice = scanner.nextInt();
if (users_choice == 1){
findRange(); # Error is here
}
public static int findRange(int[] array){
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < array.length; i++){
array[i] = scanner.nextInt();
}
Arrays.sort(array);
int range = array[array.length - 1] - array[0];
System.out.println(range);
return range;
}
任何人都知道我如何解决这个问题并正确调用这个函数?谢谢。
解决方案
当您尝试 callfindRange()
时,由于所述方法的签名,它会给出错误。
public static int findRange(int[] array){
- 此方法需要int
array
传递给它(因此(int[] array)
)。
int
array
因此,除非您在方法中创建一个main
,然后使用类似的方法调用该方法,否则findRange(yourIntArray)
您将继续收到此错误。