How to Check the Highest Number Value using Java

0
39

This tutorial is all about how to check the highest number value using java between the three different user inputs. This check the highest number value using java  comes with the algorithm that is very useful in a Java program by using an “If function” or a “Switch” function.

It is important that you understand on how to use a Scanner so that the program depends on user input. Also using “If function” to compare different and many conditions and understand on declaring and initializing different variables.

Check the highest number value in Java Steps

1.Create a new project using your Netbeans.

2.Insert the code above your class.

  1. import java.util.Scanner;

3.Add a main method inside your class.

  1. public static void main(String args[]){
  2.  
  3. }

4.Declare four variables, one for scanner and 3 for integer data type.

  1. int n_first,n_second,n_third;
  2. Scanner sc = new Scanner(System.in);
  1. Insert the following code after step 4. This statement will ask the user inputs.
  1. System.out.print("Enter First #: ");
  2. n_first = sc.nextInt();
  3. System.out.print("Enter First #: ");
  4. n_second = sc.nextInt();
  5. System.out.print("Enter First #: ");
  6. n_third = sc.nextInt();

6. Insert the following code after step 5. The code is using nested if statement, assigned different condition, initialized the algorithm, and display output.

  1. if(n_first > n_second){
  2. if (n_first > n_second){
  3. System.out.println("The max number is " + n_first);
  4. }else{
  5. System.out.println("The max number is " + n_second);
  6. }
  7. }else{
  8. if (n_second > n_third){
  9. System.out.println("The max number is " + n_second);
  10. }else{
  11. System.out.println("The max number is " + n_third);
  12. }
  13. }

7. Run the program and the output should look like the image below.

8. Complete Source Code

  1. import java.util.Scanner;
  2. public class CheckHighestValue {
  3. public static void main(String args[]){
  4. int n_first,n_second,n_third;
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. System.out.print("Enter First #: ");
  8. n_first = sc.nextInt();
  9. System.out.print("Enter First #: ");
  10. n_second = sc.nextInt();
  11. System.out.print("Enter First #: ");
  12. n_third = sc.nextInt();
  13.  
  14. if(n_first > n_second){
  15. if (n_first > n_second){
  16. System.out.println("The max number is " + n_first);
  17. }else{
  18. System.out.println("The max number is " + n_second);
  19. }
  20. }else{
  21. if (n_second > n_third){
  22. System.out.println("The max number is " + n_second);
  23. }else{
  24. System.out.println("The max number is " + n_third);
  25. }
  26. }
  27. }
  28. }

After completing this tutorial, you learn about “nested if statement” and use “multiple conditions”. A nested if statement is an if statement inside an if statement.

If you have any comment or suggestions on How to Check the Highest Number Value using Java tutorial. Please feel free to leave your comment below, use the contact page of this website or use my contact information.

Email: info@kmjtech.net  |  Cell. No.: 09468362375

Readers might check this:

Sum Two Integers Value using Java

How to Print Hello World Text in Java

Facebook Comments
(Visited 34 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here