gpt4 book ai didi

java - 需要帮助编码刽子手

转载 作者:行者123 更新时间:2023-12-01 21:28:57 26 4
gpt4 key购买 nike

这是我计算机科学期末考试的刽子手代码。我需要帮助弄清楚如何告诉玩家在猜到一个已经猜到的字母时选择不同的字母。任何建议将不胜感激!

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HangmanJava
{
public static void main(String[] args)
{
String input;
boolean NotFullMan = true;
char guessedLetter = ' ';
Scanner hm = new Scanner(System.in);
System.out.println("Enter the hangman word");
input = hm.nextLine();
List<String> letterGuesses = new ArrayList<String>();

char[] charArray = input.toCharArray();
StringBuffer buffer = new StringBuffer(input.length());
int totalCorrect = 0;

for (int i = 0; i < input.length(); i++)
buffer.append('_');

while (NotFullMan){
System.out.println("Enter a letter");
guessedLetter = hm.nextLine().charAt(0);
int correct = 0;

for (int i=0; i < charArray.length; i++){
if(guessedLetter == charArray[i] && buffer.charAt(i) == '_'){
correct++;
totalCorrect++;
buffer.setCharAt(i, guessedLetter);
}
}
System.out.println("You got " + correct + " correct!");
System.out.println(buffer.toString());

if (letterGuesses.contains(guessedLetter)){
System.out.println("Letter already chosen. Please pick another.");
}
else {
letterGuesses.append(guessedLetter);
}

if (totalCorrect == input.length()){
System.out.println("You win!");
return;
}
}
}
}

最佳答案

您需要将猜测的字母添加到之前的猜测列表中。此外,您之前的猜测列表可能应该是 List<Character>而不是String这样您就可以直接添加并与您猜测的字符输入进行比较。

关于java - 需要帮助编码刽子手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37659591/

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