java - 为什么当我执行我的 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?
解决方案
您应该将循环的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);
}
}
推荐阅读
- mongodb - 必须有一个选择的子字段。您的意思是“createEvent { ... }\”吗?”,[graphql]
- ios - 如何在使用照片框架从照片中选择图像时在选择器中隐藏 iCloud 图像
- java - 文本到语音以静音(长时间停顿)保存到音频文件
- powershell - PRTG 传感器显示“更改登录/q”是否为真/假 - 批处理文件/Powershell 文件
- coq - 在鸽巢原理的证明中应该如何使用排中?
- python - 从对象转换后有没有办法解决错误的日期?
- c++ - 关于在csv中写入文件
- visual-studio - Microsoft Visual Studio IDE 建立在哪些技术之上?
- c# - 如何在代码中获取显示在 Adobe Reader 中的证书存储?
- swift - 如何在 UITabBar 顶部创建圆角