[问题] 有丢出 不能负数的Exception吗??

楼主: akk98 (akk)   2020-03-27 19:55:10
比如说 费伯纳西数列 要只能输入正数
public static int fib(int n) throws MyException {
if(n<=-1) {
throw new MyException("需要正数");
}else if(n==1) {
return 1;
}else if(n==0) {
return 0;
}else {
return fib(n-1)+fib(n-2);
}
}
有其他的方法 需要只写在方法里面的话 不知道有没有类似的用法呢?
错误讯息
No enclosing instance of type Trn1 is accessible. Must qualify the allocation
with an enclosing instance of type Trn1 (e.g. x.new A()
where x is an instance of Trn1).
作者: easterday (....)   2020-03-27 20:36:00
通常我觉得用RuntimeException就够用了吧
作者: gasbomb (虚空雷神兽)   2020-03-27 20:38:00
讲究一点就ArithmeticException
作者: ssccg (23)   2020-03-27 21:34:00
ArithmeticException比较合理,不过我会一律用IllegalArgumentException,比较常用...除非是一整套就是数学类的library
楼主: akk98 (akk)   2020-03-28 14:27:00
请教大大 Arithmetic 这个可以直接throw吗?可以类似这样吗?ArithmeticException("中文")
作者: gasbomb (虚空雷神兽)   2020-03-31 00:12:00
你何不自己试试 推文的时间都够你写程式-编译-执行了 真的有问题的话上来发问相信大家会很乐意回答你的
楼主: akk98 (akk)   2020-03-31 00:39:00
谢谢回复 但就是没有这种方式 所以才会问 感恩
作者: swallowcc (guest)   2020-03-31 09:59:00
你要的是这样嘛? https://i.imgur.com/sDyRtDt.png
作者: qrtt1 (有些事,有时候。。。)   2020-03-31 11:23:00
就是 swallowcc 示范的那样,gasbomb 没有的情况是什么?

Links booklink

Contact Us: admin [ a t ] ucptt.com