ฉันมี do-while
ลูปที่ไม่ทำงาน เมื่อฉันป้อน String แทนที่จะเป็น int มันควรจะพูดว่า "bla" และขอให้ใส่ตัวเลขอีกครั้ง แต่จะส่งข้อความซ้ำแล้วซ้ำอีก มีอะไรผิดปกติในรหัสนี้?
boolean i = true;
do {
i = false;
try {
System.out.println("insert number");
int k = sc.nextInt();
}
catch(InputMismatchException e) {
System.out.println("test");
i = true;
}
} while ( i== true);
When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
. ดังนั้น คุณต้องข้ามโทเค็นเมื่อคุณได้รับInputMismatchException
- person Andreas Fester   schedule 01.07.2013