我必须创建一个Java项目,该项目将在文本文件中作为岛读取并随机移动鼠标100次并确定鼠标是否逃逸,饥饿或淹死。我有2节课。我的第一堂课是:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Island {
public char island [][];
private int MoveCount = 0, StarveCount = 0, DrownCount = 0, EscapeCount = 0;
public int mouse = 1;
public void mouseIsland(String fileName) throws FileNotFoundException {
island = new char [0][0];
Scanner file = new Scanner(new File(fileName));
List<String> lines = new ArrayList<String>();
while(file.hasNextLine()) {
ArrayList<String> lines1= new ArrayList<String>();
String line= file.nextLine();
lines1.add(line);
island=new char[lines1.size()][];
}
}
public void moveLeft() {
char[][] temp = island;
if(MoveCount == 100){
System.out.println("The mouse starved");
StarveCount++;
MoveCount = 0;
}
for(int i=0; i<island.length; ++i)
for(int j=0; j<island[i].length; ++j)
if(MoveCount <100 && island[i][j] == '-'){
temp[i][j] = island[i][j-1];
MoveCount++;
if(temp[i][j] == '#'){
System.out.println("The mouse drowned after moving "+MoveCount+" times.");
DrownCount++;
MoveCount = 0;
break;
}
else if(temp[0][j] == '-' || temp [island.length-1][j] == '-' || temp [i][0] == '-' || temp[i][island[0].length-1] == '-'){
System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
EscapeCount++;
MoveCount = 0;
break;
}
mouse = temp[i][j];
temp[i][j] = (char) (MoveCount+1);
island = temp;
}
}
public void moveRight() {
char[][] temp = island;
if(MoveCount == 100){
System.out.println("The mouse starved");
StarveCount++;
MoveCount = 0;
}
for(int i=0; i<island.length; ++i)
for(int j=0; j<island[i].length; ++j)
if(MoveCount <100 && island[i][j] == '-'){
temp[i][j] = island[i][j+1];
MoveCount++;
if(temp[i][j] == '#'){
System.out.println("The mouse drowned after moving "+MoveCount+" times.");
DrownCount++;
MoveCount = 0;
break;
}
else if(temp[0][j] == '-' || temp [island.length-1][j] == '-' || temp [i][0] == '-' || temp[i][island[0].length-1] == '-'){
System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
EscapeCount++;
MoveCount = 0;
break;
}
mouse = temp[i][j];
temp[i][j] = (char) (MoveCount+1);
island = temp;
}
}
public void moveUp() {
char[][] temp = island;
if(MoveCount == 100){
System.out.println("The mouse starved");
StarveCount++;
MoveCount = 0;
}
for(int i=0; i<island.length; ++i)
for(int j=0; j<island[i].length; ++j)
if(MoveCount <100 && island[i][j] == '-'){
temp[i][j] = island[i-1][j];
MoveCount++;
if(temp[i][j] == '#'){
System.out.println("The mouse drowned after moving "+MoveCount+" times.");
DrownCount++;
MoveCount = 0;
break;
}
else if(temp[0][j] == '-' || temp [island.length-1][j] == '-' || temp [i][0] == '-' || temp[i][island[0].length-1] == '-'){
System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
EscapeCount++;
MoveCount = 0;
break;
}
mouse = temp[i][j];
temp[i][j] = (char) (MoveCount+1);
island = temp;
}
}
public void moveDown(){
char[][] temp = island;
if(MoveCount == 100){
System.out.println("The mouse starved");
StarveCount++;
MoveCount = 0;
}
for(int i=0; i<island.length; ++i)
for(int j=0; j<island[i].length; ++j)
if(MoveCount <100 && island[i][j] == '-'){
temp[i][j] = island[i+1][j];
MoveCount++;
if(temp[i][j] == '#'){
System.out.println("The mouse drowned after moving "+MoveCount+" times.");
DrownCount++;
MoveCount = 0;
break;
}
else if(temp[0][j] == '-' || temp [island.length-1][j] == '-' || temp [i][0] == '-' || temp[i][island[0].length-1] == '-'){
System.out.println("The mouse found a bridge and escaped after "+MoveCount+" times.");
EscapeCount++;
MoveCount = 0;
break;
}
mouse = temp[i][j];
temp[i][j] = (char) (MoveCount+1);
island = temp;
}
}
public void moveMouse (){
for(int i=0; i<100; i++)
{
int randomNumber = 1+(int)(Math.random()*(4));
if(randomNumber == 1);
moveUp();
if(randomNumber == 2);
moveDown();
if(randomNumber == 3);
moveLeft();
if(randomNumber == 4);
moveRight();
}
}
public String printStats(){
return "The mouse starved this many times: "+StarveCount+
"\nThe mouse escaped this many times: "+EscapeCount+
"\nThe mouse drowned this many times: "+DrownCount;
}
}`
这是我的第二堂课:
public class Exercise12Main {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Island i1 = new Island();
i1.mouseIsland("mouseEscapeIsland-1.txt");
System.out.println("Mouse Escape Island 1");
System.out.println("The Coordinates of the bridges are: (0,1) & (2,11)");
System.out.println("The Mouse is Starting at (7,6)");
i1.moveMouse();
i1.printStats();
}
}
这是我的岛屿:
#-##########
#----------#
#-----------
#----------#
#----------#
#----------#
#----------#
#-----X----#
#----------#
#----------#
#----------#
############
每次尝试运行此代码时,都会出现错误: 线程“main”中的异常 显示java.lang.NullPointerException 在Island.moveUp(Island.java:97) 在Island.moveMouse(Island.java:154) 在Exercise12Main.main(Exercise12Main.java:13)
答案 0 :(得分:0)
谷歌“Java命名约定”并遵循它。
对于您的错误:您在island
方法中初始化mouseIsland
:
island=new char[lines1.size()][];
所以你有一个char数组的null,现在你尝试在island[0].length
方法中访问moveMouse
:
for(int j=0; j<island[i].length; ++j)
,您得到NullPointerException
,因为island[0]
是null
。
如果您使用调试器,您也可以轻松找到这些错误; - )