当前位置: 首页 > 知识库问答 >
问题:

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

董建茗
2023-03-14

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

但是,它会提示用户输入他们想要兑换成日元的美元金额。同样,这个想法是输入任何

但如果你在下一个空行输入一个数字,它就会进行转换!你甚至可以用空格输入数字: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个答案

杨海
2023-03-14

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

呼延修然
2023-03-14

您应该将USD=input.nextDouble();内的同时(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而不是在循环中输入:

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);
  }
}
 类似资料: