Java中case2中如何获取case1中输入的变量的值并实现加一操作
switch (line) {
case "M":
System.out.println("显示指定的年月份日历");
System.out.print("Enter full year (e.g., 2001): ");
int year = input.nextInt();
// Prompt the user to enter month
System.out.print("Enter month in number between 1 and 12: ");
int month = input.nextInt();
// input.close();
// Print calendar for the month of the year
printMonth(year, month);
case "P":
System.out.println("显示前一月份日历");
printMonth(year, getMonthName(month +1));
static int year=0,month=0;
switch (line) {
case "M":
System.out.println("显示指定的年月份日历");
System.out.print("Enter full year (e.g., 2001): ");
year = input.nextInt();
System.out.print("Enter month in number between 1 and 12: ");
month = input.nextInt();
printMonth(year, month);
case "P":
System.out.println("显示前一月份日历");
printMonth(year, getMonthName(month +1));
定义一个成员变量或者静态变量保存起来,在case2中调用。
代码呢?
所以你的代码呢