首页 > 解决方案 > 为什么当我执行我的 Java 程序时,它不会转换第一个用户输入,而是每个用户输入之后?

问题描述

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

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

但是,如果您在下一个空白行输入一个数字,那么它就会进行转换!您甚至可以输入带空格的数字: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? 

标签: javaif-statementwhile-loopuser-inputsentinel

解决方案


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

// 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);
  }
}

推荐阅读