作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的代码有一个问题,如果我按 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/
我是一名优秀的程序员,十分优秀!