How do I turn this into a simple program to take in 5 numbers and tell the least and greatest?

StackOverflow https://stackoverflow.com/questions/21572503

  •  07-10-2022
  •  | 
  •  

Question

I thought I was on to something for a while but then I had an issue with the result line being printed infinitely. Now I just can't get it to work at all. Does anyone have an idea how to turn this code style into something functional for my stated goals?

import java.util.Scanner;
public class Numerical
{
   public static void main (String  [ ] args   )
   {
   Scanner scan = new Scanner (System.in);
   System.out.println("Please enter 5 numbers one after another");
   {
   int a = scan.nextInt ( ); // First number input by user
   int b = scan.nextInt ( ); // Second number input by user
   int c = scan.nextInt ( ); // Third number input by user
   int d = scan.nextInt ( ); // Fourth number input by user
   int e = scan.nextInt ( ); // Fifth number input by user
      // Check if a is the greatest number
      if   (a > b);
      while(a > c);
      while(a > d);
      while(a > e);
      System.out.println ("The highest number is " +a);
        // Check if b is the greatest number
      else if   (b > a);
      while(b > c);
      while(b > d);
      while(b > e);
      System.out.println ("The highest number is " +b);

            // Check if c is the greatest number
      else if(c > a);
      while(c > b);
      while(c > d);
      while(c > e);
      System.out.println ("The highest number is " +c);
              // Check if d is the greatest number
      else if(d > a);
      while(d > b);
      while(d > c);
      while(d > e);
      System.out.println ("The highest number is " +d);
                   // Check if e is the greatest number
      else if(e > a);
      while(e > b);
      while(e > c);
      while(e > d);
      System.out.println ("The highest number is " +e);
      }
}

}
Was it helpful?

Solution 2

The best way would be to use an array and implement a sorting method; however, this example will be much easier for you to understand. We're simply comparing each number to the current largest number the user has entered:

import java.util.Scanner;

public class Blahh
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int largest = 0;

        int a = scan.nextInt(); // First number input by user
        int b = scan.nextInt();
        largest = largest(a, b);

        int c = scan.nextInt();
        largest = largest(largest, c);

        int d = scan.nextInt();
        largest = largest(largest, d);

        int e = scan.nextInt();

        System.out.println("The largest number is: " + largest(largest, e));
    }

    public static int largest(int num1, int num2)
    {
        if (num1 > num2)
            return num1;
        else
            return num2;
    }
}

OTHER TIPS

No need to do all of that. Read the inputs to an array*, use Arrays#sort, the first element is the smallest, the last is the greater.

I don't understand your code, what are those while and if statements? I advise you to go through a basic tutorial to better understand how things work. If you find yourself stuck, there is nothing better than debugging your code, not only you'll find the problem, but you'll understand why you had it.

* You might want to use ArrayList if you don't know the input's length

use the benefits of arrays! they make your code shorter, variable and they have a lot of helpful functions.

import java.util.Arrays;
import java.util.Scanner;

public class Numerical {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter 5 numbers one after another");

        int[] inputs = new int[5];

        for (int i = 0; i < inputs.length; i++) {
            inputs[i] = scan.nextInt();
        }

        Arrays.sort(inputs);

        System.out.println("The highest number is " + inputs[inputs.length - 1]);
    }

}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top