gpt4 book ai didi

java - 如何查看字符串的第一个字符是否是某个字符,如果是,则执行操作(无法正常工作)

转载 作者:行者123 更新时间:2023-12-01 20:09:19 25 4
gpt4 key购买 nike

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
}

编辑:因此,我更改了代码以使用 .equals 而不是 ==,并且当我输入只有一个单词的字符串时,我的代码可以成功运行,因此"is"。如果我输入一串多个单词(“是的,我愿意”),我仍然会收到错误。输入包含多个单词的字符串会产生错误。当我使用 scnr 时。 next() 时,它总是编译 else 语句,即使该语句是“是的,我愿意”,其中第一个字母是 Y。如果我使用 scnr 。 nextLine() 它编译 if 语句,重新启动循环,打印“ map 的宽度...”,然后在我输入任何内容之前给出错误。

我主要指的是底部的main方法,从“你想再玩一次吗”到main方法的末尾。我的提示用户方法也有问题,我希望该方法显示错误消息,并在用户输入除 int 之外的任何内容时再次扫描 int。

最佳答案

使用response.equals()而不是==

== 比较对象的引用,而 equals() 会比较字符串的值。

关于java - 如何查看字符串的第一个字符是否是某个字符,如果是,则执行操作(无法正常工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46922320/

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