以下是我计算年龄的代码。
我想在执行案例1时再次显示主菜单:Enter your Birth date(DD):
。
我正在休息,但那不起作用。
此处我的控制台在完成后再次显示Select your choice.
。
我想表明: import java.util.Calendar;
import java.util.Scanner;
public class Switch {
public static void main(String args[])
{
System.out.println("Select your choice");
System.out.println("1. To Calculate your Age");
System.out.println("2. Calculator");
System.out.println("3. To Exit");
boolean exit=false;
Scanner sc1=new Scanner(System.in);
int choice=sc1.nextInt();
do{
switch(choice)
{
case 1:
Calendar c1 = Calendar.getInstance();
int currDate = c1.get(Calendar.DATE);
//System.out.println(currDate);
int currMonth = c1.get(Calendar.MONTH)+1;
//System.out.println(currMonth);
int currYear=c1.get(Calendar.YEAR);
//System.out.println(currYear);
Scanner sc2=new Scanner(System.in);
System.out.println("Enter your Birth date(DD): ");
int birthDate=sc2.nextInt();
System.out.println("Enter your Birth month(MM): ");
int birthMonth=sc2.nextInt();
System.out.println("Enter your Birth year(YYYY): ");
int birthYear=sc2.nextInt();
int calculateDate=0;
calculateDate=currDate-birthDate;
int calculateMonth=currMonth-birthMonth;
int calculateYear=currYear-birthYear;
System.out.println("You are : "+ calculateDate+"days "+" "+calculateMonth+"months "+calculateYear+"years OLD");
System.out.println("thank you..");
System.out.println("\n");
break;
case 2:
System.out.println("This is case 2");
break;
case 3:
exit=true;
break;
}
}while(!exit);
}
}
任何想法?
m_words
答案 0 :(得分:2)
将下面的行移到do ...中,而while执行至少一次。
System.out.println("Select your choice");
System.out.println("1. To Calculate your Age");
System.out.println("2. Calculator");
System.out.println("3. To Exit");
Scanner sc1 = new Scanner(System.in);
int choice = sc1.nextInt();
SwitchCase.java
import java.util.Calendar;
import java.util.Scanner;
public class SwitchCase {
public static void main(String args[]) {
boolean exit = false;
do {
System.out.println("Select your choice");
System.out.println("1. To Calculate your Age");
System.out.println("2. Calculator");
System.out.println("3. To Exit");
Scanner sc1 = new Scanner(System.in);
int choice = sc1.nextInt();
switch (choice) {
case 1:
Calendar c1 = Calendar.getInstance();
int currDate = c1.get(Calendar.DATE);
// System.out.println(currDate);
int currMonth = c1.get(Calendar.MONTH) + 1;
// System.out.println(currMonth);
int currYear = c1.get(Calendar.YEAR);
// System.out.println(currYear);
Scanner sc2 = new Scanner(System.in);
System.out.println("Enter your Birth date(DD): ");
int birthDate = sc2.nextInt();
System.out.println("Enter your Birth month(MM): ");
int birthMonth = sc2.nextInt();
System.out.println("Enter your Birth year(YYYY): ");
int birthYear = sc2.nextInt();
int calculateDate = 0;
calculateDate = currDate - birthDate;
int calculateMonth = currMonth - birthMonth;
int calculateYear = currYear - birthYear;
System.out.println("You are : " + calculateDate + "days " + " "
+ calculateMonth + "months " + calculateYear
+ "years OLD");
System.out.println("thank you..");
System.out.println("\n");
break;
case 2:
System.out.println("This is case 2");
break;
case 3:
exit = true;
break;
}
} while (!exit);
}
}
示例运行
Select your choice
1. To Calculate your Age
2. Calculator
3. To Exit
1
Enter your Birth date(DD):
1
Enter your Birth month(MM):
1
Enter your Birth year(YYYY):
1920
You are : 14days 6months 97years OLD
thank you..
Select your choice
1. To Calculate your Age
2. Calculator
3. To Exit
答案 1 :(得分:0)
只需分配" true"执行案例1时变量退出。
答案 2 :(得分:0)
您已选择在循环外选择选项。选择选项的代码必须在循环内。
以下是可能有效的更新代码:
import java.util.Calendar;
import java.util.Scanner;
import java.util.Calendar;
import java.util.Scanner;
public class Switch {
public static void main(String args[])
{
boolean exit;
do{
System.out.println("Select your choice");
System.out.println("1. To Calculate your Age");
System.out.println("2. Calculator");
System.out.println("3. To Exit");
exit=false;
Scanner sc1=new Scanner(System.in);
int choice=sc1.nextInt();
switch(choice)
{
case 1:
Calendar c1 = Calendar.getInstance();
int currDate = c1.get(Calendar.DATE);
//System.out.println(currDate);
int currMonth = c1.get(Calendar.MONTH)+1;
//System.out.println(currMonth);
int currYear=c1.get(Calendar.YEAR);
//System.out.println(currYear);
Scanner sc2=new Scanner(System.in);
System.out.println("Enter your Birth date(DD): ");
int birthDate=sc2.nextInt();
System.out.println("Enter your Birth month(MM): ");
int birthMonth=sc2.nextInt();
System.out.println("Enter your Birth year(YYYY): ");
int birthYear=sc2.nextInt();
int calculateDate=0;
calculateDate=currDate-birthDate;
int calculateMonth=currMonth-birthMonth;
int calculateYear=currYear-birthYear;
System.out.println("You are : "+ calculateDate+"days "+" "+calculateMonth+"months "+calculateYear+"years OLD");
System.out.println("thank you..");
System.out.println("\n");
break;
case 2:
System.out.println("This is case 2");
break;
case 3:
exit=true;
break;
}
}while(!exit);
}
}