英文:
Need to find position of maximum of three numbers
问题
我正试图找出用户输入的三个数字中最大值的位置。我不知道如何找到该位置,但我可以找出这些数字的最大值。
以下是任务描述:
这里有一个名为getNumberOfMaxParam
的方法,它接受三个整数数字,并返回这些数字在方法参数顺序中的第一个最大值的位置。
更新:我试图比较a
、b
、c
这三个整数,但是我遇到了“此方法必须返回int类型的结果”的错误。
代码:
import java.util.Scanner;
class App {
public static int getNumberOfMaxParam(int a, int b, int c) {
int firstMax = Math.max(a, b);
int highMax = Math.max(firstMax, c);
if (a == highMax) {
System.out.println("1");
} else if (b == highMax) {
System.out.println("2");
} else if (c == highMax) {
System.out.println("3");
} else {
return highMax;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final int a = scanner.nextInt();
final int b = scanner.nextInt();
final int c = scanner.nextInt();
System.out.print(getNumberOfMaxParam(a, b, c));
}
}
现在,我如何找到这个位置呢?
英文:
I am trying to find the position of the maximum of three numbers that the user inputs. I do not know how to find the position of this but I can find max of the numbers.
Here is the task:
Here is the method named getNumberOfMaxParam that takes three integer numbers and returns the position of the first maximum in the order of the method parameters.
UPDATE: I am trying to compare the int a.b.c but I get This method must return a result of type int error
Code:
import java.util.Scanner;
class App {
public static int getNumberOfMaxParam(int a, int b, int c) {
int firstMax = Math.max(a, b);
int highMax = Math.max(firstMax, c);
if (a == highMax) {
System.out.println("1");
} else if (b == highMax) {
System.out.println("2");
} else if (c == highMax) {
System.out.println("3");
} else {
return highMax;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
final int a = scanner.nextInt();
final int b = scanner.nextInt();
final int c = scanner.nextInt();
System.out.print(getNumberOfMaxParam(a, b, c));
}
}
Now how do I find the position?
答案1
得分: 1
你可以使用Stream
API 来实现这个。只需按照所需顺序传递一个值的数组。然后创建一个值的 IntStream
并找到最大值。接着创建一个索引的 IntStream
,查找数组中第一个与最大值相匹配的值,然后返回该索引。
private static int getIndexOfMax(int... values) {
int max = IntStream.of(values).max().orElseThrow(IllegalStateException::new);
return IntStream.range(0, values.length)
.filter(index -> values[index] == max)
.findFirst().orElseThrow(IllegalStateException::new);
}
int index = getIndexOfMax(1, 2, 3);
assert index == 2;
英文:
You can do this with the Stream
api. You simply pass an array of values in the order you want. Then create an IntStream of the values and find the max. Then create an IntStream of the indices and find the first value in the array to see if it matches max, and return the index.
private static int getIndexOfMax(int... values) {
int max = IntStream.of(values).max().orElseThrow(IllegalStateException::new);
return IntStream.range(0, values.length)
.filter(index -> values[index] == max)
.findFirst().orElseThrow(IllegalStateException::new);
}
int index = getIndexOfMax(1, 2, 3);
assert index == 2;
答案2
得分: 0
**PS. 如果您为多个输入提供相同的值,可能会引发争议。**
public static int getNumberOfMaxParam(int a, int b, int c) {
int firstMax = Math.max(a, b);
int highMax = Math.max(firstMax, c);
return (highMax == a ? 1 : (highMax == b ? 2 : 3 ));
}
英文:
**PS. It could be controversial if you supply same value for multiple inputs.**
public static int getNumberOfMaxParam(int a, int b, int c) {
int firstMax = Math.max(a, b);
int highMax = Math.max(firstMax, c);
return (highMax == a ? 1 : (highMax == b ? 2 : 3 );
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论