我正在用java编写一个简单的迷宫游戏。该程序从输入文件中读取文本“地图”,用于迷宫的布局。规则很简单:通过用户输入导航迷宫(由2D数组表示),避免凹陷(由X表示),并到达'P‘(玩家)和标记为'T’的地点。现在,我已经写好了大部分代码,只是让它正常工作的问题。我已经将游戏的大部分设置为使用while循环运行,并将布尔值"got“设置为false。一旦这听起来是真的,它应该会结束游戏。
但是,我还没有对玩家实际获得宝藏的情况进行编码,所以我想知道为什么我的代码只是简单地显示“恭喜!你找到了宝藏!”没别的了。如果有人能对此有所了解,我将不胜感激。我的代码在某种程度上是一堆循环,因为我们的老师刚刚开始学习方法、构造函数和创建我们自己的类。以下是我到目前为止拥有的代码:
import java.util.*;
import java.io.File;
public class MazeGame {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new File("maze.txt"));
Scanner user = new Scanner(System.in);
int rows = scan.nextInt();
int columns = scan.nextInt();
int px = 0;
int py = 0;
String [][] maze = new String[rows][columns];
String junk = scan.nextLine();
for (int i = 0; i < rows; i++){
String temp = scan.nextLine();
String[] arrayPasser = temp.split("");
for (int j = 0; j < columns; j++){
maze[i][j] = arrayPasser[i];
}
}
boolean gotTreasure = false;
while (gotTreasure = false){
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
System.out.print(maze[i][j]);
System.out.print(" ");
}
System.out.print("\n");
}
System.out.printf("\n");
System.out.println("You may:");
System.out.println("1) Move up");
System.out.println("2) Move down");
System.out.println("3) Move left");
System.out.println("4) Move right");
System.out.println("0) Quit");
int choice = user.nextInt();
int i = 0;
if (choice == 1 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("X") == false){
maze[px][py] = ".";
maze[k][l-1] = "P";
maze[px][py] = maze[k][l-1];
}else if (maze[px][py-1] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 2 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("X") == false){
maze[px][py] = ".";
maze[k][l+1] = "P";
maze[px][py] = maze[k][l+1];
}else if (maze[px][py+1] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 3 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("X") == false){
maze[px][py] = ".";
maze[k-1][l] = "P";
maze[px][py] = maze[k-1][l];
}else if (maze[px-1][py] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 4 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("X") == false){
maze[px][py] = ".";
maze[k+1][l] = "P";
maze[px][py] = maze[k+1][l];
}else if (maze[px+1][py] == "X"){
System.out.println("Cannot move into a cave-in! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 0){
System.exit(0);
}
}
System.out.println("Congratulations, you found the treasure!");
scan.close();
user.close();
}
}
下面是示例输入文件:
:
发布于 2012-09-29 08:46:20
(叹息)一个等于符号而不是两个。您有"while (gotTreasure = false)",它将值false赋给gotTreasure,并且不进入循环。将其更改为"while (gotTreasure == false),它将进入循环。
对于未来的问题:请尝试自己弄清楚正在发生的事情,并让其他人知道您尝试了什么,以及您对此有哪些具体的问题。这是有争议的,我应该让它过去,因为它本质上是一个为您调试代码的请求。学会调试自己。如果跟踪语句没有被执行,那么很可能此时的代码没有被执行。如果没有进入循环,几乎可以肯定是因为进入循环的条件不存在。
学习使用调试器- eclipse (我相信,还有很多其他的开发工具)有一个很好的调试器。了解什么是断点,如何设置断点并在命中时检查变量,并从中找出哪里出了问题。
发布于 2012-09-29 08:47:02
如果这是一个拼写错误,忽略它,如果它不是
while (gotTreasure = false) is wrong.
您没有检查gotTreasure是否为false,而是将其分配为false。
要检查gotTreasure是否为假,请使用==操作符
while(gotTreasure==false)
让我知道如果这是一个类型,我会删除答案。:)
发布于 2012-09-29 08:48:36
您在while循环条件中有一个简单的错误,
而不是,
while (gotTreasure = false)
你应该使用,
while (gotTreasure == false)
在第一种情况下,您将false赋值给gotTreasure,在第二种情况下,您将评估gotTreasure是否为false。
https://stackoverflow.com/questions/12648898
复制相似问题