import javax.swing.JOptionPane;
public class SampleLT1 {
public static void main(String[] args) {
double dAllowance;
int days;
String strDAllowance, strDays;
do{
strDAllowance = JOptionPane.showInputDialog(null,"Please enter Daily Allowance $:(>0)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
dAllowance = Double.parseDouble(strDAllowance);
if (dAllowance<0) System.out.println("Daily Allowance shoould be >0. Please enter again.");
if (dAllowance==0) {//if enter daily Allowance as 0, exit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
}while (dAllowance<0);//if daily allowance is smaller than 0, enter again
do{
strDays = JOptionPane.showInputDialog(null,"Please enter No. of working days: (>0 and <42)",
"Allowance", JOptionPane.INFORMATION_MESSAGE);
days = Integer.parseInt(strDays);
if(days<0 || days>42) System.out.println("No. of working days should >0 and <42. Please enter again.");
if (days==0) {//enter 0 to quit the program
System.out.println("Thank you for using Allowance Calculating Application.");
System.exit(0);
}
}while (days<0 || days>42);//if days <0 or >42, enter days again
System.out.println("For an internship student who worked "+days +" days with daily allowance $"+dAllowance+", his total allowance is $"+calAllowance(dAllowance,days));
System.exit(0);
}
public static double calAllowance(double dailyAllowance, int noOfDays){
return dailyAllowance * noOfDays;//calculate and return total allowance
}
}
如何使用while循環替換此do-while循環?
任何人都可以教我嗎?需要知道我即將開始的測試。如何使用while循環,for循環來替換這個do-while循環。
「for」循環是如何工作的?涉及哪些組件? –
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html可能會有所幫助。 – Dragondraikk
我建議你用一些簡單的例子來演奏循環(例如打印出數字)。他們並不難理解,但所有Swing代碼的偶然複雜性使得用您的示例「玩」更加困難。 –