我只是从Java入手,对于我的入门课程,必须创建前哨值为0的循环才能将输入的USD转换为GBP。有一些运行时错误,看来我是在无限循环中创建的。有人可以帮忙吗?提前致谢!

import java.util.Scanner;
/**
 * Prompts user for today's conversion of USD to GBP
 * Then prompts user for USD value, which will inform user of GBP
 * Prompting loop for other USD values, util sentinel value of (0) is encountered
 * hint: while loop shouldnt include prompt for exchange rate. When user enteres 0, program will print terminating "goodbye" message and end
 */
public class AP6_DollarToPound
{
   public static void main(String[] args)
   {
       Scanner in = new Scanner(System.in);

       double amount = 0;
       int rate = in.nextInt();
       int value = in.nextInt();

       System.out.println("Enter today's US Dollar to British Pound Sterling exchange rate: ");
       System.out.println("Enter a value in US Dollars: ");

       while (value != 0);
       {
           value = in.nextInt();
           if (value != 0)
           {
               amount = rate * value;
               System.out.println("Value in GBP is: " + value);
            }
            else
            {
                System.out.println("Goodbye!");
            }
        }
        }
    }

最佳答案

你的while循环是错误的。

while (value != 0);


这意味着在值不为零时进行迭代。在这种情况下,它始终不为零。

您应该执行以下操作:

while (value != 0)
{
    // Change value as you like
}

10-07 13:21