If statement not working all the time in java

44 views Asked by At

I am making a text game movement system and a found a difficult issue. When i enter the cardinal directions, east returns the value expected but locks me in place, west returns my programmed error message, likewise north, and south returns a value no where near the player. Also my programming is sloppy as this is a rough draft and the array isn't static because i learned about static variables recently.

//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);
            }
            }

}

These are the errors, i mentioned. Some of them print out numerals and words as i was debugging to no avail.

 
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

I've made many revisions none of which solved the central problem of east not incrementing after being used more than once. I have been focusing on east in the hopes of finding a universal solution.

1

There are 1 answers

1
Basil Bourque On
            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){
                …

As commented, you set the x and y to both be zero. Immediately after, you test for the x and y values being within a certain range. That test makes no sense. The values will always be zero.

It is now time for you to learn to use a debugger.