[英]Can not find out why my output is printing 2 times and how swap chars in StringBuilder
I am using a StringBuilder and appending it with * for each letter of the word to be guessed.我正在使用 StringBuilder 并为要猜测的单词的每个字母附加 * 。 Then when the user guesses a letter/char correct the StringBuilder should be changing the char at a certain index from * to the guessed letter/char.
然后,当用户猜测正确的字母/字符时,StringBuilder 应该将某个索引处的字符从 * 更改为猜测的字母/字符。 Then print the new StringBuilder to show the correct letter like this (ho * s *).
然后打印新的 StringBuilder 以显示正确的字母(ho * s *)。 If the guess is wrong then just print the StringBuilder and say wrong guess.
如果猜测是错误的,那么只需打印 StringBuilder 并说出错误的猜测。
I am trying to figure out why this is not working properly.我想弄清楚为什么这不能正常工作。 I am getting output like: (minus the / it wouldn't post just the *)
我得到的输出如下:(减去 / 它不会只发布 *)
HANGMAN刽子手
Try and guess the word, you have 9 attempts:试着猜这个词,你有 9 次尝试:
/*************** /****************
Guess a letter: g猜一个字母:g
/************************* /*****************************
Guess a letter: p猜一个字母:p
p****p****p****p****p**** p****p****p****p****p****
Guess a letter猜一封信
It is also printing the word more than once and I am not sure why.它也不止一次打印这个词,我不知道为什么。
import java.util.Random;
import java.util.Scanner;
public class Hangman {
static String[] words = {"house", "show", "garage", "computer", "programming", "porch", "dog"};
static char[] correct = new char[26];
static char[] wrong = new char[26];
static char guess;
static Random generator = new Random();
static Scanner input = new Scanner(System.in);
static String word;
static int attempts = 0;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args){
word = words[generator.nextInt(words.length)];
System.out.print("HANGMAN\nTry and guess the word, you have 9 attempts: \n");
printAstrick();
while(attempts <= 9){
System.out.print("\nGuess a letter: ");
guess = input.next().charAt(0);
findMatch(guess);
}
if(attempts == 9){
System.out.println("Your attempts are up");
}
}
public static void findMatch(char c){
for(int i = 0; i < word.length(); i++){
if(word.charAt(i) == c){
correct[i] = c;
sb.setCharAt(i, c);
System.out.print(sb.toString());
}
else if(word.charAt(i) != c){
wrong[i] = c;
sb.setCharAt(i, '*');
System.out.print(sb.toString());
}
}
attempts++;
}
public static void printAstrick(){
for(int i = 0; i < word.length(); i++){
sb.append("*");
System.out.print(sb.toString());
}
}
You are overwriting any correct guesses with this line:您正在使用此行覆盖任何正确的猜测:
sb.setCharAt(i, '*');
in your findMatch
method so you should remove it.在您的
findMatch
方法中,因此您应该将其删除。
Also, your print statements are in for
loops so each word is printed out n times.此外,您的打印语句在
for
循环中,因此每个单词都会打印n次。 Fix this by moving your calls to System.out.print(sb.toString())
outside of the for
loops.通过将您的调用移到
for
循环之外的System.out.print(sb.toString())
来解决此问题。
This leaves you with:这给你留下:
public static void findMatch(char c) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == c) {
correct[i] = c;
sb.setCharAt(i, c);
} else if (word.charAt(i) != c) {
wrong[i] = c;
}
}
System.out.print(sb.toString());
attempts++;
}
public static void printAstrick() {
for (int i = 0; i < word.length(); i++) {
sb.append("*");
}
System.out.print(sb.toString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.