⑴ java 怎麼判斷除數是否為零
除數為0了就不該有返回值了,應該手動拋出異常!
publicclassManager{
publicdoublediv(doublex,doubley)
{
try{
if(y==0)
{
thrownewArithmeticException();
}
else
returnx/y;
}catch(ArithmeticExceptionae)
{
ae.printStackTrace();
System.out.println("除數為0");
}
returnx/y;
}
publicstaticvoidmain(Stringar[])
{
Managerm=newManager();
System.out.println(m.div(10,0));
}
}
運行結果: