构建一个Hangman游戏,我在打印用户已经猜到的字母方面遇到了问题。会发生什么是用户猜对的第一个字母总是没有问题。在第一个字母有时会进入有时没有正确的字母,有时也没有,我无法找出一个系统。它可能与延迟有关,这是可能的,因为我的电脑不是很好。
以下是您可能需要的一些变量声明。
StringBuilder orderedLetters = new StringBuilder("_____");
JLabel label = new JLabel(orderedLetters);
String word = "horse";//word is always a randomly generated 5 letter word
这是我认为麻烦的方法。
void printLetterSpaces() {
//inserts the first letter of the word and deletes the one it moved out of the way
if (isLetterSolved(0, 1)) {
orderedLetters.insert(0, word.substring(0,1));
orderedLetters.deleteCharAt(1);
}
//inserts the second letter of the word and deletes the one it moved out of the way
else if (isLetterSolved(1, 2)) {
orderedLetters.insert(1, word.substring(1,2));
orderedLetters.deleteCharAt(2);
}
//inserts the third letter of the word and deletes the one it moved out of the way
else if (isLetterSolved(2, 3)) {
orderedLetters.insert(2, word.substring(2,3));
orderedLetters.deleteCharAt(3);
}
//inserts the fourth letter of the word and deletes the one it moved out of the way
else if (isLetterSolved(3, 4)) {
orderedLetters.insert(3, word.substring(3,4));
orderedLetters.deleteCharAt(4);
}
//inserts the fifth letter of the word and deletes the one it moved out of the way
else if (isLetterSolved(4, 5)) {
orderedLetters.insert(4, word.substring(4,5));
orderedLetters.deleteCharAt(5);
}
lettersLabel.setText(orderedLetters.toString());
System.out.println(userInput.getText().substring(0, 1));
userInput.setText("");
}
以下是我在上述方法中引用的方法:
boolean isLetterSolved(int x, int y) {
if(lettersGuessedCorrectly.toString().contains(word.substring(x,y)))
return true;
else
return false;
}
在猜一封信之前: https://gyazo.com/d6d547250e0da4cc0319e1f7e5b2fc14
第一次正确猜出一封信后(O是我猜的字母):https://gyazo.com/f2d8535f8a46629ba9fa9d336faa7c2e
第二次正确猜出一封信后(r是我猜到的字母(它使jlabel转向正确猜测)):https://gyazo.com/159e7c82c3f07fc5b18df51fbf00bbef
测试字母是否正确的方法是完全正确的,因为如果不调用printLetterSpaces,JLabel就不会告诉你猜测是否正确
编辑:好吧,我解决了它,我不知道它为什么会起作用,但这是解决方法:
void printLetterSpaces() {
//inserts the first letter of the word and moves the one it replaced out of the way
if (isLetterSolved(0, 1)) {
orderedLetters.replace(0, 1, word.substring(0, 1));
}
//inserts the second letter of the word and moves the one it replaced out of the way
if (isLetterSolved(1, 2)) {
orderedLetters.replace(1, 2, word.substring(1, 2));
}
//inserts the third letter of the word and moves the one it replaced out of the way
if (isLetterSolved(2, 3)) {
orderedLetters.replace(2, 3, word.substring(2, 3));
}
//inserts the fourth letter of the word and moves the one it replaced out of the way
if (isLetterSolved(3, 4)) {
orderedLetters.replace(3, 4, word.substring(3, 4));
}
//inserts the fifth letter of the word and moves the one it replaced out of the way
if (isLetterSolved(4, 5)) {
orderedLetters.replace(4, 5, word.substring(4, 5));
}
lettersLabel.setText(orderedLetters.toString());
System.out.println(userInput.getText().substring(0, 1));
userInput.setText("");
}
答案 0 :(得分:1)
不完全相同但下面的代码使用不同的方法解决问题。
创建DTO。
public class WordDTO {
private String orignalWord;
private String newWord;
private String guessdWord;
private char inputChar;
private int position;
private boolean status;
public String getNewWord() {
return newWord;
}
public void setNewWord(String newWord) {
this.newWord = newWord;
}
public String getGuessdWord() {
return guessdWord;
}
public void setGuessdWord(String guessdWord) {
this.guessdWord = guessdWord;
}
public char getInputChar() {
return inputChar;
}
public void setInputChar(char inputChar) {
this.inputChar = inputChar;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getOrignalWord() {
return orignalWord;
}
public void setOrignalWord(String orignalWord) {
this.orignalWord = orignalWord;
}
}
从用户接收输入并生成随机单词然后要求用户猜出单词的类。 我用012345初始化了最初猜测的单词,即从0到单词的大小。您可以使用" _"
进行初始化public class GuessTheWordMain {
public static void main(String args[]) {
Scanner reader=new Scanner(System.in);
System.out.println("Enter the size of Word to guess!!!!");
int sizeOfNum=reader.nextInt();
String word=generateRndomWord(sizeOfNum);
System.out.println(word);
WordDTO wordDTO=new WordDTO();
wordDTO.setNewWord(word);
String guesWordInitial="";
for (int j=0;j<word.length();j++) {
guesWordInitial=guesWordInitial+String.valueOf(j);
}
wordDTO.setGuessdWord(guesWordInitial);
wordDTO.setPosition(-1);
for(int i=0;i<sizeOfNum;i++) {
System.out.println("Enter the Guess Number :"+i);
char guess=reader.next().charAt(0);
wordDTO.setInputChar(guess);
wordDTO=displayGuessResult(wordDTO);
System.out.println(wordDTO.getGuessdWord());
}
System.out.println();
}
public static String generateRndomWord(int size) {
Random random=new Random();
StringBuilder stringBuilder=new StringBuilder(size);
for(int i=0;i<size;i++) {
stringBuilder.append((char)('a'+random.nextInt(26)));
}
return stringBuilder.toString();
}
public static WordDTO displayGuessResult(WordDTO wordDTO) {
String newWord=wordDTO.getNewWord();
boolean status=wordDTO.isStatus();
int position=-1;
for (int i=0;i<newWord.length();i++) {
if((newWord.charAt(i))==wordDTO.getInputChar()) {
status=true;
position=i;
break;
}
}
if(status) {
wordDTO.setNewWord(newWord.replace(newWord.charAt(position), '*'));
wordDTO.setStatus(status);
wordDTO.setGuessdWord(wordDTO.getGuessdWord().replace(wordDTO.getGuessdWord().charAt(position), wordDTO.getInputChar()));
}
return wordDTO;
}
}
如有任何疑问/改善,请询问。