dimanche 1 novembre 2015

There is an issue with my if-else statements where my variables are not getting initialized

I am writing a simple program in Java that determines the numerical order of three users inputted integers values. For some reason, when I build the program, it says that my variables, min, mid, and max, have not been initialized. Here's the code:

import java.util.Scanner;

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

    System.out.print ("This program finds the minimum, maximum, and    middle number of three numbers that you input.");

    System.out.print ("\n\nEnter your first number: ");
    int num1 = scan.nextInt();
    System.out.print ("Enter your second number: ");
    int num2 = scan.nextInt();
    System.out.print ("Enter your third number: ");
    int num3 = scan.nextInt();

    int min, mid, max;

    if (num1<=num2 && num1<=num3)
    {min = num1;
        if (num2<=num3)
        {
            mid = num2;
            max = num3;
        }
        if (num3<=num2)
        {
            mid = num3;
            max = num2;
        }
    }
    if (num2<=num1 && num2<=num3)
    {min = num2;
        if (num1<=num3)
        {   mid = num1;
            max = num3;
        }
        if (num3<=num2)
        {
            mid = num3;
            max = num2;
        }
    }
    if (num3<=num1 && num3<=num2)
    {min = num3;
        if (num1<=num2)
        {   mid = num1;
            max = num2;
        }
        if (num2<=num1)
        {
            mid = num2;
            max = num1;
        }
    }

    System.out.print ("\nMinimum: " + min);
    System.out.print ("\nMiddle: " + mid);
    System.out.print ("\nMaximum: " + max);
}
}

I'm pretty sure the problem is with my if statements, but i'm not sure what it is. Thanks for the help!

Aucun commentaire:

Enregistrer un commentaire