随机输入数字时,Exception问题 需要注意的是:nextInt(3) 所返回的值,范围是 0~2 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我把Crow = rand.nextInt(3)-1; Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1; Ccol = rand.nextInt(4)-1;还是会抛出异常Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦 这个你debug 调试看看就明白了。 我把Crow = rand.nextInt(3)-1; Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1; Ccol = rand.nextInt(4)-1;还是会抛出异常Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题 rand.nextInt(3) +1 也会有异常。我试了rand.nextInt(2)+1, 就没有异常发生了。但是不知道为什么13 和 27行会出现Exception in thread "main" java.lang.StackOverflowError at java.util.Random.nextInt(Unknown Source)请问你知道如何修改吗?谢啦 我把Crow = rand.nextInt(3)-1; Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1; Ccol = rand.nextInt(4)-1;还是会抛出异常Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题楼主是刚开始学java吧,你可以去看下API,nextInt(n)返回是0~(n-1)的一个随机数!所以你取到的数组下标为-1的时候会报异常。 我知道了,应该写成nextInt(3)就可以了,谢啦~ 我把Crow = rand.nextInt(3)-1; Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1; Ccol = rand.nextInt(4)-1;还是会抛出异常Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题楼主是刚开始学java吧,你可以去看下API,nextInt(n)返回是0~(n-1)的一个随机数!所以你取到的数组下标为-1的时候会报异常。谢谢你,问题基本解决了~ 关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n)关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1) 关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n)关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)恩,是的。如果后面有+1,就永远取不到0,如果有-1, 就会异常。所以不用+1或是-1.谢谢你啦 关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n)关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)我试了一下,写不写try catch都可以执行。那我throws exception吗? Thanks 为啥要try catch?没理解你的意图 我的意思是,已经规定了随机输入的范围,就可以不用throws exception了吧? 只要控制好随机数的范围,就可以不用。Random rand = new Random();Crow = rand.nextInt(ChessPad.board.length)+1;Ccol = rand.nextInt(ChessPad.board[0].length)+1; quarz 调度问题 Eclipse有哪些常用插件方便用来看项目代码的? 面试题,求解 求最优算法---关于字符串数组的问题 碰到一个奇怪的eclipse问题 救命啊~~如何在命令行程序中定义热键! java GUI 高手们求帮助 渲染一个Jtree 包含jprogessBar +图片+文字 请教JAVA达人 could not load the Java virtual machine.program will exit! java试题 求解 nginx或LVS做负载均衡,其session如何共享? json转换 死循环 sqlserver 不支持定位的更新和删除
Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1;
Ccol = rand.nextInt(4)-1;还是会抛出异常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦
Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1;
Ccol = rand.nextInt(4)-1;还是会抛出异常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦
nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题
rand.nextInt(3) +1 也会有异常。我试了rand.nextInt(2)+1, 就没有异常发生了。
但是不知道为什么13 和 27行会出现
Exception in thread "main" java.lang.StackOverflowError
at java.util.Random.nextInt(Unknown Source)请问你知道如何修改吗?谢啦
Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1;
Ccol = rand.nextInt(4)-1;还是会抛出异常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦
nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题楼主是刚开始学java吧,你可以去看下API,nextInt(n)返回是0~(n-1)的一个随机数!所以你取到的数组下标为-1的时候会报异常。
Ccol = rand.nextInt(3)-1;改成 Crow = rand.nextInt(4)-1;
Ccol = rand.nextInt(4)-1;还是会抛出异常
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at ComputerPlay.computerPlay(ComputerPlay.java:22)是不是我写的try catch 不对啊~谢啦
nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题楼主是刚开始学java吧,你可以去看下API,nextInt(n)返回是0~(n-1)的一个随机数!所以你取到的数组下标为-1的时候会报异常。谢谢你,问题基本解决了~
关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n)关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)
关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n)关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)恩,是的。如果后面有+1,就永远取不到0,如果有-1, 就会异常。所以不用+1或是-1.
谢谢你啦
关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n)关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)我试了一下,写不写try catch都可以执行。
那我throws exception吗? Thanks
我的意思是,已经规定了随机输入的范围,就可以不用throws exception了吧?
Random rand = new Random();
Crow = rand.nextInt(ChessPad.board.length)+1;
Ccol = rand.nextInt(ChessPad.board[0].length)+1;