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

时间:2021-06-09 03:54:08

标签: java if-statement while-loop user-input sentinel

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

但是,它会提示用户接下来输入他们想要转换为日元的美元金额。同样,这个想法是输入任何大于 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? 

2 个答案:

答案 0 :(得分:0)

您应该将 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);
  }
}

答案 1 :(得分:0)

您要求输入两次,这就是您必须按两次 Enter 的原因。 一次在循环 input.nextDouble() 之前,一次在循环开始处。

相关问题