I am using a method getNumberOfMaxParam to find the position of the maximum of three numbers. How do I find position?

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

I am using a method getNumberOfMaxParam to find the position of the maximum of three numbers. How do I find position?

问题

我正在使用一个名为getNumberOfMaxParam的方法,该方法接受三个整数,并返回方法参数顺序中第一个最大值的位置。

问题:我不知道如何从3个数字中找到最大值的位置,我不想打印出最大值,我只想打印最大值的位置。

该方法应该返回1、2或3。

示例输入1:
12 3 12

示例输出1:
1

代码:(输出在底部)

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);
        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));
    }
}

输入:
1
7
8

输出:
8

我不想打印出最大值8,而是想打印出3,因为这是最大值8的位置。

谢谢,如果我把这个问题搞得太复杂了,我很抱歉。 I am using a method getNumberOfMaxParam to find the position of the maximum of three numbers. How do I find position?

英文:

I am using a method named getNumberOfMaxParam that takes three integer numbers and returns the position of the first maximum in the order of the method parameters.

Problem: I don't know how to find position of maximum from 3 numbers and I do not want to print maximum out, I only want to print the position of maximum

The method should return 1, 2 or 3

Sample Input 1:
12 3 12

Sample Output 1:
1

Code: (Output at bottom)

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);
        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));
    }
}

Input:
1
7
8

Output:
8

I do not want to print 8 which is the maximum but instead I want to print 3 as that is position of maximum (8)

Thanks and sorry if I over-complicated this. I am using a method getNumberOfMaxParam to find the position of the maximum of three numbers. How do I find position?

答案1

得分: 1

使用数组存储值,然后找出最大数及其索引

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int[] nums = new int[3];
        nums[0] = scanner.nextInt();
        nums[1] = scanner.nextInt();
        nums[2] = scanner.nextInt();

        System.out.print(getNumberOfMaxParam(nums));
    }

    public static int getNumberOfMaxParam(int[] nums) {

        int maxValue = nums[0];
        int index = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > maxValue) {
                maxValue = nums[i];
                index = i;
            }
        }
        return index + 1;

    }
}
英文:

Try this, use arrays to store value and then find out the max number with its index

import java.util.Arrays; 

public class Main
{
	public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);

    int[] nums = new int[3];
    nums[0] = scanner.nextInt();
    nums[1] = scanner.nextInt();
    nums[2] = scanner.nextInt();


    System.out.print(getNumberOfMaxParam(nums));
	}
	 
  public static int getNumberOfMaxParam(int[] nums) {

    int maxValue = nums[0];
    int index = 0;
    for(int i = 1; i &lt; nums.length; i++){
        if(nums[i] &gt; maxValue){
            maxValue = nums[i];
            index = i;
            }
        }
    return index+1;
    
    }
}

答案2

得分: 0

你找到了最大的数。现在将其与方法参数进行比较,找出它是它们中的哪一个,并返回其位置。

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) {
        return 1;
    }
    else if (b == highMax) {
        return 2;
    }
    else if (c == highMax) {
        return 3;
    }
    else {
        throw new RuntimeException("这里有些奇怪。");
    }
}
英文:

You found the highest number. Now compare it with the method parameters to find which of them it is and return its position.

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) {
        return 1;
    }
    else if (b == highMax) {
        return 2;
    }
    else if (c == highMax) {
        return 3;
    }
    else {
        throw new RuntimeException(&quot;Something strange here.&quot;);
    }
}

答案3

得分: 0

如果你有这三个值,我可能会这样做。基本上只是比较这些值并跟踪其索引。如果某个值高于当前最大值,就将新的当前最大值以及最高索引设置为该值。

public static int getNumberOfMaxParam(int a, int b, int c) {
    int currentMax = a;
    int highestIndex = 1;
    if (currentMax < b) {
        currentMax = b;
        highestIndex = 2;
    }

    if (currentMax < c) {
        currentMax = c;
        highestIndex = 3;
    }

    return highestIndex;
}

我假设你正在开始学习编程,所以这可能有点复杂,但是下面是说明:

如果你想让事情变得更容易使用并允许更多的参数,你可以使用这个:

public static int getNumberOfMaxParam(int... values) {
    if (values.length == 0) return 0;

    int highestIndex = 1;
    int currentMax = values[0];
    for (int i = 1; i < values.length; i++) {
        if (values[i] > currentMax) {
            currentMax = values[i];
            highestIndex = i + 1;
        }
    }
    return highestIndex;
}

解释一下发生了什么:

int... values 定义了一个可变长度的 int 数组(值的列表)(这就是三个点的作用)。这允许你以任意数量的参数调用函数。要了解更多信息,请查阅“Java Varargs”。

所以现在我们有一个值的列表,我们可以按照一系列简单的步骤来获取最高值的索引:

  1. 定义基本值(开始的最高索引为1,并且当前最大值为数组中的第一个值([0] 访问索引为0的元素,即第一个元素))
  2. 遍历剩余的值
  3. 将当前迭代的值与当前最大值进行比较
  4. 如果该值高于当前最大值,则更新它以及当前最高索引
  5. 重复步骤3和4,直到所有值都处理完毕
  6. 返回最高索引

如果你喜欢的话,你可以在这里的 ideone 链接 上查看这个解决方案!

英文:

If you have those three values, I would probably do it like this. Essentially just comparing the values and tracking the index with it. If something is higher than the current maximum, set the new current maximum as well as the highest index.

public static int getNumberOfMaxParam(int a, int b, int c) {
    int currentMax = a;
    int highestIndex = 1;
    if (currentMax &lt; b) {
        currentMax = b;
        highestIndex = 2;
    }


    if (currentMax &lt; c) {
        currentMax = c;
        highestIndex = 3;
    }

    return highestIndex;
}

I am going to assume that you are starting to learn programming so this might be a little much but here goes:

If you want to make things a little easier to use and allow for more parameters, you could use this:

public static int getNumberOfMaxParam(int... values) {
    if (values.length == 0) return 0;

    int highestIndex = 1;
    int currentMax = values[0];
    for (int i = 1; i &lt; values.length; i++) {
        if (values[i] &gt; currentMax) {
            currentMax = values[i];
            highestIndex = i + 1;
        }
    }
    return highestIndex;
}

To explain what's happening:

int... values defines an int array (a list of values) of variable length (that's what the three dots are for). That allows you to call the function with as many arguments as you like. For more information on that, look up "Java Varargs".

So now we have a list of values and we can follow a simple set of steps to get the index of the highest value:

  1. Define base values (a starting highest index of 1 and as current maximum the first value in the array ([0] accesses the element at index 0 which is the first element)
  2. Loop through the remaining values
  3. Compare the value of the current iteration with the current maximum
  4. If that value is higher than the current maximum, update it as well as the current highest index
  5. Repeat 3 and 4 for all values
  6. Return the highest index

You can have a look at this solution in a little ideone here if you like!

huangapple
  • 本文由 发表于 2020年7月30日 16:35:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63169297.html
匿名

发表评论

匿名网友

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

确定