Java 我不断得到一个无限的do-while循环
我写了一个可以捕捉用户输入错误的程序 当我试图让我的程序执行“do over”(使用do while循环)时,它会遇到错误。程序(而不是通过扫描仪重新启动用户输入)将反复打印异常捕获块中的Java 我不断得到一个无限的do-while循环,java,infinite-loop,do-while,Java,Infinite Loop,Do While,我写了一个可以捕捉用户输入错误的程序 当我试图让我的程序执行“do over”(使用do while循环)时,它会遇到错误。程序(而不是通过扫描仪重新启动用户输入)将反复打印异常捕获块中的System.out.println行 下面是我的代码,我真的希望有人能帮我 int x = 0; int y = 0; int Temp = 0; int Rest = 0 ; int Works = 1; Scanner s = new Scanner(Sy
System.out.println
行
下面是我的代码,我真的希望有人能帮我
int x = 0;
int y = 0;
int Temp = 0;
int Rest = 0 ;
int Works = 1;
Scanner s = new Scanner(System.in);
do{
try{
System.out.println("Bitte geben sie zwei Nummern ein für den ggT.");
System.out.print("Die erste Nummer: ");
x = s.nextInt();
System.out.print("Die zweite Nummer: ");
y = s.nextInt();
if (x < y){
Temp = x;
x = y;
y = Temp;
}System.out.println("Berechnung des ggT von " +x +" und " +y +".");
do{
System.out.println("Rechne " +x +" mod " +y +": ");
Rest = x % y;
if (Rest != 0){
x = y;
y = Rest;
}
}while (Rest !=0);
System.out.println("Der größte gemeinsame Teiler lautet: " + y);
Works=2;
}
catch(Exception e){
System.out.println("Sie dürfen nicht durch Null (0) teilen.");
System.out.println("Sie dürfen keine Dezimalzahlen benutzen.");
System.out.println("Sie dürfen keine Zeichen benutzen.");
System.out.println("Versuchen wir es noch einmal :)");
}
}while(Works==1);
}
一遍又一遍。而不是等待用户输入
我希望德语和我的代码不会让人讨厌 在catch块中的扫描仪上调用nextLine()方法,尝试将扫描仪前进到下一行。这将清除输入
catch(Exception e){
System.out.println("Sie dürfen nicht durch Null (0) teilen.");
System.out.println("Sie dürfen keine Dezimalzahlen benutzen.");
System.out.println("Sie dürfen keine Zeichen benutzen.");
System.out.println("Versuchen wir es noch einmal :)");
s.nextLine(); // Advance Scanner to the next line
}
通过在catch块中的扫描仪上调用nextLine()方法,尝试将扫描仪前进到下一行。这将清除输入
catch(Exception e){
System.out.println("Sie dürfen nicht durch Null (0) teilen.");
System.out.println("Sie dürfen keine Dezimalzahlen benutzen.");
System.out.println("Sie dürfen keine Zeichen benutzen.");
System.out.println("Versuchen wir es noch einmal :)");
s.nextLine(); // Advance Scanner to the next line
}
javascript在哪里?你在使用什么类型的输入错误?可能重复的javascript在哪里?你在使用什么类型的输入错误?可能重复的非常感谢:)这已经解决了我的问题,我会更新你的答案,但我缺乏这样做的业力。所以我不得不再次说声谢谢,这让我昨天发疯了!非常感谢:)这已经解决了我的问题,我会投票给你的答案,但我缺乏这样做的业力。所以我不得不再次说声谢谢,这让我昨天发疯了!