dimanche 16 octobre 2016

How to collect information after "if" or "else" statements

I am trying to collect information about months regarding how far apart they are in regards to dates.

I begin by letting the User input what month they want to use.

Scanner uMonth = new Scanner(System.in);

String tMonth = uMonth.next();
    if (tMonth.equals("January")){
        int eMonth = 1;
    }else if (tMonth.equals("February")){
        int eMonth = 2;
    }else if (tMonth.equals("March")){
        int eMonth = 3;
    }else if (tMonth.equals("April")){
        int eMonth = 4;
    }else if (tMonth.equals("May")){
        int eMonth = 5;
    }else if (tMonth.equals("June")){
        int eMonth = 6;
    }else if (tMonth.equals("July")){
        int eMonth = 7;
    }else if (tMonth.equals("August")){
        int eMonth = 8;
    }else if (tMonth.equals("September")){
        int eMonth = 9;
    }else if (tMonth.equals("October")){
        int eMonth = 10;
    }else if (tMonth.equals("November")){
        int eMonth = 11;
    }else if (tMonth.equals("December")){
        int eMonth = 12;

Then the same for a second date except I use a new Scanner and use tMonth2 and uMonth2 for the next variables.

My problem comes when I try to use the eMonth variable outside of the loop.

int finalMonths = int(eMonth - eMonth2);{
if (finalMonths < 0);
finalMonths = (eMonth2 - eMonth);

Is there something I'm missing? Can I not use variables from inside a loop? Is there some extra step I'm missing?

Thanks.

Aucun commentaire:

Enregistrer un commentaire