3 回答
![?](http://img1.sycdn.imooc.com/5458506b0001de5502200220-100-100.jpg)
TA贡献1820条经验 获得超10个赞
您只需要将输入和验证代码括在while循环中,并使用一个标志来控制是否回送。遵循以下原则:
boolean invalidInput;
do {
System.out.println("do you want to convert to Peso or Yen?");
char type = k.next().charAt(0); //get 1st char of user input
invalidInput = false;
switch(type)
{
case 'p':
case 'P':
//convert and print
total = usd * PESO;
System.out.printf("$%.2f = %.2f Peso\n", usd, total);
break;
case 'y':
case 'Y':
//convert and print
total = usd * YEN;
System.out.printf("$%.2f = %.2f Yen\n", usd, total);
break;
default:
System.out.println("Sorry Invalid Currency type, " +
"please try again");
invalidInput = true;
}
} while (invalidInput);
![?](http://img1.sycdn.imooc.com/533e4cde000148e602000200-100-100.jpg)
TA贡献1895条经验 获得超7个赞
在切换之前将bool for loop竞争设置为false
bool selected = false;
接下来,在开关周围创建一个while循环,设置为在“ selected”布尔值为false时循环
while(!selected){
switch(type)
在成功选择用户后,将布尔值更改为true
case 'p':
case 'P':
selected = true;
添加回答
举报