程序没有终止

时间:2015-03-31 14:27:39

标签: java

我的代码存在问题,如果按q或Q,程序不会终止。我放弃的代码接近底部。你能告诉我怎么解决吗? 谢谢

这是我的代码:

import java.util。*;

public class Stuff
    {
        static // Hash Map Function
        Map<String, String> myMap = new LinkedHashMap<String, String>();

public Stuff()
    {
        //Functions to split out the code.
        fillMapInformation();
        drawMap();
    }

private void fillMapInformation()
    {   
        // -A0 and -A1 put an indent so to not clash with the numbers indicating the rows.
        // myMap is used to draw the table, not using the 2D array.
                // row , column
        myMap.put("-A0", " ");
        myMap.put("A0", " A");
        myMap.put("B0", "B");
        myMap.put("C0", "C");
        myMap.put("D0", "D");
        myMap.put("-A1", "1:");
        myMap.put("A1", "-");
        myMap.put("B1", "-");
        myMap.put("C1", "-");
        myMap.put("D1", "-");
        myMap.put("-A2", "2:");
        myMap.put("A2", "-");
        myMap.put("B2", "-");
        myMap.put("C2", "-");
        myMap.put("D2", "-");
        myMap.put("-A3", "3:");
        myMap.put("A3", "-");
        myMap.put("B3", "-");
        myMap.put("C3", "-");
        myMap.put("D3", "-");
    }

private static void drawMap()
    {
        for (Map.Entry<String, String> entry : myMap.entrySet()) 
            {
                String key = entry.getKey();    // Function to see what is entered into the myMap row.
                String value = entry.getValue();    // Function to see what is entered into the myMap column.

                if(key.contains("-"))
                    {
                        TextIO.putln(" ");
                    }
                TextIO.put(value + "\t");
            }
    }

public static void main(String[] args) 
    {       
        Stuff stuff = new Stuff();
        int userMoves = 1;
        boolean quitGame = false;
        boolean validMove = false;

        TextIO.putln(" ");
        // intstructions
        TextIO.putln("nmjhju ");            

        TextIO.putln(" ");
        TextIO.putln("Where do you want to place your knight? ");
        String Knight = TextIO.getlnString();
        Knight = Knight.toUpperCase();

        String KnightMoves = Integer.toString(userMoves);
        myMap.put(Knight, KnightMoves);

        userMoves ++;



        do 
            {
                drawMap();
                TextIO.putln(" ");
                TextIO.putln("Where do you want to put your next move? Or press 'q' to quit.");

                Knight = TextIO.getlnString();
                Knight = Knight.toUpperCase();

                KnightMoves = Integer.toString(userMoves);

                if (myMap.get(Knight) != "-")
                {
                    TextIO.putln("Invalid move!");
                }
                else
                {
                    validMove = true;
                }

                if (validMove == true)
                {
                    myMap.put(Knight, KnightMoves);
                    userMoves ++;



                    if (Knight == "Q")
                    {
                        quitGame = true;
                    }
                    validMove = false;

                }

            }
        // Problem, Knight is not recognising Q so it is not quitting, but userMoves != 13 is working.
        while (quitGame == false && userMoves != 13);

        drawMap();
        TextIO.putln("Game over!");

        if (userMoves == 13)
        {
            TextIO.putln("Congratulations you have completed the game!");
        }
    }

}

1 个答案:

答案 0 :(得分:2)

正确的是:

 if (Knight.equals("Q"))
<顺便说一下...阅读Java代码约定。变量名称应该从lowercase开始。 Knight = knight