【发布时间】:2020-07-12 22:09:23
【问题描述】:
我在 java 中创建了一个打印菜单屏幕的方法,如下所示:
MENU
c - Number of whitespace characters
f - Find text
r - Replace all !'s
q - Quit
Choose an option:
该方法返回一个字符。如何使用 main 中方法的返回值来制作 if else 语句?
printMenu 方法:
public static char printMenu(Scanner scnr) {
char menuOp;
//display the menu
System.out.println("\nMENU");
System.out.println( "c - Number of whitespace characters");
System.out.println("f - Find text");
System.out.println("r - Replace all !\'s");
System.out.println("q - Quit\n");
menuOp = ' ';
//loop until the user has entered a c, f, r or q
while (menuOp != 'c' && menuOp != 'f' &&
menuOp != 'r' &&
menuOp != 'q') {
System.out.println( "Choose an option:");
menuOp = scnr.nextLine().charAt(0);
}
//return the letter that the user entered
return menuOp;
} //end of the printMenu method
我希望能在 main 中做什么:
while (return value from printMenu method != 'q'){
printMenu(scnr);
if (return value from printMenu method == 'c'){ //do this
}
else if (return value from printMenu method == 'f'){ //do this
}
else if (return value from printMenu method == 'r'){ //do this
}
}
}
我还是新手,非常感谢大家的帮助、耐心和善意。谢谢
编辑 - 我必须使用 printMenu() 的返回值作为项目的要求。
【问题讨论】:
-
提示:查看
menuOp = scnr.nextLine().charAt(0);这一行。在这里,您将charAt的返回值 分配给menuOp。你也应该尝试用printMenu做类似的事情! -
很棒的提示,让我朝着正确的方向又迈出了一步,感谢扫地者!
标签: java if-statement methods return