gpt4 book ai didi

java - 想要返回 Switch Case 中的主菜单

转载 作者:行者123 更新时间:2023-12-02 02:42:57 29 4
gpt4 key购买 nike

下面是我计算年龄的代码。

我想在执行案例 1 时再次显示主菜单:选择您的选择:...

我正在休息,但这不起作用。

在我的控制台中,完成后再次显示输入您的出生日期(DD):

我想显示:选择您的选择。有什么想法吗?

    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);
}
}

最佳答案

将下面的行移到 do...while 中,因为 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

关于java - 想要返回 Switch Case 中的主菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117385/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com