// 我一直试图让我的程序只接受来自扫描仪的单个字符。现在该程序将接受任意数量的字符,只要第一个字母是列出的字母之一。如果可能的话,我想用 charAt(0) 重写这段代码,或者添加一个 if 语句。if(sea.length > 1){} 类似的东西。我希望我能很好地解释这个问题以便理解。任何帮助表示赞赏,并感谢您的时间。
public static char promptForChoice(Scanner in) {
System.out.println("High, Low or Seven(H/L/S");
char sel = in.next().charAt(0);
sel = Character.toUpperCase(sel);
int i = 1;
while (i != 0) {
System.out.println("High, Low or Seven(H/L/S");
if (sel == 'H' || sel == 'L' || sel == 'S') {
i = 0;
} else {
System.out.println("You must enter only H, L or S.");
i = 1;
}
}
return sel;
}