dimanche 25 février 2018

Find maximum of three inputs

Here are the steps we need to take:


a. Use the Scanner class to create an object to read input from the keyboard.

b. Declare three int variables called x, y, z, and max.

c. Prompt the user to enter a value for variables x, y, and z.

d. Find the maximum of x, y, and z, then assign the maximum to max.

e. Display the maximum.

My code seems to run endlessly whats wrong here's my code:

import java.util.Scanner;

public class Maximum {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int x;
        int y;
        int z;
        int max;

        System.out.print("Enter Value For x: ");
        x = keyboard.nextInt();

        System.out.print("Enter Value For y: ");
        y = keyboard.nextInt();

        System.out.print("Enter Value For z");
        z = keyboard.nextInt();

        max = Math.max(Math.max(x, y), z);

        if (x > y && x > z) {
            x = max;
        }
        if (y > x && y > z) {
            y = max;
        }
        if (z > x && z > y) {
            z = max; // Getting "assigned value is never used"
        }
        System.out.println("The Maximum is" + max);
    }
}

Aucun commentaire:

Enregistrer un commentaire