需要帮助编码刽子手



这是我在计算机科学决赛中为刽子手准备的代码。我需要帮助弄清楚如何告诉玩家如果猜到已经猜到的字母,可以选择不同的字母。任何建议将不胜感激!

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,以便您可以直接添加并与猜测的字符输入进行比较。

将 letterGuesses 的类型更改为 ArrayList,然后将 append 更改为 add,如下所示。

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<Character> letterGuesses = new ArrayList<Character>();

        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.add(guessedLetter);
            }

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

相关内容

  • 没有找到相关文章

最新更新