如何使用while循环,for循环来替换这个do-while循环。

时间:2013-11-27 15:22:20

标签: java

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循环,for循环来替换这个do-while循环?
任何人都可以教我吗?需要知道我即将进行的测试。

2 个答案:

答案 0 :(得分:2)

您无需在for循环中输入任何内容。完全为空的for循环for(;;)while(true)相同。任何while循环都可以转换为for循环:

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

与:

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

因为你的例子是做的 - 虽然你不能在没有完全空循环的情况下获得相同的逻辑并自己做破解。 for / 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

但这是你可能不应该放下的那种答案。

答案 1 :(得分: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
    }
    }