函数不执行 do-while 循环...不返回主函数?
Function not doing do-while loop...not returning to main?
我的 class 有一些代码,我们在其中合并了堆栈。该程序读入一个函数名称列表以及它们附带的数据类型。然后它允许用户输入函数 header 名称和参数。如果函数名称与读入的其中一个匹配并且数据类型匹配,则函数名称和参数将被放入堆栈并提示用户再次输入 header。
我试图让它用 do-while 循环重复提示,但它没有执行。我不确定在检查数据类型后控件是否没有被传输回 main,或者是否存在其他问题。可能需要注意的重要一点是,被调用的函数保存在与 main 所在的不同的 class 文件中,并且它们正在合并功能分解。
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
语句,如果执行案例 2,则会发生 fall-through 并且案例 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.
我的 class 有一些代码,我们在其中合并了堆栈。该程序读入一个函数名称列表以及它们附带的数据类型。然后它允许用户输入函数 header 名称和参数。如果函数名称与读入的其中一个匹配并且数据类型匹配,则函数名称和参数将被放入堆栈并提示用户再次输入 header。
我试图让它用 do-while 循环重复提示,但它没有执行。我不确定在检查数据类型后控件是否没有被传输回 main,或者是否存在其他问题。可能需要注意的重要一点是,被调用的函数保存在与 main 所在的不同的 class 文件中,并且它们正在合并功能分解。
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
语句,如果执行案例 2,则会发生 fall-through 并且案例 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.