【发布时间】:2020-06-13 03:58:45
【问题描述】:
我有一些代码用于我的班级,我们在其中合并了堆栈。程序读入一个函数名列表和伴随它们的数据类型。然后它允许用户输入函数头名称和参数。如果函数名与读入的其中一个匹配,并且如果数据类型匹配,则将函数名和参数放入堆栈,并提示用户再次输入标题。
我试图让它通过 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