dimanche 29 septembre 2019

What do I need to change in the second "if" statement?

I cannot get the second number to be correct. There is something wrong in the "if" statement. Can anyone spot it? It works when I input 2 3 4 or 4 3 2 but not scrambled like 6 -5 4. If I do 6 -5 & 4 then my output is -5 -5 6

import java.util.*; // imported scanner
public class ThreeNumbers{

   public static void main(String[] args){

      Scanner input = new Scanner(System.in);

        // Prompt the user to enter three floating-point numbers
        System.out.print("Enter 3 numbers separated by a space: ");
        int num1 = input.nextInt();
        int num2 = input.nextInt();
        int num3 = input.nextInt();


        // Display inputs in sorted order
        System.out.print("Sorted numbers: ");

        // Find the minimum number input by the user
        if ((num1 < num2) && (num1 < num3)) {
            System.out.print(num1 + " ");
        } else if ((num2 < num1) && (num2 < num3)) {
            System.out.print(num2 + " ");
        } else if ((num3 < num1) && (num3 < num2)) {
            System.out.print(num3 + " ");

        }
        // Find the middle number input by the user
        if ((num1 != num2) && (num1 != num3) && (num1 >= num2) && (num1 <= num2)) {
            System.out.print(num1 + " ");
        } else if ((num2 != num1) && (num2 != num3)) {
            System.out.print(num2 + " ");
        } else if ((num3 != num1) && (num3 <= num2)) {
            System.out.print(num3 + " ");

        }
        // find the maximum number input by the user
        if ((num1 > num2) && (num1 > num3)) {
            System.out.print(num1 + " ");
        } else if ((num2 > num1) && (num2 > num3)) {
            System.out.print(num2 + " ");
        } else if ((num3 > num1) && (num3 > num2)) {
            System.out.print(num3 + " "); 

Aucun commentaire:

Enregistrer un commentaire