I am just a beginner at programming, so I couldn't find my way around this. Existing questions seem to be either in other languages, or going over my head.
I am trying to write a small program for returning the week-specific day for any given date input.
import java.util.*;
class bday {
public static void main(String[] args){
Scanner yearfinder = new Scanner(System.in);
System.out.println("Please type any year");
int year = yearfinder.nextInt();
Scanner monthfinder = new Scanner(System.in);
System.out.println("Please type any month");
String textmonth = monthfinder.nextLine();
int month;
switch(textmonth) {
case ("january"):
month = 1;
break;
case ("february"):
month = 2;
break;
case ("march"):
month = 3;
break;
case ("april"):
month = 4;
break;
case ("may"):
month = 5;
break;
case ("june"):
month = 6;
break;
case ("july"):
month = 7;
break;
case ("august"):
month = 8;
break;
case ("september"):
month = 9;
break;
case ("october"):
month = 10;
break;
case ("november"):
month = 11;
break;
case ("december"):
month = 12;
break;
default:
System.out.println("The month you input was invalid");
}
Scanner datefinder = new Scanner(System.in);
System.out.println("Please type any day");
int date = datefinder.nextInt();
System.out.println("The date you gave was " + date + "/" + month + "/" + year);
}
}
//Jan 1 1900 was a monday.
Compiling this gives me an error that states that "variable month may not have been initialized", while pointing at "month" in the println statement. Is the initialization inside any of the cases invalid or something? I declared the month variable outside the switch...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…