Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用while循环,for循环替换此do while循环。 import javax.swing.JOptionPane; 公共类样本1{ 公共静态void main(字符串[]args){ 双延迟; 国际日; 字符串标准容差,标准日期; 做{ strdAmount=JOptionPane.showInputDialog(空,“请输入每日津贴$:(>0)”, “津贴”,作业窗格。信息(信息); dAllowance=Double.parseDouble(标准容差); if(dAllowance_Java - Fatal编程技术网

如何使用while循环,for循环替换此do while循环。 import javax.swing.JOptionPane; 公共类样本1{ 公共静态void main(字符串[]args){ 双延迟; 国际日; 字符串标准容差,标准日期; 做{ strdAmount=JOptionPane.showInputDialog(空,“请输入每日津贴$:(>0)”, “津贴”,作业窗格。信息(信息); dAllowance=Double.parseDouble(标准容差); if(dAllowance

如何使用while循环,for循环替换此do while循环。 import javax.swing.JOptionPane; 公共类样本1{ 公共静态void main(字符串[]args){ 双延迟; 国际日; 字符串标准容差,标准日期; 做{ strdAmount=JOptionPane.showInputDialog(空,“请输入每日津贴$:(>0)”, “津贴”,作业窗格。信息(信息); dAllowance=Double.parseDouble(标准容差); if(dAllowance,java,Java,我已经将第一个do while循环转换为for循环,代码如下 编辑: 我认为下面的版本比上一个版本稍微好一点: import javax.swing.JOptionPane; public class SampleLT1 { public static void main(String[] args) { double dAllowance; int days; String strDAllowance, strDays;

我已经将第一个do while循环转换为for循环,代码如下

编辑:

我认为下面的版本比上一个版本稍微好一点:

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
    }
    }
import javax.swing.JOptionPane;
公共类样本1{
公共静态void main(字符串[]args){
双dAllowance=0;
国际日;
字符串标准容差,标准日期;
对于(inti=-1;i0)i++;
}//如果每日津贴小于0,请重新输入
做{

strDays=JOptionPane.showInputDialog(null,“请输入工作日数:(>0和0,并且您不需要在for循环中输入任何内容。完全空的for循环
for(;)
while(true)
相同。任何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
    }
    }
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);
}
如果你真的想深入研究“什么是有效的循环逻辑”,请考虑下面的内容:

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(
无效的
“请输入每日津贴$:(>0)”,
“津贴”,
JOptionPane.INFORMATION(消息)
) ) < 0) {
System.out.println(“每日津贴应大于0,请重新输入”);
}
如果(dAllowance==0){
System.out.println(
“感谢您使用津贴计算应用程序。”);
系统出口(0);
}
请注意,为了可读性,我只在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
    }
    }
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);
}
用于(;
(dAllowance=Double.parseDouble(
JOptionPane.showInputDialog(
无效的
“请输入每日津贴$:(>0)”,
“津贴”,
JOptionPane.INFORMATION(消息)
) ) < 0;
System.out.println(“每日津贴应大于0,请重新输入”)

);//循环的
是如何工作的?涉及哪些组件?可能会有帮助。我建议你用一些简单的例子(例如打印出数字)来处理循环。它们不难理解,但所有Swing代码附带的复杂性使“玩”更难“用你的例子。啊,非常感谢!在再次尝试之前,我会尝试其他循环的例子!非常感谢!虽然我仍然不确定循环,但我会尽量吸收!没问题。我已经编辑了我认为更好的答案。这是(;I<10;)的
很有用:我不知道你能做到这一点。(+1)。@KnockFall没问题。如果问题已经解决,一定要标记它。:@bluesh34确实,你可以或多或少地将任何你想要的东西放入for循环中。
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);
}
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