2013-11-27 212 views
0
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循環。

+0

「for」循環是如何工作的?涉及哪些組件? –

+0

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html可能會有所幫助。 – Dragondraikk

+0

我建議你用一些簡單的例子來演奏循環(例如打印出數字)。他們並不難理解,但所有Swing代碼的偶然複雜性使得用您的示例「玩」更加困難。 –

回答

2

你不需要在for循環中輸入任何內容。完全空循環for(;;)while(true)相同。任何while循環可轉化爲一個循環:

int i = 0; 
while (i < 10) i++; 

是一樣的:

int i = 0; 
for (; i < 10;) i++; 

因爲你的例子是做,而你不能不做一個完全空白得到相同的邏輯循環,並自己做好休息。 A for/while也會在之前進行有條件檢查循環進入但是do-while只在之後執行。

你可以得到非常相似的東西,但你必須控制變量分配給一些符合條件:

int days = -1; 

for (; days < 0 || days > 42;) { 

} 

但我真的不知道爲什麼教練要你重構環路的東西像這樣冗餘。因爲您正在檢查循環內的所有可能值,所以不需要外部條件。你可以休息一下。否則,你正在做同樣的檢查兩次。我會親自做以下(甚至在做,一會兒):

double dAllowance; 
String strDAllowance; 

for (; ;) { 
    strDAllowance = JOptionPane.showInputDialog(/* ~~~ */); 

    try { 
     dAllowance = Double.parseDouble(strDAllowance); 

     if (dAllowance > 0) { 
      break; 

     } else if (dAllowance == 0) { 
      System.exit(0); 
     } 
    } catch (NumberFormatException e) {} 

    /* notify and automatically continues */ 
} 

如果你真的想在鑽研最深的「什麼是有效的循環邏輯」考慮以下幾點:

while (
    (dAllowance = Double.parseDouble(
     JOptionPane.showInputDialog(
      null, 
      "Please enter Daily Allowance $:(>0)", 
      "Allowance", 
      JOptionPane.INFORMATION_MESSAGE) 
    )) < 0) { 
     System.out.println("Daily Allowance shoould be >0. Please enter again."); 
} 

if (dAllowance == 0) { 
    System.out.println(
     "Thank you for using Allowance Calculating Application."); 
    System.exit(0); 
} 

請注意,我僅支持while循環以提高可讀性。大括號不應該是必要的。這當然也可以被轉換爲for循環:

for (; 
    (dAllowance = Double.parseDouble(
     JOptionPane.showInputDialog(
      null, 
      "Please enter Daily Allowance $:(>0)", 
      "Allowance", 
      JOptionPane.INFORMATION_MESSAGE) 
    )) < 0; 
     System.out.println("Daily Allowance shoould be >0. Please enter again.") 
); // <- no body necessary 

但是,這是你可能不應該放下的答案。

+0

唉!感謝您的解決方案。它幫助我很多! – KnockFall

+0

對於(;我<10;)'是有用的:我不知道你能做到這一點。 (+1)。 –

+0

@KnockFall沒問題。確保標記問題是否解決。:) – Radiodef

0

我已將第一個do-while循環轉換爲for循環,代碼如下。

編輯:

我覺得下面的版本比前一個好一點:

import javax.swing.JOptionPane; 

public class SampleLT1 { 

    public static void main(String[] args) { 
     double dAllowance = 0; 
     int days; 

     String strDAllowance, strDays; 
     for(int i =-1;i<0;){ 
      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); 
      } 
      if (dAllowance>0) i++; 
     }//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 
    } 
    } 
+0

非常感謝!雖然我仍然不確定循環,但我會盡力吸收! – KnockFall

+0

沒問題。我已經編輯了我認爲更好的答案。 –