If 语句在 java 中始终不起作用

问题描述 投票:0回答:1

我正在制作一个文本游戏运动系统,发现一个难题。当我输入基本方向时,东返回预期的值,但将我锁定在适当的位置,西返回我编程的错误消息,同样北,南返回一个远离玩家的值。另外,我的编程很草率,因为这是一个草稿,并且数组不是静态的,因为我最近了解了静态变量。

//import java.util.Arrays;
import java.util.Scanner;
public class game {
    public static void main(String[]args){
        Scanner input = new Scanner(System.in);
        System.out.println("You wake up with a searing headache accomponied by a fizzing sound. After slowly standing up and");
        System.out.println(" looking around you see your space ship infront of you. It is in pretty bad shape and it left a");
        System.out.print("what will you do?");
        
        Boolean gameIsRunning = true;

        while(gameIsRunning){
        String dir1 = input.next();
        movement(dir1);
        }

       

        input.close();
        }
         public static void movement(String in){
            //get direction west,south,etc..
          String dir = in;


            //set map and location
            String loclib[][] = new String [3][3];
            loclib [0][0] = "north pole";
            loclib [0][1] = "geyser field";
            loclib [0][2] = "hill";
            loclib [1][0] = "ridges";
            loclib [1][1] = "ice spikes";
            loclib [1][2] = "ridges";
            loclib [2][0] = "ice spikes";
            loclib [2][1] = "plaines";
            loclib [2][2] = "south pole";
    
            int playerX=0;
            int playery=0;
            
            if (dir.equals("west") && playerX>0 && playerX<=2){
                playerX--;
                System.out.println(loclib [playery] [playerX]);
                
            }
            else if (dir.equals("east") && playerX>=0 && playerX<=1){
                ++playerX;
                System.out.println(loclib [playery] [playerX]);
                System.out.println(". player x and y, " + playerX + ", " + playery);
            }
            else if (dir.equals("south") && playery>=0 && playery<=1){
                playery++;
                System.out.println(loclib [playery] [playerX]);
                
            }
            else if (dir.equals("North") && playery>0 && playery<=2){
                playery--;
                System.out.println(loclib [playery] [playerX]);
                
            }
             else {
                System.out.println("Either that word is not valid or you are trying to cross your restriction." + dir);
            }
            }

}

我提到过这些是错误。其中一些打印出数字和单词,因为我正在调试但无济于事。

 
You wake up with a searing headache accomponied by a fizzing sound. After slowly standing up and
 looking around you see your space ship infront of you. It is in pretty bad shape and it left a
what will you do?east
geyser field
. player x and y, 1, 0
west
Either that word is not valid or you are trying to cross your restriction.west
south
ridges
North
Either that word is not valid or you are trying to cross your restriction.North

我做了很多修改,但都没有解决东方在使用多次后不增加的核心问题。我一直关注东方,希望找到一个通用的解决方案。

java if-statement variables increment post-increment
1个回答
0
投票
            int playerX=0;
            int playery=0;
            
            if (dir.equals("west") && playerX>0 && playerX<=2){
                …
            }
            else if (dir.equals("east") && playerX>=0 && playerX<=1){
                …
            }
            else if (dir.equals("south") && playery>=0 && playery<=1){
                …

正如评论,您将 x 和 y 设置为零。之后,您立即测试 x 和 y 值是否在特定范围内。那个测试没有任何意义。这些值将始终为零。

© www.soinside.com 2019 - 2024. All rights reserved.