Persistent compilation error when searching for a maximum of three integers using java

I am new to Java and trying to find a solution to a problem that returns a persistent compilation error.

I pasted my code as below:

import java.util.*;

class MaxInteger {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter three integers: ");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int num3 = sc.nextInt();
        int max = getMax(num1, num2, num3);

        System.out.println("Maximum input integer is " + max);
    }

    public static int getMax(int num1, int num2, int num3) { 
        if ((num1 >= num2) && (num1 >= num3)) 
            return num1;
        else if ((num2 >= num1) && (num2 >= num3)) 
            return num2;
        else if ((num3 >= num1) && (num3 >= num2)) 
            return num3;
    }
}

edit: edit this question to improve it by seeing answers that may be considered non-topic.

  • The error message I get is the "missing return statement".

  • I understand that there is a Math.max method to find the maximum, but in this particular case, the task was performed to convert to if if statements.

  • My source code lacked brackets, parentheses, and parentheses. Perhaps they occurred while copying the code. Apologies for any confusion.

"tl; dr" : , , .

+4
9

, - , . , else if a else, , - .

public static int getMax(int num1, int num2, int num3) { 
    if ((num1 >= num2) && (num1 >= num3)) 
        return num1;
    else if ((num2 >= num1) && (num2 >= num3)) 
        return num2;
    return num3;
}

! , , , . , , .

. .

DRY. -, - ? , :

public static int maxOfThree(int num1, int num2, int num3) { 
    return Math.max(Math.max(num1,num2),num3);
}
+3

Math.max() . ,

int max =  Math.max(Math.max(num1,num2),num3);
+4

:

public static int getMax(int num1, int num2, int num3) { 
    if ((num1 >= num2) && (num1 >= num3)) 
        return num1;
    else if ((num2 >= num1) && (num2 >= num3)) 
        return num2;
    else if ((num3 >= num1) && (num3 >= num2)) 
        return num3;
}

else if/else.

+1

, getMax() . :

public static int getMax(int num1, int num2, int num3) { 
    if ((num1 >= num2) && (num1 >= num3)) 
        return num1;
    else if ((num2 >= num1) && (num2 >= num3)) 
        return num2;
    // otherwise num3 must be the greatest
    else return num3;
}

, , @Amy .

, :

public static int getMax(int num1, int num2, int num3) {
    int max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3) ? num2 : num3;
    return max;
}

, , , , . :

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

        System.out.print("Enter three integers: ");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int num3 = sc.nextInt();
        int max = getMax(num1, num2, num3);

        System.out.println("Maximum input integer is " + max);
    }

    public static int getMax(int num1, int num2, int num3) {
        int max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3) ? num2 : num3;
        return max;
    }
}
+1

getMax , . , , youre if - else.

0

, "" . , .

: . , , :

public static int getMax(int num1, int num2, int num3) { 
  if ((num1 >= num2) && (num1 >= num3)) {
    return num1;
  } else {
    if ((num2 >= num1) && (num2 >= num3)) {
      return num2;
    } else {
      if ((num3 >= num1) && (num3 >= num2)) {
      return num3;
    }
  }
}

, : " " "; : , , ... ? : , return. - , // !

, :

  • . Java, !
  • , .
  • And, of course, as others have rightly pointed out: do not reinvent the wheel. Java comes with tons of ready-to-use well-tested libraries. Besides the effects of learning, it makes no sense to implement your own min / max functions.
0
source

How about this solution?

private static int getMax(int a, int b, int c) {
    int[] values = new int[]{a, b, c};
    Arrays.sort(values);
    return values[2];
}
0
source

You can use Collections

public class MaxInteger {
 static List<Integer> list = new ArrayList<Integer>();  
 public static void main(String[] args) {
 System.out.print("Enter three integers: ");
    Scanner sc = new Scanner(System.in);
    for(int i = 0; i < 3; i++) {        
      list.add(sc.nextInt());
    }
    Collections.sort(list); // Sort the arraylist
    System.out.println("Maximum input integer is " + list.get(list.size() - 1));
    } 
}  
0
source

Java 8 solution for finding max from any number of inputs:

public static int maxOf(int... num) {
    return Arrays.stream(num).max().getAsInt();
}
0
source

Source: https://habr.com/ru/post/1653777/


All Articles