public static void mainString args Random myr=new Random; Scanner mys=new ScannerSystemin; int maps= 000000 000000 000000
在判断箱子在墙边时,需要修改判断条件中的数组索引。当前代码中使用的是rl == maps[rl].length+1,应该修改为rl == maps[rh].length-1。代码修改后的部分如下所示:
if(rl==maps[rh].length-1){
System.out.println("在墙边");
continue;
}
//判断人头上是不是箱子,撞箱子
if(rl+1==xl && rh==xh){
//判断箱子在墙边,不让箱子出墙w
if(rl+1 == maps[rh].length-1){
System.out.println("撞墙了");
continue;
}
//让箱子能停留在位置上
xl++;
maps[xh][xl]=2;
}
这样修改后,程序在往右推箱子时不会报错。
原文地址: http://www.cveoy.top/t/topic/h5lN 著作权归作者所有。请勿转载和采集!