Dies ist das ProgrammWie funktioniert input.nextInt() genau?
public class bInputMismathcExceptionDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean continueInput = true;
do {
try {
System.out.println("Enter an integer:");
int num = input.nextInt();
System.out.println("the number is " + num);
continueInput = false;
}
catch (InputMismatchException ex) {
System.out.println("Try again. (Incorrect input: an integer is required)");
}
input.nextLine();
}
while (continueInput);
}
}
Ich weiß nextInt()
nur die ganze Zahl nicht lesen die "\n"
, aber warum sollten wir brauchen die input.nextLine()
die "\n"
zu lesen? ist es nötig?? weil ich denke, auch ohne input.nextLine()
, nachdem es zurückgeht auf try {}
, die input.nextInt()
kann immer noch die nächste Ganzzahl ich Typ lesen, aber in Wirklichkeit ist es eine Endlosschleife.
Ich weiß immer noch nicht die Logik dahinter, hoffe jemand kann mir helfen.
lesen Sie diese http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html –