我需要使下面的两个方法返回三个数字的最大值和最小值,但是我不断收到错误消息,指出在其他两个方法的上面的主方法中,它找不到12行中的MaxOf3符号和15行中的MinOf3符号。我该如何解决???
我在互联网上进行了查找,但是找不到正确的解决方案。
import java.util.Scanner;
public class MaxAndMin {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
int n3 = in.nextInt();
int max = MaxOf3(n1,n2,n3);
System.out.println("Maximum: " + max);
System.out.println("Minimum: " + MinOf3(n1,n2,n3));
}
public static int findMax(int max) {
int maximum = Math.max(8, 9);
int maximum2 = Math.max(maximum, 1);
System.out.println("The maximum of 8, 9, and 1 is " + maximum2);
return maximum2;
}
public static int findMin(int min) {
int minimum = Math.min(8, 9);
int minimum2 = Math.min(minimum, 1);
System.out.println("The minimum of 8, 9 and 1 is " + minimum2);
return minimum2;
}
}
答案 0 :(得分:1)
由于未实现MaxOf3
/ MinOf3
而收到错误,但是您可以删除它们并使用findMax
/ finaMin
findMax
/ finaMin
以获得3个int
参数并返回它们的最大值/最小值findMax
/ finaMin
而不是MaxOf3
/ MinOf3
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1 = in.nextInt();
int n2 = in.nextInt();
int n3 = in.nextInt();
int max = findMax(n1, n2, n3);
System.out.println("Maximum: " + max);
System.out.println("Minimum: " + findMin(n1, n2, n3));
}
public static int findMax(int n1, int n2, int n3) {
int maximum = Math.max(n1, n2);
int maximum2 = Math.max(maximum, n3);
System.out.println("The maximum is " + maximum2);
return maximum2;
}
public static int findMin(int n1, int n2, int n3) {
int minimum = Math.min(n1, n2);
int minimum2 = Math.min(minimum, n3);
System.out.println("The minimum is " + minimum2);
return minimum2;
}
输出
8
9
1
The maximum is 9
Maximum: 9
The minimum is 1
Minimum: 1