Java start: finding largest number from a list

I am trying to write code that asks the user for three numbers, and the program has to say which number is the largest. I didn't want to do a bunch of "System.out.print" in if and else if statements. The error according to the debugger is that the "largest" and "largest" were not initialized.

import java.util.Scanner;

public class number1
{

    public static void main(String[] args)
    {

        double a, b, c;
        double greatest, greatest1;

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter one number :");
        a  = keyboard.nextDouble();
        System.out.print("Enter another number :"); 
        b  = keyboard.nextDouble();
        System.out.print("Enter a third number :");
        c  = keyboard.nextDouble();

        if(a > b && a > c) {
            greatest = a;
        } //end of if statement

        else if(b > a && b > c){   
            greatest = b;
        }

        else if(c > a && c > b) { 
            greatest = c;
        }
        else if(a==b && c < a) {
            greatest = a;
            greatest1 = b;

        }

        else if(a==c && b < a) {
            greatest = a;
            greatest1 = c;
        }

        else if(b==c && a < b) {
            greatest = b;
            greatest1 = c;
        }

        else {
            System.out.print("All of the numbers are greatest");
        }
        System.out.print("The greatest number is: " +greatest+ "and" +greatest1);
    }
} 

      

+3


source to share


5 answers


Since others are already pointing out the problem. I'm going to point out that you can find the largest number of the three by simply doing:

biggest = Math.max(a,Math.max(b,c));

      

You can replace all the conventions in your code.

Displaying that you want the maximum set of integers (say an array of int). You can do something like:

biggest = array[0]; // initialize max to the first number of the set

      



iterating over the set and checking the max:

for(int i = 1; i < array.size; i++) 
   biggest = Math.max(biggest,array[i]);

      

You can also create your own maximum functionality, for your case you can:

public double maxDouble (double a, double b){
       if(a > b) return a;
       else return b;
}

      

You can read here more details about Math class and its methods (for example, the public static double max (double a, double b) .

+4


source


To fix this, you have to make sure that greatest

and are greatest1

always assigned to a value. What happens if it enters this code block:

if (a > b && a > c) {
    greatest = a;
} //end of if statement

      

greatest1

will not be assigned like that when it prints it out in the last statement that says



System.out.print("The greatest number is: " + greatest + "and" + greatest1);

      

This gives you an error.

+3


source


As a general hint: Initialize with 0:

double greatest = 0;
double greatest1 = 0;

      

But be careful, in your case this indicates an error in your code logic. Correct this logic, otherwise the result will be 0, which may be incorrect.

0


source


Another tip: if you press enterafter entering each number, after reading each number, you should read a new line \n

.

eg.

a  = keyboard.nextDouble();

      

Should be changed to

a  = keyboard.nextDouble();
keyboard.nextLine();

      

and etc.

0


source


Please do keyboard input and create a double array and then try below code.

double dblArray[] = {24.0,40.2,38.9};
Arrays.sort(dblArray);

System.out.print("The greatest number is: " +dblArray[dblArray.length-1]+ " and " +dblArray[dblArray.length-2]);

      

You can print the n largest numbers.

0


source







All Articles