java如何查看字符串的第一个字符是否是某个字符,如果是,则执行一个操作(无法正常工作)
import java.util.Random;
import java.util.Scanner;
public class MineSweeper {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
boolean playAgain = true;
System.out.println("Welcome to Mine Sweeper!");
while (playAgain) {
final int MIN_SIZE = 3;
final int MAX_SIZE = 20;
final char NO_NEARBY_MINE = ' ';
String errorWidth = "Expected a number from " + MIN_SIZE + " to " + MAX_SIZE + "\nWhat width of map would you like (" + MIN_SIZE + "-" + MAX_SIZE + "):";
String errorHeight = "Expected a number from " + MIN_SIZE + " to " + MAX_SIZE + "\nWhat height of map would you like (" + MIN_SIZE + "-" + MAX_SIZE + "):";
System.out.println("What width of map would you like (" + MIN_SIZE + "-" + MAX_SIZE + "):");
int userWidth = promptUser(scnr, errorWidth, MIN_SIZE, MAX_SIZE);
System.out.println("What height of map would you like (" + MIN_SIZE + "-" + MAX_SIZE + "):");
int userHeight = promptUser(scnr, errorHeight, MIN_SIZE, MAX_SIZE);
String errorGetRow = "Expected a number from " + 1 + " to " + userWidth + ".";
String errorGetCol = "Expected a number from " + 1 + " to " + userHeight + ".";
int minRowCol = 1;
char[][] mineArray = new char[userHeight][userWidth];
eraseMap(mineArray);
simplePrintMap(mineArray);
System.out.println("row: ");
int row = promptUser(scnr, errorGetRow, minRowCol, userWidth);
System.out.println("column: ");
int column = promptUser(scnr, errorGetCol, minRowCol, userHeight);
mineArray[row-1][column-1] = NO_NEARBY_MINE;
simplePrintMap(mineArray);
System.out.println("Would you like to play again (y/n)?");
String response = scnr.next();
if (response.startsWith("Y") || response.startsWith("y")) {
playAgain = true;
}
else {
playAgain = false;
}
}
System.out.println("Thank you for playing Mine Sweeper!");
}
public static int promptUser(Scanner in, String prompt, int min, int max) {
int userTempVal = 0;
do {
userTempVal = in.nextInt();
if (userTempVal < min || userTempVal > max) {
System.out.println(prompt);
}
}while (userTempVal < min || userTempVal > max);
return userTempVal;
}
public static void eraseMap(char[][] map) {
final char UNSWEPT = '.';
for (int row = 0; row < map.length; row++) {
for (int col = 0; col < map[row].length; col++) {
map[row][col] = UNSWEPT;
}
}
return;
}
public static void simplePrintMap(char[][] map) {
for (int row = 0; row < map.length; row++) {
for (int col = 0; col < map[row].length; col++) {
System.out.print(map[row][col] + " ");
}
System.out.println("");
}
return; //FIXME
}
编辑: 所以我改变了我的代码来利用。等于而不是==,当我输入一个只有一个单词的字符串时,我的代码可以成功运行,所以“是”。如果我输入一串多个单词(“是的,我会”),我仍然会得到一个错误。键入包含多个单词的字符串会产生错误。当我使用scnr时。next(),它总是编译else语句,即使该语句是“Yes I do”,其中第一个字母是Y。如果我使用scnr。nextLine()它编译if语句,重新启动循环,打印“地图的宽度…”然后在我输入任何东西之前给出一个错误
我主要指的是底部的主要方法,从“你想再玩一次吗”到主要方法的结尾。我的promptUser方法中也有一个问题,我希望该方法显示错误消息,并在用户输入除int以外的任何内容时再次扫描int
# 1 楼答案
你的代码有两个问题
使用
==
比较String
这是错误的,因为它比较的是对象引用,而不是对象内容。您需要使用
String.equals()
进行比较。在您的情况下,您甚至可以使用String.startsWith()
来检查您的输入是否以某个内容开头在你的输入中使用
String.substring()
由于用户只需按
[ENTER]
即可输入空字符串,因此不能使用String.substring(0,1)
,否则将得到String index out of range
要解决上述两个问题,您可以按如下方式更改代码
为了确保下一次阅读准备就绪,我建议在完成当前行的阅读后添加
Scanner.nextLine()
例如
# 2 楼答案
用
response.equals()
代替==
==
比较对象的引用,而equals()
比较字符串的值# 3 楼答案
您正面临这个问题,因为您正在使用
==
运算符来比较字符串。如果使用语句response.substring(0, 1).equals("Y")
,这将解决问题,因为这是检查字符串相等性的实际方法。或者你可以使用下面列出的不那么详细的方法只需取字符串的第一个字符,并检查是否与所需的任何其他字符相等