gpt4 book ai didi

java - HangMan 错误输出

转载 作者:行者123 更新时间:2023-12-01 09:26:45 24 4
gpt4 key购买 nike

我似乎有一个无法解决的问题,当用户为我的程序输入完整的单词时,它每次搜索字符时都会显示,而不是仅显示整个单词,表明他们猜对了。当用户输入整个单词时,如何让它只显示单词,而不是每次搜索字符时显示?感谢您以后的回复

package assignment1Q2;

import java.io.File;
import java.io.FileNotFoundException;

import java.util.Scanner;
public class HangmanClassExample {

static Scanner keyboard = new Scanner(System.in);
static int play, size, size2;
static String word;
static String[] ARRAY = new String[0];


public static void main(String[] args) {

setUpGame();
}

public static void setUpGame() {
System.err.printf("Welcome to hangman.\n");

try {

Scanner scFile = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
String line;
while (scFile.hasNext()) {
line = scFile.nextLine();
Scanner scLine = new Scanner(line);
size++;
}
ARRAY = new String[size];
Scanner scFile1 = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
while (scFile1.hasNext()) {
String word;
line = scFile1.nextLine();
Scanner scLine = new Scanner(line);
word = scLine.next();
ARRAY[size2] = word;
size2++;
calculateGuess();
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
}

public static void calculateGuess() {

try {
do {

int random = (int) (Math.random() * ARRAY.length);
String randomWord = ARRAY[random];
String word = randomWord;
char[] ranWord = randomWord.toCharArray();
char[] dash = word.toCharArray();

int LEFT = 6;
for (int i = 0; i < dash.length; i++) {
dash[i] = '-';
System.out.print(dash[i]);
}
for (int A = 1; A <= dash.length;) {
System.out.print("\nGuess a Letter:");
String userletters = keyboard.next();;

for (int i = 0; i < userletters.length(); i++) {
char userLetter = userletters.charAt(i);
String T = Character.toString(userLetter);
for (int B = 0; B < ranWord.length; B++) {

if (userLetter == dash[B]) {
System.out.println("this '" + userLetter + "' letter already exist");
B++;
if (userLetter == dash[B-1]) {
break;
}


} else if (userLetter == ranWord[B]) {
dash[B] = userLetter;
A--;
}
}
if (!(new String(ranWord).contains(T))) {
LEFT--;
System.out.println("You did not guess a correct letter, you have " + LEFT + " OF "
+ dash.length + " trys left to guess correctly");
}

System.out.println(dash);
}
if ((new String(word)).equals(new String(dash))) {
System.out.println("\nYou have guessed the word correctly!");
break;

}

}

System.out.println("Play agian? (y/n)");
String name = keyboard.next();

if (name.equals("y")) {
play = 0;

} else {
play = 1;
return;
}
} while (play == 0);

} catch (NullPointerException e) {

}
}

}

输出:

Welcome to hangman.



--------

Guess a Letter:c

c-------

Guess a Letter:c

this 'c' letter already exist

c-------

Guess a Letter:computer

this 'c' letter already exist

c-------

co------

com-----

comp----

compu---

comput--

compute-

computer

You have guessed the word correctly!

Play agian? (y/n)

n

最佳答案

您可以使用 startsWith 来检查用户输入的字符,而不是使用两个 for 循环并检查每个字符。例如如果用户输入 comp ,您可以简单地检查 originalString.startsWith(comp) - 如果为 true,只需打印 comp 并从 OriginalString 中删除前 4 个字符。

关于java - HangMan 错误输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39771561/

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