如何解决回调方法并执行while循环
我是一个初学者/学生,在一些基本概念上苦苦挣扎。一世
我正在尝试回调我的 displaymenu
方法,以便当用户
输入正确的字符,它会提示他们输入温度
ints 然后完成后,让他们再次选择温度
直到他们选择退出选项。
这是我目前的代码:
import java.util.Locale;
import java.util.Scanner;
public class project7 {
static Scanner kb = new Scanner(System.in);
public static int fartocel(int f){
f = ( f - 32 ) * 5 / 9;
return f;
}
public static int caltofar(int c){
c = c * 9 / 5 + 32;
return c;
}
public static char displaymenu(){
char pick;
boolean first;
first = false;
do {
System.out.println("\nF - Far ");
System.out.println("C - Cel ");
System.out.println("Q - quit");
System.out.print("\nPick: ");
pick = Character.toUpperCase(kb.next().charAt(0));
switch (pick) {
case 'C':
case 'F':
case 'Q':
first = true;
break;
default:
System.out.println("\nPlease choose F,C,or Q");
break;
}
}while (!first);
return pick;
}
public static void main(String[] args) {
int c;
int f;
String answer;
System.out.println("\n\nYou picked: " + displaymenu());
do {
System.out.print("Enter the Cel temp: ");
c = kb.nextInt();
System.out.println("The temp " + c + " Cel " + "is Far " + caltofar(c));
System.out.print("enter the Far temp: ");
f = kb.nextInt();
System.out.print("The temp " + f + "Far " + "is Cel " + fartocel(f));
System.out.println("\n\nTry again? Yes or No ");
answer = kb.next();
}
while (answer.equalsIgnoreCase("Yes"));
}
}
解决方法
我正在尝试回调我的显示菜单方法
此处不建议使用短语回调,因为它可能会导致与 callback 函数混淆;只需说再打电话。为此,请更改
System.out.println("\n\nYou picked: " + displaymenu());
do {
…
System.out.println("\n\nTry again? Yes or No ");
answer = kb.next();
}
while (answer.equalsIgnoreCase("Yes"));
到
for (char picked; (picked = displaymenu()) != 'Q'; )
{
…
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。