我的班级有一些代码,我们在其中合并了堆栈。该程序将读取功能名称和随之而来的数据类型的列表。然后,它允许用户输入函数标题名称以及参数。如果函数名称与读入的函数之一匹配,并且数据类型匹配,则函数名称和参数将放在堆栈中,并提示用户再次输入标题。
我正在尝试通过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");
}
任何建议都会很棒。谢谢:)
答案 0 :(得分:2)
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
条语句。您应该使用像IntelliJ Idea这样的优质IDE,因为它会警告您此处发生的故障。
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.