gpt4 book ai didi

java - 程序未终止

转载 作者:行者123 更新时间:2023-12-02 04:49:22 25 4
gpt4 key购买 nike

我的代码有一个问题,如果我按 q 或 Q,程序不会终止。我放置的退出代码位于底部附近。你能告诉我如何修复它吗?谢谢

这是我的代码:

导入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!");
}
}

}

最佳答案

正确的是:

 if (Knight.equals("Q"))

顺便说一下...阅读 Java 代码约定。变量名称应以小写开头。 骑士=骑士

关于java - 程序未终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29371058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com