Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 函数不执行do while循环…不返回main?_Java_Controls_Do While_Decomposition - Fatal编程技术网

Java 函数不执行do while循环…不返回main?

Java 函数不执行do while循环…不返回main?,java,controls,do-while,decomposition,Java,Controls,Do While,Decomposition,我有一些我的类的代码,我们在其中合并堆栈。程序读入一个函数名列表以及与之相关的数据类型。然后,它允许用户输入函数头名称和参数。如果函数名与读入的函数名之一匹配,并且数据类型匹配,则函数名和参数将放在堆栈上,并提示用户再次输入标题 我试图让它通过do while循环重复提示,但它没有执行。我不确定在检查数据类型后控件是否没有被传输回main,或者是否存在其他问题。需要注意的一件可能很重要的事情是,被调用的函数保存在不同于main所在位置的类文件中,并且它们合并了函数分解 int x = -

我有一些我的类的代码,我们在其中合并堆栈。程序读入一个函数名列表以及与之相关的数据类型。然后,它允许用户输入函数头名称和参数。如果函数名与读入的函数名之一匹配,并且数据类型匹配,则函数名和参数将放在堆栈上,并提示用户再次输入标题

我试图让它通过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");
      }
公共静态无效检查函数(int索引){
布尔值=真;
if(inputParamNum!=函数[index].numParams){
System.out.println(“参数数量不匹配。\n参数应为:”);
对于(int j=0;j
任何建议都很好。谢谢:)

您的交换机中缺少
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.
您的交换机中缺少
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.