我正在尝试使用try
and catch
。如果输入的输入无效,则循环会重复并再次要求用户输入,但它不起作用。当我输入错误时,它只会重复System.out.println
.
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 x = 1;
int month, day, year;
do {
try {
System.out.println("Please enter a month MM: ");
month = scan.nextInt();
if(month>12 && month<1)
{
System.out.println("FLOP");
}
x=2;
}
catch(Exception e){
System.out.println("not today mate");
}
}
while(x==1);
}
}