函数未执行do-while循环...未返回至main?

问题描述 投票:0回答:1

我为班级提供了一些代码,在这些代码中我们合并了堆栈。该程序将读取功能名称和随之而来的数据类型的列表。然后,它允许用户输入函数标题名称以及参数。如果函数名称与读入的函数之一匹配,并且数据类型匹配,则将函数名和参数放在堆栈中,并提示用户再次输入标题。

我正在尝试通过do-while循环使其重复执行提示,但未执行。我不确定在检查数据类型之后是否没有将控件转移回main或是否存在其他问题。一个可能要注意的重要事情是,被调用的函数与main所在的目录保存在不同的类文件中,并且它们合并了功能分解。

    int x = -1;
    do {
        System.out.println("What would you like to do? Enter a number 1-3");
        System.out.println("1.) Call a Function");
        System.out.println("2.) End the Function");
        System.out.println("3.) Exit the Program");
        Scanner input = new Scanner(System.in);
        x = input.nextInt();

        switch(x) {
            case 1:
                System.out.println("Please enter function");
                Scanner scan = new Scanner(System.in);
                String functionHeader = scan.nextLine();
                getName(functionHeader);
            case 2:
                System.out.println("it worked!");
            case 3:
                System.exit(0);
        }
    }while(x != 2);

应该返回控制之前的最后一个功能:

     public static void checkFunction(int index) {
          boolean works = true;
          if(inputParamNum != functions[index].numParams) {
              System.out.println("Number of Paramaters do not match. \nThe parameter(s) should be:");
              for(int j = 0; j < functions[index].numParams; j++) {
                  System.out.print(functions[index].params[j] + " ");
              }
              System.exit(0);
          }


          for(int i = 0; i < functions[index].numParams; i++) {
              if(functions[index].params[i].equals("String") && ! 
            (inputParams[i].substring(0,1).equals("\""))) {
                  System.out.println("You did not input a String when a String was expected. \nThe correct parameter(s) for this function are/is:");
                  for(int j = 0; j < functions[index].numParams; j++) {
                       System.out.print(functions[index].params[j] + " ");
                  }
                  System.exit(0);
              }
              else if(functions[index].params[i].equals("char") && !(inputParams[i].substring(0,1).contentEquals("\'"))) {
                  System.out.println("You did not input a char when a char was expected. \nThe correct parameter(s) for this function are/is:");
                  for(int j = 0; j < functions[index].numParams; j++) {
                      System.out.print(functions[index].params[j] + " ");
                  }
                  System.exit(0);
              }
              else if(functions[index].params[i].equals("int")) {
                  try{
                      Integer.parseInt(inputParams[i]);
                  }catch(NumberFormatException e){
                      System.out.println("You did not input an int when an int was expected. \nThe correct parameter(s) for this function are/is:");
                      for(int j = 0; j < functions[index].numParams; j++) {
                          System.out.print(functions[index].params[j] + " ");
                      }
                  }
              }
              else if(functions[index].params[i].equals("float")) {
                  try{
                      Float.parseFloat(inputParams[i]);
                  }catch(NumberFormatException e){
                      System.out.println("You did not input a float when a float was expected. \nThe correct parameter(s) for this function are/is:");
                      for(int j = 0; j < functions[index].numParams; j++) {
                          System.out.print(functions[index].params[j] + " ");
                      }
                  }
              }
          }
          System.out.println("Congrats! you input correctly");
      }

任何建议都会很棒。谢谢:)

java controls do-while decomposition
1个回答
0
投票
       switch(x) {
            case 1:
                System.out.println("Please enter function");
                Scanner scan = new Scanner(System.in);
                String functionHeader = scan.nextLine();
                getName(functionHeader);
            case 2:
                System.out.println("it worked!");
            case 3:
                System.exit(0);
       }

您在交换机中缺少break语句。您应该使用良好的IDE,例如IntelliJ Idea,因为它会警告您此处发生的故障。

                case 2:
                    System.out.println("it worked!");
                case 3:
                    System.exit(0);

[没有break语句,如果执行了案例2,将发生失败,案例3也将被执行。


解决方案:

您将需要如下break语句:

            case 1:
                System.out.println("Please enter function");
                Scanner scan = new Scanner(System.in);
                String functionHeader = scan.nextLine();
                getName(functionHeader);
                break;
            case 2:
                System.out.println("it worked!");
                break;
            case 3:
                System.exit(0);
                break; //This one is technically unnecessary because it's the final case label.
© www.soinside.com 2019 - 2024. All rights reserved.