gpt4 book ai didi

java - 比较方法和数组

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

嘿伙计们,我一直在研究我的代码,几乎让它正常工作,但是在 numGuess 和 hideWord 方法之间的某个地方,我有一个逻辑错误,没有比较这两个方法我想要的方式。我正在尝试替换星号。所以如果用户输入是我想要的“a”星号填充“a”以进行更新。

public class Lab12 {

public static final int MAXWORD = 15000;
public static final int MAXCHAR = 20;
public static final int MAXGUESS = 8;

public static void main(String[] args) {//main
Scanner keyboard = new Scanner(System.in);
int cout = 0;
//method calling bulk of other methods
storage();

}//end main

public static int pickrandom(int count)//generates random number
{
Random generator = new Random();
return generator.nextInt(count);
}

public static int storage()//holds bulk of other methods also performs intro and reads in file
{
String wordList[] = new String[MAXWORD];
String wordLetter[] = new String[MAXCHAR];
String dictionary = "dictionary.txt";
Scanner readFileIn = null;
String dictionaryVal = " ";
int count = 0;
String cont = "";

try//Try to read in the file.
{
readFileIn = new Scanner(new File(dictionary));//creates object scanner and object file?

while (readFileIn.hasNextLine()) {
wordList[count] = readFileIn.nextLine();
count++;
}

System.out.println("");
System.out.println("H A N G M A N");
System.out.println("");
System.out.println("This is a word guessing game A word will be selected at random");
System.out.println("and kept hidden. You will try to figure out the secret word by");
System.out.println("guessing letters which you think are in the word. You will guess");
System.out.println("one letter at a time. If the letter you guess is correct, the");
System.out.println("position(s) of the letter in the secret word will be shown.");
System.out.println("You will be allowed 8 wrong guesses. If you guess incorrectly 8");
System.out.println("times, you lose the game. If you guess all of the letters in the");
System.out.println("word, you win.");
System.out.println("");

int rand = pickrandom(count);
String hiddenWord = wordList[rand];
char[] asterisks = new char[MAXCHAR];

clearScreen(cont);//clear screen method call
hideWord(hiddenWord);//hidden word method call
System.out.println((hideWord(hiddenWord)));//print out hidden word in asterisks
System.out.println("");
System.out.println("");
numGuess(hiddenWord, asterisks);//method call to number of guesses/comparisions

} catch (Exception e)//Catch error when trying to open/find the file dictionary.txt
{

System.out.println("Error can not open dictionary.txt");
}
return count;
}

public static void clearScreen(String cont)//clear screen method for user
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Press enter to continue:");
cont = keyboard.nextLine();
if (cont.equals(""));
{
for (int i = 0; i < 100; i++) {
System.out.println("");
}
}
}

public static String hideWord(String hiddenWord)//puts hidden word in asterisks
{
int wordLength = hiddenWord.length();
char[] asterisks = new char[wordLength];

for (int i = 0; i < wordLength; i++) {
asterisks[i] = '*';//couldn't figure out how to double space asterisks
}
String hideAsterisks = String.valueOf(asterisks);
return hideAsterisks;//return the value of
}

public static void numGuess(String hiddenWord, char[] asterisks)//compares guesses and counts
//attempts, however I feel like
//like I'm so close but I have a
//logic error in comparisons.
{
Scanner keyboard = new Scanner(System.in);
String hiddenword = hideWord(hiddenWord);
int remAttempts = MAXGUESS;
char attempts;
do {
System.out.println("Enter a letter or 9 to quit");
attempts = keyboard.next().charAt(0);
remAttempts--;
System.out.println("Attempts remaining: " + remAttempts);
for (int i = 0; i < hiddenWord.length() - 1; i++) {
if (attempts == (hiddenword.charAt(asterisks[i])))//trying to compare user input to *
{
System.out.println("Nice job!");

}
}
} while (attempts != '9' && remAttempts > 0);
}
}

最佳答案

在 Java 中进行字符串操作的方法有很多种。最快的方法之一可能是使用 REGEX。但我向您展示如何使用循环并检查每个字符。

String answer = "hello";
String hidden = answer.replaceAll(".", "*"); //let hidden fill with *

char guess = 'e'; //Get this from scanner

for(int x=0; x<answer.length(); x++)
if(answer.charAt(x) == guess)
hidden = hidden.substring(0,x) + guess + hidden.substring(x+1);

关于java - 比较方法和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27447304/

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