2013-11-27 211 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

没问题。我已经编辑了我认为更好的答案。 –