我正在尝试制作一个 try/catch 程序计算器,它可以消除以下错误,但它不起作用。请告诉我我做错了什么。我应该测试非法操作数运算和除以零。
do // do while is for many operations
{
try
{
System.out.println("Enter num1 and num2 : ");
int num1 = new Scanner();
int num2 = new Scanner();
System.out.println("Enter sign : ");
char sign = new Scanner();
}
catch(IllegalArgumentException ex) // This will check for illegal inputs
{
System.out.println(ex.Message()); //Will print error message
}
if(sign == '+')
{ // part of code where you will write the plus operation
System.out.println(num1+num2);
}
if(sign == '-')
{
System.out.println(num1-num2);
}
if(sign == '*')
{
System.out.println(num1*num2);
}
if(sign == '/')
{
try
{
System.out.println(num1/num2);
}
catch(ArithmeticException ex)// Check for divide by zero exception
{
System.out.println("Divide by zero");
}
}
if(sign == 'x') // to exit
{
flag = false
}
else
{
System.out.println("Error : Unknown Operator\n");
}
}while(flag == true) // flag will be true until user enters 'x' as the choice to exit.