我的类有一些代码,我们在其中合并了堆栈。该程序读入函数名称及其附带的数据类型的列表。然后,它允许用户输入函数头名称和参数。如果函数名称与读入的函数名称之一匹配并且数据类型匹配,则函数名称和参数将被放入堆栈中,并提示用户再次输入标题。
我试图让它用 do-while 循环重复提示,但它没有执行。我不确定在检查数据类型后控件是否没有被传输回主线程,或者是否存在其他问题。需要注意的可能很重要的一点是,被调用的函数保存在与 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");
}
任何建议都会很棒。谢谢:)
最佳答案
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);
}
您的 switch 中缺少 break
语句。您应该使用一个好的 IDE,例如 IntelliJ Idea,因为它会警告您此处发生的失败。
case 2:
System.out.println("it worked!");
case 3:
System.exit(0);
如果没有 break
语句,如果执行 case 2,则会发生失败,并且 case 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.
关于java - 函数不执行 do-while 循环...不返回 main?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60462023/