java - 无法使用扫描仪接受多个输入
问题描述
我正在编写 java 代码以将来自用户的输入分配给三个变量(类型为 double、string、int)。我为此使用了扫描仪。它编译并运行完美,没有任何错误。
我的问题:
我只能向 double 类型的变量(第一个变量)输入一个。我无法为其他两个变量输入值。你能解释为什么会发生这种情况并为我解决这个问题吗?
public class Main {
public static void main(String[] args) {
double revenueTill;
String month;
int dayOfMonth;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter revenue until now: ");
revenueTill = scanner.nextDouble();
System.out.println("Enter month: ");
month = scanner.nextLine();
System.out.println("Enter day of month: ");
dayOfMonth = scanner.nextInt();
MonthPrediction monthPrediction = new MonthPrediction(revenueTill, month, dayOfMonth);
monthPrediction.showRevenueOfMonth(revenueTill, month, dayOfMonth);
}
}
解决方案
尝试将 nextLine() 更改为 next():
System.out.println("Enter month: ");
month = scanner.next();
System.out.println("Enter day of month: ");
dayOfMonth = scanner.nextInt();
推荐阅读
- python - slice(-4, 4, 0.25) 的确切含义是什么?
- java - Proguard 配置问题
- python - Heroku 部署的 django 站点出现应用程序错误
- ruby-on-rails - Rails 序列化器协会
- mysql - MySQL 将日期格式从“2018 年 7 月 5 日星期四”更改为“2018-07-05”
- bash - Mono WinFroms 应用程序不会在启动时运行
- ruby-on-rails - 在 Heroku 上使用 Action Mailer 安排电子邮件
- python - 如何在循环中更新 Python Pandas 数据框内容?
- java - 如何在 MockMvc 测试期间使用正确的 Spring Security 配置
- r - 为向量中的每个值分配一个负号(在 R 中)?