andrea
andrea

Reputation: 31

code who can find the biggest of the 5 numbers

I am new here and I must write a code who can find the biggest of the 5 numbers who put the user. I have write something but it is not working. Can anyone help me? Thanks!

public static void main(String[] args) {
    // import java.lang.Math;
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please input 5 integers: ");
    int x = Integer.parseInt(keyboard.nextLine());
    int y = Integer.parseInt(keyboard.nextLine());
    int z = Integer.parseInt(keyboard.nextLine());
    int m = Integer.parseInt(keyboard.nextLine()); 
    int n = Integer.parseInt(keyboard.nextLine()); 
    int max = Math.max(x,y);

    if (x>y && x>z && x>m && x>n)
        System.out.println ("The first of your numbers is the bigest");

    else if(y>x && y>z && y>m && y>n)
        System.out.println ("The second of your numbers is the bigest");

    else if (z>x && z>y && z>m && z>n)
         System.out.println ("The third of your numbers is the bigest");

    else if (m>x && m>y && m>z && m>n)
         System.out.println ("The fourth of your numbers is the bigest");

    else if (n>x && n>y && n>z && n>m)
         System.out.println ("The fifth of your numbers is the bigest");




    System.out.println("The max of three is: " + max); 

Upvotes: 2

Views: 541

Answers (4)

user3437460
user3437460

Reputation: 17454

I see that you have used Math.max() which means you are allowed to use it and you may make full use of it. In case you cannot use array or other data structures, you may consider this:

To get the maximum of the 5 numbers, you can do this:

int largest = Math.max(Math.max (Math.max(x,y), Math.max(m,n)), z);

To print the position of the largest:

String pos = "";
switch(largest){
    case x: pos = "first";
            break;
    case y: pos = "second";
            break;
    case z: pos = "third";
            break;
    case m: pos = "forth";
            break;
    case n: pos = "fifth";
            break;
};

System.out.println ("The " + pos " + of your numbers is the biggest");

Upvotes: 0

user6904265
user6904265

Reputation: 1938

Let the Collections class does it for you :)

List<Integer> list = Arrays.asList(x,y,z,m,n);
int max = Collections.max(list).intValue();
System.out.println("And the winner is: " + max);

If you want to find also the position in the collection you should do as follow:

int index = list.indexOf(max);
String[]position={"first","second","third","fourth","fifth"};
System.out.println("The "+position[index]+" of your numbers is the bigest");

Upvotes: 2

Adam
Adam

Reputation: 36703

Whilst this can be answered with Collections.max() this looks like homework which is probably looking for a more nuts-and-bolts approach such as ....

int [] array = {x, y, z, m , n};

int largest = x;
int index = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] > largest) {
        largest = array[i];
        index = i;
    }
}
System.out.println("Largest found at index " + index);

Upvotes: 0

aladinsane7
aladinsane7

Reputation: 3

You only find maximum of x and y and print it in the end. You should assign new values to max in each conditions(in if - else if statements). For example in first condition, if x is bigger than y, z, m and n, you should assign x to max.

Upvotes: 0

Related Questions