【问题标题】:How to convert a do while loop to a while loop如何将do while循环转换为while循环
【发布时间】:2014-09-15 23:01:44
【问题描述】:

如何将我的 do-while 循环转换为 while 循环?

int numAttempts = 0;

do
{
    System.out.println("Do you want to convert to Peso or Yen?");
    pesoOrYen = readKeyboard.nextLine();

    if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen"))
    {
        notPesoOrYen = false;
    }
    else if (numAttempts < 2)
    {
        System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type. Try again:");
        notPesoOrYen = true; 
    }

    numAttempts++;

} while(notPesoOrYen==true && numAttempts < 3);

我试图做while(notPesoOrYen==true &amp;&amp; numAttempts &lt; 3) 然后声明但它不起作用。

我的完整代码

打包货币转换器;

导入 java.util.Scanner; 导入 java.text.NumberFormat;

公共类 CurrencyConverter {

public static void main(String[] args) 
{
    Scanner readKeyboard = new Scanner(System.in);

    double doubleUsersCaptial;
    boolean notPesoOrYen=true;
    String pesoOrYen;
    double usersConvertedCapital;
    boolean userInputToRunProgramAgain=true;

    final double US_DOLLAR_TO_PESO = 13.14;
    final double US_DOLLAR_TO_YEN  = 106.02;


    do
    {

        System.out.println    ("How much money in US dollars do you have?");
        String usersCaptial    = readKeyboard.nextLine();
        doubleUsersCaptial = Double.parseDouble(usersCaptial);

        int numAttempts = 0;

        do
        {
            System.out.println    ("Do you want to convert to Peso or Yen?");
            pesoOrYen      = readKeyboard.nextLine();



            if(pesoOrYen.equalsIgnoreCase("Peso")||pesoOrYen.equalsIgnoreCase("Yen"))
            {
                notPesoOrYen = false;
            }
            else if (numAttempts < 2)
            {
                System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type. Try again:");
                notPesoOrYen = true; 

            }
        numAttempts++;
        }while(notPesoOrYen==true && numAttempts < 3);

        if(numAttempts==3)
        {
            System.out.println("Sorry, but '"+pesoOrYen+"' is not a valid currency type.");
            System.out.println("You entered the wrong currency type too many times\nGood Bye");
            System.exit(0); 
        }

        if (pesoOrYen.equalsIgnoreCase("Peso"))
        {
            usersConvertedCapital = doubleUsersCaptial*US_DOLLAR_TO_PESO;
        }
        else 
        {
            usersConvertedCapital = doubleUsersCaptial*US_DOLLAR_TO_YEN;
        }


        NumberFormat formatter     = NumberFormat.getCurrencyInstance();
        String formatUsersCaptial = formatter.format(doubleUsersCaptial);
        String formatUsersConvertedCapital  = formatter.format(usersConvertedCapital);


        System.out.println(formatUsersCaptial+"US Dollars = "
                          +formatUsersConvertedCapital+" "+pesoOrYen);
        System.out.println("Would you like to run the Program Again?(enter 'yes' or 'no')");
        String runProgramAgain = readKeyboard.nextLine();


        if (runProgramAgain.equalsIgnoreCase("yes"))
        {
            userInputToRunProgramAgain = true;
        }
        else if (runProgramAgain.equalsIgnoreCase("no"))
        {
            System.out.println("Goood Bye");
            System.exit(0);    
        }

        else
        {
            System.out.println ("You entered something other than 'yes' or 'no'\n"
                               +"Good Bye");
            System.exit(0);
        }
    }while (userInputToRunProgramAgain==true);
}

}

【问题讨论】:

  • 您尝试以不同方式重写此循环的任何特殊原因?
  • @Herman - 你知道while 循环和do/while 循环之间的区别吗?
  • 这是针对我的 Java 类的,我的教授要求使用 while 循环而不是 do-while。
  • 您的 while 条件看起来正确。确保在此之前“notPesoOrYen==true”

标签: java loops while-loop do-while


【解决方案1】:

whiledo... while 几乎相同,do... while 只是在第一次评估退出条件之前执行一次迭代,而 while 甚至在第一次迭代时也会评估它(所以最终while 循环永远不会成为到达者,而 do... while 主体将始终至少执行一次)。

您的代码 sn-p 不完整,但我猜您没有在循环之前将 notPesoOrYen 初始化为 true,这就是它不起作用的原因。最后,不要写while(notPesoOrYen==true &amp;&amp; numAttempts &lt; 3)而是while(notPesoOrYen &amp;&amp; numAttempts &lt; 3)== true比较是不必要的。

【讨论】:

  • 我试过了,运行程序一次后它不会让用户输入另一种货币类型
  • 嗯,whiledo while 之间的唯一区别是第一次迭代,所以如果一开始连续条件为真(这是你的情况),它们是严格等价的。
【解决方案2】:

在while循环外初始化你的布尔变量:

int numAttempts = 0;
boolean notPesoOrYen=true;
while (notPesoOrYen && numAttempts < 3) {
    System.out.println("Do you want to convert to Peso or Yen?");
    pesoOrYen = readKeyboard.nextLine();

    if (pesoOrYen.equalsIgnoreCase("Peso") || pesoOrYen.equalsIgnoreCase("Yen")) {
        notPesoOrYen = false;
    } else if (numAttempts < 2) {
        System.out.println("Sorry, but '" + pesoOrYen + "' is not a valid currency type. Try again:");
        notPesoOrYen = true; 
    }
    ++numAttempts;
};

【讨论】:

    猜你喜欢
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2012-01-18
    • 2018-11-21
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多