我想程序重新做while循環,當它捕獲異常 - 例外是接收數字零。相反,它使用下面的代碼繼續一個while循環,我希望它再次詢問用戶輸入,直到用戶輸入一個不同於零的數字。嘗試捕獲異常,而循環
import java.util.InputMismatchException;
import java.util.Scanner;
public class whilePerjashtim {
public static int division(int a, int b){
return a/b;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a, b;
System.out.println("Enter a value: ");
a = s.nextInt();
while(true){
try
{
System.out.println("Enter b value");
b = s.nextInt();
System.out.println("Sum of division is: " + division(a,b));
}
catch(ArithmeticException e)
{
System.err.println("Don't divide by zero!!!");
}
catch (java.util.InputMismatchException e)
{
System.err.println("Enter just a Number!!!");
}
finally
{
System.out.println();
}
}
}
}
你如何_break_出循環?你如何繼續_循環? –
你有沒有試過繼續使用! – vishram0709
首先,你只需要服用一次,如果這是你想要的就可以了,但除非有例外,否則沒有任何中斷條件。 –