关于异常监控字符的问题
//下面的代码,加上异常处理,使输入的数不为数字时,给出提示,并再次输入问题是我想当我输入a字符串时与输入b字符串是都能捕捉,并能保证在循环异常的情况,直到最后正确为止。import javax.swing.*;
public class Test{
public static void main(String[] args) {
int a =0, b = 0, result = 0;
String stra, strb;
stra = JOptionPane.showInputDialog("请输入一个被除数");
strb = JOptionPane.showInputDialog("请输入一个除数");
while (true) {
try {
if(true){
a = Integer.parseInt(stra);
}else if(true){
b = Integer.parseInt(strb);
}else if(true){
result = a / b;
break;
}
}
catch (ArithmeticException e)
{
strb = JOptionPane.showInputDialog("您刚才输入的除数是零,请重新输入");
b = Integer.parseInt(strb);
}
catch(NumberFormatException ee)
{
strb = JOptionPane.showInputDialog("您刚才输入的除数是字母,请重新输入");
b = Integer.parseInt(strb);
}
}
result = a / b;
JOptionPane.showMessageDialog(null, a + "/" + b + "=" + result);
}
}