Мне нужно создать проект 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)
Care, чтобы сказать нам, какие ошибки вы получаете? – t0mppa
Почему люди никогда не понимают, что было бы полезно узнать об ошибке .... – Blub
Ошибка, которую я получаю: Исключение в потоке «main» java.lang.NullPointerException \t на Island.moveUp (Island.java : 97) \t на Island.moveMouse (Island.java:154) \t at Exercise12Main.main (Exercise12Main.java:13) – user3083802