java - 如果选择"is",则用户返回主菜单

标签 java netbeans

大家好,我对如何执行此操作有疑问,我已经用谷歌搜索了它,但它没有多大意义。 我需要这样做; 该程序询问用户是否希望继续。  如果选择是,将返回主菜单。  如果选择“否”,则应付总额将为 显示然后程序将终止

  int option, quantity, confirm;
  float childTotal;
  float adultTotal;
  float seniorTotal;

  final double childCost = 18;
  final double adultCost = 36;
  final double seniorCost = 32.50;

  char resume;

  Scanner input = new Scanner(System.in);

    System.out.println("1 = Child (4-6 yrs)");
    System.out.println("2 = Adult (16+ yrs)");
    System.out.println("3 = Senior (60+ yrs)" + "\n");

    System.out.println("Enter your option:" );
    option=input.nextInt();

    switch (option) {
        case 1:
            System.out.println("Enter total No of tickets for Child:" );
            quantity=input.nextInt();

            System.out.println("You are purchasing " + quantity + " child tickets");

            System.out.println("Press 1 to confirm");
            confirm=input.nextInt();

            break;

        case 2:
            System.out.println("Enter total No of tickets for Adult:" );
            quantity=input.nextInt();

            System.out.println("You are purchasing " + quantity + " adult tickets");

            System.out.println("Press 1 to confirm");
            confirm=input.nextInt();

            break;

        default:
            System.out.println("Enter total No of tickets for Senior:" );
            quantity=input.nextInt();

            System.out.println("You are purchasing " + quantity + " senior tickets");

            System.out.println("Press 1 to confirm");
            confirm=input.nextInt();

            break;
    }

    if (confirm !=1) {
        System.out.println("Incorrect key! User to go back to main menu");
    }


  System.out.println("Do you wish to continue? (Y/N) ");
  resume = input.next().charAt(0);

  if (resume == 'y' || resume == 'Y') {

} else {
      switch (option) {
            case 1:
                childTotal=(int) ((double) quantity*childCost) ;
                System.out.println("Total amount for child tickets: $" + childTotal);
                break;
            case 2:
                adultTotal=(int) ((double) quantity*adultCost) ;
                System.out.println("Total amount for adult tickets $" + adultTotal);
                break;
            default:
                seniorTotal=(int) ((double) quantity*seniorCost);
                System.out.println("Total amount for senior tickets $" + seniorTotal);
                break;
        }
  } 

最佳答案

创建一个设置为 true 的 boolean 变量。

boolean continueLoop = true;

将主要逻辑添加到 while 循环中,直到 continue 为 true

while(continueLoop){
  //Do your code here
  System.out.println("Do you wish to continue? (Y/N) ");
  resume = input.next().charAt(0);
  if (resume == 'y' || resume == 'Y'){}
  else{
  //Do Code here
   continueLoop=false;
   }
} //End while loop.

在 while 循环之后继续您的代码。我已将resume == y 的条件更改为resume !=y,因为如果用户不按y,代码应该停止迭代。

你的代码将变成

int option, quantity, confirm;
        float childTotal;
        float adultTotal;
        float seniorTotal;

        final double childCost = 18;
        final double adultCost = 36;
        final double seniorCost = 32.50;

        boolean continueLoop = true;
        char resume;

        Scanner input = new Scanner(System.in);
        while(continueLoop){
                System.out.println("1 = Child (4-6 yrs)");
                System.out.println("2 = Adult (16+ yrs)");
                System.out.println("3 = Senior (60+ yrs)" + "\n");

                System.out.println("Enter your option:" );
                option=input.nextInt();

                switch (option) {
                    case 1:
                        System.out.println("Enter total No of tickets for Child:" );
                        quantity=input.nextInt();

                        System.out.println("You are purchasing " + quantity + " child tickets");

                        System.out.println("Press 1 to confirm");
                        confirm=input.nextInt();

                        break;

                    case 2:
                        System.out.println("Enter total No of tickets for Adult:" );
                        quantity=input.nextInt();

                        System.out.println("You are purchasing " + quantity + " adult tickets");

                        System.out.println("Press 1 to confirm");
                        confirm=input.nextInt();

                        break;

                    default:
                        System.out.println("Enter total No of tickets for Senior:" );
                        quantity=input.nextInt();

                        System.out.println("You are purchasing " + quantity + " senior tickets");

                        System.out.println("Press 1 to confirm");
                        confirm=input.nextInt();

                        break;
                }

                if (confirm !=1) {
                    System.out.println("Incorrect key! User to go back to main menu");
                }


              System.out.println("Do you wish to continue? (Y/N) ");
              resume = input.next().charAt(0);

              if (resume == 'y' || resume == 'Y') {
              }else{
                  continueLoop = false;
                  switch (option) {
                    case 1:
                        childTotal=(int) ((double) quantity*childCost) ;
                        System.out.println("Total amount for child tickets: $" + childTotal);
                        break;
                    case 2:
                        adultTotal=(int) ((double) quantity*adultCost) ;
                        System.out.println("Total amount for adult tickets $" + adultTotal);
                        break;
                    default:
                        seniorTotal=(int) ((double) quantity*seniorCost);
                        System.out.println("Total amount for senior tickets $" + seniorTotal);
                        break;
                  }
              }
        }
    }

关于java - 如果选择"is",则用户返回主菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51022792/

相关文章:

java - 如何配置 Vert.x 事件总线以跨 Docker 容器集群工作?

java - 如何将任务重新分配给另一个组

java - 如何指定实现接口(interface)的引用方法参数

java - Tomcat 没有选择 index.html 上显示的 404

java - 我遇到了 char 问题

java - 将列连接到外键

java - Eclipse Hibernate 工具的问题

java - Netbeans如何恢复编译错误弹出

java - 如何在 Net-Beans(或一般 Java)中设置 FileReader 的路径?

java - Eclipselink 生成的规范元模型不会从另一个 jar 扩展基本元模型