【发布时间】:2014-10-15 20:35:26
【问题描述】:
我想知道将 do while 循环中的 if 语句转换为 do while 循环中的 switch 语句的最佳方法是什么。
收紧此代码的更好方法是什么?
do{
currency = keyboard.nextInt();
if (currency == 1)
{
sterling = euros * 0.79;
System.out.printf("£ %.2f", sterling);
}
else if (currency == 2)
{
usDollars = euros * 1.28;
System.out.printf("$ %.2f", usDollars);
}
else if (currency == 3){
auDollars = euros * 1.44;
System.out.printf("$ %.2f", auDollars);
}
else{
System.out.printf("Invalid Option");
}
System.out.printf("\nWould you like to go again");
System.out.printf("\n1. Yes\n2 No");
repeat = keyboard.nextInt();
if (repeat == 2){
System.out.printf("Exit Program");
System.exit(0);
}
}while(repeat == 1);
【问题讨论】:
-
你为什么不去read up on switch statements,自己试试,然后问你有没有问题?听起来您是在要求我们为您做这件事。
-
“在 do while loo 中转换此 if 语句的最佳方式”。我希望你知道 loo 的含义。您在发布问题之前阅读过您的问题吗?
标签: java switch-statement do-while