需要找到三个数字中最大的位置

huangapple go评论91阅读模式
英文:

Need to find position of maximum of three numbers

问题

我正试图找出用户输入的三个数字中最大值的位置。我不知道如何找到该位置,但我可以找出这些数字的最大值。

以下是任务描述:
这里有一个名为getNumberOfMaxParam的方法,它接受三个整数数字,并返回这些数字在方法参数顺序中的第一个最大值的位置。

更新:我试图比较abc这三个整数,但是我遇到了“此方法必须返回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 );
      }

huangapple
  • 本文由 发表于 2020年7月28日 20:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63134556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定