为什么当我执行我的 Java 程序时,它不转换第一个用户输入,而是转换每个用户输入?

Why when I execute my Java program does it not convert the first user input, but does every user input after?

我正在尝试编写一个 java 货币转换程序作为 class 作业。我的代码几乎完全按照我希望的方式工作。执行时,它会要求用户输入日元对美元的汇率。输入任何数字,它会正常工作。

但是,接下来它会提示用户输入他们想要兑换成日元的美元金额。同样,我们的想法是输入任何大于 0 的数字,因为如果用户输入 0,标记值将强制退出并生成成功消息。当您输入一个数字并单击 'Enter,' 时,它只显示一个空行,并没有像我想要的那样进行转换。

但是,如果您在下一个空白行输入一个数字,它就会进行转换!您甚至可以输入带空格的数字:10 20 50 100...它会将所有数字转换成不同的行。

我只是想弄清楚如何摆脱它给出的第一条黑线并直接进入转换...或其他一些解决方法。

这是我的代码:

// Import scanner Object
import java.util.Scanner;
// Import text Object for formatting
import java.text.DecimalFormat;
/**
 *
 * @author dcraw
 */
public class CurrencyConversion {
    public static void main (String [] args) 
    {
        // Identify Scanner 
        Scanner input = new Scanner(System.in);
        
        // Declare variables
        double USDtoJPY; //JPY stands for Japanese Yen
        double USD;// USD stands for US Dollar
        int count = 0;
        
        // Formatting input
        DecimalFormat f = new DecimalFormat ("#,###.##");
        
        // Ask user to input exchange rate from JPY to USD
        System.out.println("What is today's price in JPY for 1 USD?"); // As of 6/6/2021, 1 USD = 109.55 JPY
        USDtoJPY = input.nextDouble();
        
        // Ask user to input how many USD dollars they want to convert
        System.out.println("Enter amount in USD to convert: (0 to Quit)"); 
        USD = input.nextDouble();
        
        // Process Data until sentinel is entered
        while (USD != 0)
        {
            USD = input.nextDouble();
                count++;
        
            if (count > 0)
            {
                double JPY = USDtoJPY * USD; // Perform calculation with exchange rate
                System.out.println("Your " + USD + " dollars is converted to " + f.format(JPY) + " yen");
            }
            else
            {
                System.out.println("Incorrect Data"); // If data is entered incorrectly print Incorrect data
            }
        }
    } 
}


// Why do I have to input something to get it to start the loop? 

您应该将 while (USD != 0) 循环内的 USD = input.nextDouble(); 移动到循环的末尾。当前,您在第一个输出之前接受两个输入。如果将它移到最后,它会按预期工作。

// Import scanner Object
import java.util.Scanner;
// Import text Object for formatting
import java.text.DecimalFormat;
/**
 *
 * @author dcraw
 */
public class CurrencyConversion {
    public static void main (String [] args) 
    {
        // Identify Scanner 
        Scanner input = new Scanner(System.in);
        
        // Declare variables
        double USDtoJPY; //JPY stands for Japanese Yen
        double USD;// USD stands for US Dollar
        int count = 0;
        
        // Formatting input
        DecimalFormat f = new DecimalFormat ("#,###.##");
        
        // Ask user to input exchange rate from JPY to USD
        System.out.println("What is today's price in JPY for 1 USD?"); // As of 6/6/2021, 1 USD = 109.55 JPY
        USDtoJPY = input.nextDouble();
        
        // Ask user to input how many USD dollars they want to convert
        System.out.println("Enter amount in USD to convert: (0 to Quit)"); 
        USD = input.nextDouble();
        
        // Process Data until sentinel is entered
        while (USD != 0)
        {
            count++;
        
            if (count > 0)
            {
                double JPY = USDtoJPY * USD; // Perform calculation with exchange rate
                System.out.println("Your " + USD + " dollars is converted to " + f.format(JPY) + " yen");
            }
            else
            {
                System.out.println("Incorrect Data"); // If data is entered incorrectly print Incorrect data
            }
            USD = input.nextDouble(); // move this here
        }
    } 
}

或者更好的是,您可以改用 do while 并仅在循环内输入:

import java.util.Scanner;
import java.text.DecimalFormat;
/**
 *
 * @author dcraw
 */
public class DummyClass {
  public static void main (String [] args)
  {
    // Identify Scanner
    Scanner input = new Scanner(System.in);

    // Declare variables
    double USDtoJPY; //JPY stands for Japanese Yen
    double USD;// USD stands for US Dollar

    // Formatting input
    DecimalFormat f = new DecimalFormat ("#,###.##");

    // Ask user to input exchange rate from JPY to USD
    System.out.println("What is today's price in JPY for 1 USD?"); // As of 6/6/2021, 1 USD = 109.55 JPY
    USDtoJPY = input.nextDouble();

    // Ask user to input how many USD dollars they want to convert
    System.out.println("Enter amount in USD to convert: (0 to Quit)");

    // Process Data until sentinel is entered
    do {
      USD = input.nextDouble();

      if (USD > 0)
      {
        double JPY = USDtoJPY * USD; // Perform calculation with exchange rate
        System.out.println("Your " + USD + " dollars is converted to " + f.format(JPY) + " yen");
      }
      else
      {
        System.out.println("Incorrect Data"); // If data is entered incorrectly print Incorrect data
      }
    } while (USD != 0);
  }
}

您要求输入两次,这就是为什么您必须按两次回车键。 一次在循环之前 input.nextDouble() 一次在循环开始时。