2015-05-07 90 views
0

我想修改我的朋友在Java中的簡單文本冒險。我正在嘗試在菜單中添加一個'quit'選項,並修改main()方法中的循環,以便在用戶輸入此命令時退出。(Java)文本冒險:如果輸入輸入,關閉遊戲

但是這個代碼停止遊戲從一開始無論輸入是什麼:

while (playerLocation==10); 
      System.out.println("You won the game"); 
      break; 

而這會產生一個錯誤:

while(scanner.equals("9")) { 
      System.out.println("You have quit the game."); 
      break; 
     } 

下面

全碼:

package practice; 

import java.util.Scanner; 

public class practice 
{ 

private final int NO_EXIT = 99999; // indicates that there is no exit in that direction 

private int map[][]   = {{NO_EXIT,1,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},  // 1 
           {2,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},  // 2 
           {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,3,NO_EXIT,NO_EXIT},  // 3 
           {NO_EXIT,NO_EXIT,4,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},    // 4 
           {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,5},  // 5 
           {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,6,NO_EXIT,NO_EXIT},    // 6 
           {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,7},  // 7 
           {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,8,NO_EXIT},  // 8 
           {9,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT},  // 9 
           {NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT,NO_EXIT}}; // 10 


private String description[] = {"(Stage 1) stranded in dead-looking forest. You are trying to find your way out of here. You then see a pathway heading east.", 
           "(Stage 2) now in a large abandoned military base. There is no other way around and you see another pathway heading north.", 
           "(Stage 3) now brought by the path you took here in an abandoned farmhouse. You now see a cave up ahead on the northwest", 
           "(Stage 4) now inside a cave. It has an entrance to the west.", 
           "(Stage 5) currently in the left wing of the cave. On the southwest there is an entrance leading somwhere.", 
           "(Stage 6) halfway through to your escape. You are in the bottom of the cave. \nThere is a small hole ahead on your northeast, but you can fit right through", 
           "(Stage 7) inside a section where there is a body of water. On your southwest direction, you see a ladder leading somewhere.", 
           "(Stage 8) inside a slightly dark hallway. You see a light ahead in the southeast direction.", 
           "(Stage 9) now outside the cave. You see a path heading north connecting towards an establishment.", 
           "(Stage 10) finally on your destination!"}; 


private String objectName[]  = {"An adult magazine", "A fully loaded .45 calibre gun", "A motherboard from the 90s", "A syringe", 
            "A skull", "A worn out pair of shoes", "A Matchbox", "A Wooden Plank", "Illegal Drugs", "A bottle of whiskey" }; 
private int objectLocation[] = {0, 1, 3, 1, 2, 0, 7, 6, 2, 1}; 
private int playerLocation  = 0; 



// Prints out a description of the location the player is currently in, including a list of any objects at that location 
private void describeLocation() 
{ 
    System.out.println(); 
    System.out.println("You are " + description[playerLocation]); 
    System.out.println("\n\t\tIn this area, you found: "); 
    int numObjects = 0; 
    for (int i=0; i<objectLocation.length; i++) 
    { 
     if (objectLocation[i]==playerLocation) 
     { 
      System.out.println("\n\t\t" + objectName[i]); 
      numObjects++; 
     } 
    } 
    if (numObjects==0) 
    { 
     System.out.println("\n\t\tNo Item(s)"); 
    } 
    System.out.println(); 
} 

// implements a simple text-driven menu 
private int getMenuSelection(Scanner s) 
{ 


    // display menu 
    System.out.println("What do you want to do?"); 
    System.out.println("1. Go North"); 
    System.out.println("2. Go East"); 
    System.out.println("3. Go West"); 
    System.out.println("4. Go South"); 
    System.out.println("5. Go North East"); 
    System.out.println("6. Go North West"); 
    System.out.println("7. Go South East"); 
    System.out.println("8. Go South West"); 
    System.out.println("9. Quit Game"); 
    System.out.print("Enter command (1-9): "); 
    // get and return the user's selection 
    return s.nextInt(); 
} 






// try to move in the specified direction 
private void move(int direction) 
{ 
    int nextLocation = map[playerLocation][direction]; 

    if (nextLocation==NO_EXIT) 
    { 
     System.out.println("\nThere is no way there. Try again!"); 
    } 

    else 
    { 
     playerLocation = nextLocation; 
    } 
} 

public void startGame() 
{ 
    Scanner scanner = new Scanner(System.in); 
    do 
    { 
     describeLocation(); 
     int selection = getMenuSelection(scanner); 
     move(selection-1); 



     while (playerLocation==10); 
      System.out.println("You won the game"); 
      break; 


    } while (true);  
} 

public static void main(String[] args) 
{ 
    practice adv = new practice(); 
    adv.startGame(); 


    } 
} 

所以基本上w我需要知道的是在這些事情得到糾正後如何進行清點。

謝謝。

回答

0

你的一個很多問題是這樣的代碼片段:

while (playerLocation==10); // < Semi colon here means loop forever if playerLocation==10 
System.out.println("You won the game"); 
break; // This will always happen. 

你可能想要這個:

playerLocation = scanner.nextInt(); 
if (playerLocation==10){ 
    System.out.println("You won the game"); 
    break; // this will break the outer loop 
} 
0

首先,在playerLocation永遠是10,你的數組大小隻有9.您必須從0開始計數而不是從1開始計數。陣列中的第一個值爲0.

while (playerLocation == 9) { System.out.println("You won the game"); break; }

下一個是,你的方向,而不是新的位置。如果玩家想要東移,則將位置更改爲陣列中的位置2。

所以想想它,並與切換案例一起工作。它可能是這樣的。

// try to move in the specified direction 
private void move(int direction) { 

    int x = 0;//this should be the actual position of the player 
    int y = 0;//this should be the actual position of the player 

    switch (direction) { 
    case 0: 
     System.out.println("up"); 
     y++; 
     break; 
    case 1: 
     System.out.println("down"); 
     y--; 
     break; 
    case 2: 
     System.out.println("left"); 
     x--; 
     break; 
    case 3: 
     System.out.println("right"); 
     x++; 
     break; 
    default: 
     break; 
    } 
}