嗨,我试图创建一个让用户输入日期的代码。然后,我将操纵这个日期来计算每天的旅行费用。我正在努力添加异常以防止输入错误。谁能给我一些关于如何做到这一点的提示?我的代码:
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Price
{
public static void main (String [] args)
{
userInput();
}
public static void userInput()
{
Scanner scan = new Scanner(System.in);
int month, day, year;
System.out.println("Please enter a month MM: ");
month = scan.nextInt();
System.out.println("Please enter a day DD: ");
day = scan.nextInt();
System.out.println("Please enter a year YYYY: ");
year = scan.nextInt();
System.out.println("You chose: " + month + " /" + day + " /" + year);
}
}