dimanche 16 octobre 2016

How to return a variable after assigning it a value in an If/Else statement in java?

I am having trouble returning a variable after I have assigned it a value in a an if else statement.

I have inserted the method in which I am attempting to do this below. I need to return the variable "absolutepday" and its value, in the if else statements, for use in a later method.

[Context] This code segment is supposed to input the present date and output the absolute day of the year that corresponds with that date. (Ex. 1/15 is the 15th absolute day of the year)

Any and all help is appreciated!

public static void todaysdate()
    {
        int absolutepday = 0;
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Please enter today's date:");
        
        System.out.print("What is the month (1-12)? ");
        int pmonth= input.nextInt();
        
        System.out.print("What is the day   (1-31)? ");
        int pday= input.nextInt();      
        
        if (pmonth == 1)
        absolutepday = pday;
        else if (pmonth == 2)
        absolutepday = 31 + pday;
        else if (pmonth == 3)
        absolutepday = 59 + pday;
        else if (pmonth == 4)
        absolutepday = 90 + pday;
        else if (pmonth == 5)
        absolutepday = 120 + pday;
        else if (pmonth == 6)
        absolutepday = 151 + pday;
        else if (pmonth == 7)
        absolutepday = 181 + pday;
        else if (pmonth == 8)
        absolutepday = 212 + pday;
        else if (pmonth == 9)
        absolutepday = 243 + pday;
        else if (pmonth == 10)
        absolutepday = 273 + pday;
        else if (pmonth == 11)
        absolutepday = 304 + pday;
        else if (pmonth == 12)
        absolutepday = 334 + pday;
        
        System.out.println(pmonth + "/" + pday + " is day #" + absolutepday + " of 365");
        System.out.println();
    }

Aucun commentaire:

Enregistrer un commentaire