jeudi 21 décembre 2017

Declaring Variable in an If Statement (Java)

I was attempting to code a program that gives you the season based on an inputed month and day, but ran into a problem halfway through. After I intialize the variable month in an if statement, to check if the value inputed is a valid month, I cannot use the variable month later in the code to find the season as it gives me the error "cannot find symbol." Any help would be much appreciated.

import java.util.Scanner;
public class Date 
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the number of the month: ");
        if (in.hasNextInt() && in.nextInt() > 0 && in.nextInt() <= 12)
            {
            int month = in.nextInt();
            }
        else 
            {
                System.out.println("Error: Invlaid month value");
            }
        System.out.println("Please enter the day: ");
        int day = in.nextInt();

        String season;
        if (0 < month && month <= 3)
            {
                season = "Winter";
            }
        else if (3 < month && month <= 6)
            {
                season = "Spring";
            }
        else if (6 < month && month <= 9)
            {
                season = "Summer";
            }
        else if (9 < month && month <= 12)
            {
                season = "Fall";
            }

    }

}

Aucun commentaire:

Enregistrer un commentaire