gpt4 book ai didi

java - 如何更新方法中的值?

转载 作者:行者123 更新时间:2023-12-02 05:57:49 24 4
gpt4 key购买 nike

我构建了一个字符串猜测游戏,它工作得很好,除了当用户输入输入时,然后检查输入的字符串中是否有非法字符,如果有,用户必须重新输入新的输入,但是输入不会在程序中更新。

我尝试将输入值设为全局值,但事实并非如此,该类也是由主类调用的。

import java.util.Scanner;
public class GuessCharactersInAString {
static Scanner scan = new Scanner(System.in);
static String input;
public static void guessTheString(){
String response;
introduction();
do {
System.out.println("Please enter a string of words.");
String input = scan.nextLine();
legalString(input);
int inputLength = input.length();
String charsUsed = " abcdefghijklmnopqrstuvwxyz";
int charsUsedLength = charsUsed.length();
String answerString = "?".repeat(inputLength);
int answerStringLength = answerString.length();


char[] answerStringChars = answerString.toCharArray();
char[] charsUsedChars = charsUsed.toCharArray();
char used = '\u26c7';
String newAnswerString;
String newcharsUsedLength;
boolean gameIsDone = false;
System.out.println("Guess a letter, only alphabet letters and the space bar please,"
+ "\n" + "special characters/symbols and numbers are not allowed");
while (!gameIsDone) {


String guess = scan.nextLine();
legalGuess(guess);

String guessRepeat = guess.repeat(3);
char guessChar = guessRepeat.charAt(2);

for (int i = 0; i < answerString.length(); i++) {
if (input.charAt(i) == guessChar) {
answerStringChars[i] = guessChar;
}
}
for (int j = 0; j < charsUsed.length(); j++) {
if (charsUsed.charAt(j) == guessChar) {
charsUsedChars[j] = used;
}

}
System.out.println("\t\t\t\t\t\t\t" + "Original String: " + input);
newAnswerString = String.valueOf(answerStringChars);
System.out.println("\t\t\t\t\t\t\t" + "Guessed String: " + newAnswerString + "\n");
newcharsUsedLength = String.valueOf(charsUsedChars);
System.out.println("\t\t\t\t\t\t\t" + "Characters to choose from:" + "\n" + "\t\t\t\t\t\t\t" + newcharsUsedLength);

if (newAnswerString.equals(input)) {
gameIsDone = true;
}

}
System.out.println("Congratulations! You guessed it!");
System.out.println("Would you like to play again?, please enter yes");
response=scan.nextLine();
}
while (response.equalsIgnoreCase("yes"));
}

private static void introduction() {
System.out.println("\t\t\t\t\t" + " This game prompts for a string of words. It \n" +
" then allows the player to guess the characters\n" +
" in the string of words. It keeps track of the \n" +
" characters guessed and tells the player when he\n" +
" wins.");
}

private static String legalString (String input){
input = input.toLowerCase();

while (input.contains("1") || input.contains("2") || input.contains("3") || input.contains("4") || input.contains("5") || input.contains("6") || input.contains("7") || input.contains("8")
|| input.contains("9") || input.contains("0") || input.contains("!") || input.contains("@") || input.contains("#") || input.contains("$") || input.contains("%") || input.contains("^")
|| input.contains("&") || input.contains("1") || input.contains("1") || input.contains("1") || input.contains("1") || input.contains("1") || input.contains("1") || input.contains("1") ||
input.contains("&") || input.contains("*") || input.contains("(") || input.contains(")") || input.contains("-") || input.contains("_") || input.contains("=") || input.contains("+") ||
input.contains("`") || input.contains("~") || input.contains("-") || input.contains("[") || input.contains("{") || input.contains("]") || input.contains("}") || input.contains(";") ||
input.contains(":") || input.contains("'") || input.contains(",") || input.contains("<") || input.contains(".") || input.contains(">") || input.contains("?") || input.contains("/") ||
input.contains("|")) {
System.out.println("Inappropriate value(s), please enter a new value");
input = scan.nextLine();
}
return input;
}
private static String legalGuess(String guess){

int guessLength = guess.length();
guess=guess.toLowerCase();
while (guess.contains("1") || guess.contains("2") || guess.contains("3") || guess.contains("4") || guess.contains("5") || guess.contains("6") || guess.contains("7") || guess.contains("8")
|| guess.contains("9") || guess.contains("0") || guess.contains("!") || guess.contains("@") || guess.contains("#") || guess.contains("$") || guess.contains("%") || guess.contains("^")
|| guess.contains("&") || guess.contains("1") || guess.contains("1") || guess.contains("1") || guess.contains("1") || guess.contains("1") || guess.contains("1") || guess.contains("1") ||
guess.contains("&") || guess.contains("*") || guess.contains("(") || guess.contains(")") || guess.contains("-") || guess.contains("_") || guess.contains("=") || guess.contains("+") ||
guess.contains("`") || guess.contains("~") || guess.contains("-") || guess.contains("[") || guess.contains("{") || guess.contains("]") || guess.contains("}") || guess.contains(";") ||
guess.contains(":") || guess.contains("'") || guess.contains(",") || guess.contains("<") || guess.contains(".") || guess.contains(">") || guess.contains("?") || guess.contains("/") ||
guess.contains("|") || guessLength != 1)
{

System.out.println("inappropriate value(s), please enter a new guess");
guess = scan.nextLine();
guess=guess.toLowerCase();
guessLength=guess.length();
}

System.out.println("Character read: " + guess);

return guess;
}
}

这就是我得到的:

Please enter a string of words
User:112
Inappropriate value(s), please enter a new value
User:The cat in the hat
Guess a letter, only alphabet letters and the space bar please,
special characters/symbols and numbers are not allowed
User:a
Original String: 55
Guessed String: 55

Characters to choose from:
abcdefghijklmnopqrstuvwxyz

预期

                            Original String: The cat in the hat
Guessed String: The cat in the hat

Characters to choose from:
abcdefghijklmnopqrstuvwxyz

最佳答案

我认为你的问题可能在于 legalGuess(guess); 行。据我了解,此方法将返回有效的猜测,但是您不会将此值分配给 guessTheString() 方法中的 guess 字符串。 legalGuess(String Guess) 方法中的 guess 变量也被命名为 guess,但它与 guess 不同guessTheString() 方法中的 code> 变量。尝试添加 guess = legalGuess(guess)。向您解释变量名称有点令人困惑,但我希望这会有所帮助。

关于java - 如何更新方法中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55996601/

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