English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Display the maximum value of three integer values in Java

The following is an example of displaying the maximum value of three integer values.

Example

public class Demo {
   public static void main(String[] args) {
      int val1 = 10;
      int val2 = 20;
      int val3 = 30;
      System.out.println("Number 1 = "+val1);
      System.out.println("Number 2 = "+val2);
      System.out.println("Number 3 = "+val3);
      if (val2 > val1) {
         val1 = val2;
      }
      if (val3 > val1) {
         val1 = val3;
      }
      System.out.println("The greatest of three numbers: "+val1);
   }
}

Output result

Number 1 = 10
Number 2 = 20
Number 3 = 30
The greatest of three numbers: 30

In the above program, we use three integer variables and compare them.

int val1 = 10;
int val2 = 20;
int val3 = 30;

Now use a condition to check which integer value is the largest.

if (val2 > val1) {
   val1 = val2;
}
if (val3 > val1) {
   val1 = val3;
}

Return the maximum value above.