为何我这里的IndexOutOfBoundsException无法跳过(Java)?

这是真·CS小白在courseraPrincetonAlgorithms上码的第一个作业,真是好心酸...作业网址如下:http://coursera.cs.princeton.edu/algs4/assignments/percolation.html
由两个文件组成,Percolation和PercolationStats,后者中将实例化前者并完成相应的计算和模拟。
publicclassPercolation{
int[][]grid;
intranN;
intN;
WeightedQuickUnionUFwuf;
publicPercolation(intN){
if(N<=0){
thrownewIllegalArgumentException("N不可以小于等于0");
}
grid=newint[N][N];
for(introw=0;rowfor(intcol=0;colgrid[row][col]=0;
}
}
wuf=newWeightedQuickUnionUF(N*N);
}
publicvoidopen(inti,intj){
if(i<1||i>N||j<1||j>N){
thrownewIndexOutOfBoundsException("RoworColindexoutofbounds!");
}
grid[i-1][j-1]=1;
}
publicbooleanisOpen(inti,intj){
if(i<1||i>N||j<1||j>N){
thrownewIndexOutOfBoundsException("RoworColindexoutofbounds!");
}
if(grid[i-1][j-1]==1)
returntrue;
else
returnfalse;
}
publicbooleanisFull(inti,intj){
if(i<1||i>N||j<1||j>N){
thrownewIndexOutOfBoundsException("RoworColindexoutofbounds!");
}
intparent=(i-1)*N+j-1;
if(isOpen(i,j)&&wuf.find(parent)returntrue;
else
returnfalse;
}
publicbooleanpercolates(){
for(inti=1;i<=N;i++){
if(isFull(N,i)==true)
returntrue;
}
returnfalse;
}
publicstaticvoidmain(String[]args){
return;
}
}
于是乎...为何我在PercolationStats里调用open(),isOpen()函数等,无论我的i,j是多少都会调用IndexOutOfBoundsException呢?感觉很困惑...
importjava.lang.System;
publicclassPercolationStats{
staticintN,T;
intrdRow,rdCol,rdN,openCount;
doublepercentage;
double[]group;
Percolationpc;
publicPercolationStats(intN,intT){
intnum=N*N;
group=newdouble[T];
inti=0;
while(i
pc=newPercolation(N);
openCount=0;
while(pc.percolates()==false){
do{
rdRow=StdRandom.uniform(1,N+1);
rdCol=StdRandom.uniform(1,N+1);
}
while(pc.isOpen(rdRow,rdCol)==false);
pc.open(rdRow,rdCol);
openCount++;
rdN=(rdRow-1)*N+rdCol-1;
if(rdCol+1<=N&&pc.isOpen(rdRow,rdCol+1))
pc.wuf.union(rdN,rdN+1);
if(rdCol-1>=1&&pc.isOpen(rdRow,rdCol-1))
pc.wuf.union(rdN,rdN-1);
if(rdRow+1<=N&&pc.isOpen(rdRow+1,rdCol))
pc.wuf.union(rdN,rdN+N);
if(rdRow-1>=1&&pc.isOpen(rdRow-1,rdCol))
pc.wuf.union(rdN,rdN-N);
}
percentage=openCount/num;
group[i]=percentage;
i++;
}
}
publicdoublemean(){
returnStdStats.mean(group);
}
publicdoublestddev(){
returnStdStats.stddev(group);
}
publicdoubleconfidenceLo(){
doubleLo=this.mean()-1.96*this.stddev()/Math.sqrt(T);
returnLo;
}
publicdoubleconfidenceHi(){
doubleHi=this.mean()-1.96*this.stddev()/Math.sqrt(T);
returnHi;
}
publicstaticvoidmain(String[]args){
N=Integer.parseInt(args[0]);
T=Integer.parseInt(args[1]);
PercolationStatsPS=newPercolationStats(N,T);
System.out.println("mean="+PS.mean());
System.out.println("stddev="+PS.stddev());
System.out.print("95%confidenceinterval="+PS.confidenceLo()+","+PS.confidenceHi());
}
}
下面是我调用open()所用的类。初学java,在上princeton的算法课,结果第一个作业就亚历山大...以及...我在删掉IndexOutOfBoundsException的if后,运行javaPercolationStats200100没有任何反应啊,这是怎么回事呢?小白一枚真心求教!如果有愿意点开coursera作业并帮忙分析原因的大神本人感激不尽!谢谢~~
芜湖不芜
浏览 416回答 2
2回答

翻过高山走不出你

Percolation类的N赋值了吗?似乎没有,因此你的N被默认赋值为0`publicvoidopen(inti,intj){if(iN||jN){//i>N一直成立所以这个异常总是抛出thrownewIndexOutOfBoundsException("RoworColindexoutofbounds!");}`你可以在publicPercolation(intN){//在这里加个句this.N=N;......}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript