我无法让扫描仪等待用户输入。我的问题出在以下几行代码中:
System.out.println("Enter the day of the month: ");
newOnetime.setDay(scan.nextInt());
scan.nextLine();
System.out.println("Enter the year: ");
newOnetime.setYear(scan.nextInt());
我无法让用户输入年份,因为扫描仪只是跳过它。这是我的控制台输出从调试中看起来的样子:
按1进行新的一次约会(假设我按1)
输入约会的说明: 牙医访问
输入约会月份: 3
输入当月的日期:
输入年份:
^^就在那里,用户无法放入年份,因为控制台假设该数字适用于日期和年份。
如果这是一个简单的解决方案,我道歉。我不是扫描仪的最大粉丝,通常我会被细微的差别所绊倒。谢谢!
这是我的程序的驱动程序: 包预约;
import java.util.Scanner;
import Appointment.Appointment;
import Appointment.Daily;
import Appointment.Monthly;
import Appointment.Onetime;
public class AppointmentBookDriver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Press 1 for new one time appointment");
Onetime.createOneTime(scan);
}
这是一次性课程: 包预约;
import java.util.Scanner;
public class Onetime extends Appointment{
public Onetime(String description, int month, int day, int year) {
super(description, month, day, year);
}
/**
* this method creates a new onetime appt. I realize it is a lengthy
* method but it is necessary and I couldn't logically break it into
* smaller methods.
* @param scan the scanner that is passed from the driver class
*/
public static void createOneTime(Scanner scan) {
Onetime newOnetime = new Onetime(null, 0, 0, 0);
System.out.println("Enter a description of the appointment: ");
newOnetime.setDescription(scan.nextLine());
System.out.println("Enter the month of your appointment: ");
if (scan.hasNextInt()) {
int month = 0;
if (month > 0 && month < 13) {
month = scan.nextInt();
}
} else if (scan.hasNextLine()){
String monthString = scan.next().toLowerCase();
switch (monthString) {
case "january": newOnetime.setMonth(1); break;
case "february": newOnetime.setMonth(2); break;
case "march": newOnetime.setMonth(3); break;
case "april": newOnetime.setMonth(4); break;
case "may": newOnetime.setMonth(5); break;
case "june": newOnetime.setMonth(6); break;
case "july": newOnetime.setMonth(7); break;
case "august": newOnetime.setMonth(8); break;
case "september": newOnetime.setMonth(9); break;
case "october": newOnetime.setMonth(10); break;
case "november": newOnetime.setMonth(11); break;
case "december": newOnetime.setMonth(12); break;
default:
{System.out.println("Please enter an integer representing a month...");
newOnetime.setMonth(scan.nextInt());
}
}
}
System.out.println("Enter the day of the month: ");
newOnetime.setDay(scan.nextInt());
scan.nextLine();
System.out.println("Enter the year: ");
newOnetime.setYear(scan.nextInt());
scan.nextLine();
}
}
答案 0 :(得分:0)
我只能说这对我来说很好用:
import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the day of the month: ");
System.out.println("You typed: " + scan.nextInt());
System.out.println("Enter the year: ");
System.out.println("You typed: " + scan.nextInt());
}
}
Scanner
每次都会阻止,并等待来自System.in
的输入。
答案 1 :(得分:0)
我决定取消我的switch语句,只强制用户输入为1-12(含)的数字。这样做可以让我保持用户输入的最高点,而且我不必担心凌乱的慢速切换。感谢您的投入,但我决定以不同的方式设计该程序!对不起,如果我浪费了任何人的时间。