随机输入数字时,Exception问题

Sally_simple 2013-12-23 02:13:58
import java.io.IOException;
import java.util.Random;


public class ComputerPlay {

public int Crow;
public int Ccol;

public void computerPlay() throws Exception{
try{
Random rand = new Random();
Crow = rand.nextInt(3)-1;
Ccol = rand.nextInt(3)-1;


} catch (ArrayIndexOutOfBoundsException e){
computerPlay();
}

//computerPlay();
if(ChessPad.board[Crow][Ccol] == ChessPad.EMPTY){
ChessPad.board[Crow][Ccol] = ChessPad.CROSS;

}
}

}


这段代码需要实现的是: Crow, Ccol是两个电脑随机输入的数字, 取值范围是1-3.判断, 如果board[Crow][Ccol]是empty(int 0), 则把Cross (int 2)赋给 board[Crow][Ccol]. 如果board[Crow][Ccol]的值不为empty, 则再从新随机输入数字, 找到一个空的board[][]并赋给2.
注:初始值都是empty(int 0);

用这段代码实现,如果当前随机输入值的board不为空,总是会throws exception “ArrayIndexOutOfBounds ” 。
改了好几遍,都不能避免 exception。

请问该如果修改啊?谢谢
...全文
645 18 打赏 收藏 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2013-12-28
  • 打赏
  • 举报
回复
只要控制好随机数的范围,就可以不用。 Random rand = new Random(); Crow = rand.nextInt(ChessPad.board.length)+1; Ccol = rand.nextInt(ChessPad.board[0].length)+1;
Sally_simple 2013-12-28
  • 打赏
  • 举报
回复
引用 16 楼 ldh911 的回复:
为啥要try catch?没理解你的意图。。。
我的意思是,已经规定了随机输入的范围,就可以不用throws exception了吧?
MiceRice 2013-12-27
  • 打赏
  • 举报
回复
为啥要try catch?没理解你的意图。。。
Sally_simple 2013-12-25
  • 打赏
  • 举报
回复
引用 12 楼 ldh911 的回复:
[quote=引用 10 楼 Sally_simple 的回复:] 我知道了,应该写成nextInt(3)就可以了,谢啦~
关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n) 关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)[/quote] 我试了一下,写不写try catch都可以执行。 那我throws exception吗? Thanks
Sally_simple 2013-12-24
  • 打赏
  • 举报
回复
引用 1 楼 ldh911 的回复:
需要注意的是:nextInt(3) 所返回的值,范围是 0~2
我把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 不对啊~谢啦
Sally_simple 2013-12-24
  • 打赏
  • 举报
回复
引用 12 楼 ldh911 的回复:
[quote=引用 10 楼 Sally_simple 的回复:] 我知道了,应该写成nextInt(3)就可以了,谢啦~
关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n) 关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)[/quote] 恩,是的。如果后面有+1,就永远取不到0,如果有-1, 就会异常。所以不用+1或是-1. 谢谢你啦
MiceRice 2013-12-24
  • 打赏
  • 举报
回复
引用 10 楼 Sally_simple 的回复:
我知道了,应该写成nextInt(3)就可以了,谢啦~
关键不是 nextInt( n ) 中 n 的取值,因为它永远返回的是 [0, n) 关键是后面那个操作是 +1 还是 -1,如果用“-1”,最终取值范围就变成了 [-1, n-1)
Sally_simple 2013-12-24
  • 打赏
  • 举报
回复
引用 9 楼 u011972917 的回复:
[quote=引用 7 楼 s346658910 的回复:] [quote=引用 4 楼 Sally_simple 的回复:] [quote=引用 1 楼 ldh911 的回复:] 需要注意的是:nextInt(3) 所返回的值,范围是 0~2
我把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 不对啊~谢啦[/quote] nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题[/quote]楼主是刚开始学java吧,你可以去看下API,nextInt(n)返回是0~(n-1)的一个随机数!所以你取到的数组下标为-1的时候会报异常。[/quote] 谢谢你,问题基本解决了~
Sally_simple 2013-12-24
  • 打赏
  • 举报
回复
引用 6 楼 u013028373 的回复:
Ccol = rand.nextInt(3)+1;
我知道了,应该写成nextInt(3)就可以了,谢啦~
江城DiorsMan 2013-12-24
  • 打赏
  • 举报
回复
引用 7 楼 s346658910 的回复:
[quote=引用 4 楼 Sally_simple 的回复:] [quote=引用 1 楼 ldh911 的回复:] 需要注意的是:nextInt(3) 所返回的值,范围是 0~2
我把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 不对啊~谢啦[/quote] nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题[/quote]楼主是刚开始学java吧,你可以去看下API,nextInt(n)返回是0~(n-1)的一个随机数!所以你取到的数组下标为-1的时候会报异常。
Sally_simple 2013-12-24
  • 打赏
  • 举报
回复
引用 6 楼 u013028373 的回复:
Ccol = rand.nextInt(3)+1;
rand.nextInt(3) +1 也会有异常。我试了rand.nextInt(2)+1, 就没有异常发生了。 但是不知道为什么13 和 27行会出现 Exception in thread "main" java.lang.StackOverflowError at java.util.Random.nextInt(Unknown Source) 请问你知道如何修改吗?谢啦
知风23_ 2013-12-24
  • 打赏
  • 举报
回复
引用 4 楼 Sally_simple 的回复:
[quote=引用 1 楼 ldh911 的回复:] 需要注意的是:nextInt(3) 所返回的值,范围是 0~2
我把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 不对啊~谢啦[/quote] nextInt(n)将返回一个大于等于0小于n的随机数,虽然负数下标可以用,但还是不推荐,对这方面不是很懂,不过推测应该是这个问题
小陈杰帝 2013-12-24
  • 打赏
  • 举报
回复
Ccol = rand.nextInt(3)+1;
tony4geek 2013-12-24
  • 打赏
  • 举报
回复
这个你debug 调试看看就明白了。
tony4geek 2013-12-23
  • 打赏
  • 举报
回复
楼上的对的,万一是小于0
oh_Maxy 2013-12-23
  • 打赏
  • 举报
回复
顶1L,-1改成+1即可
MiceRice 2013-12-23
  • 打赏
  • 举报
回复
需要注意的是:nextInt(3) 所返回的值,范围是 0~2

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧