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

go评论77阅读模式

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

# 问题

12 3 12

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

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.

# 答案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

``````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

``````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. 返回最高索引

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!

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

go 42

go 42

go 57

go 31