有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中用猜测的字母替换破折号

这里是一个真正的javascript初学者,创建一个刽子手游戏。我很难向用户展示正确的猜测。 据我所知,我只是用破折号来掩盖这个秘密的单词,所以当猜对了一个正确的字母时,我试图让它暴露出来。 我想我需要在某个地方使用charAt,但老实说,我就是想不出来

我的代码仍然是非常基本的,我没有做太多的其他事情,因为如果你看不到猜测,那么写下游戏的其余部分就没有多大意义,但这是我到目前为止的代码。。。请记住,这仍然是一个非常未完成的项目

 package hangmangame;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
*
* @author Matt
 */
public class HangmanGame {


/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
    char letter = 0; //declares and initailise letter
    String marks = ""; //declares and initailise string for dashes
    String [] words = { "gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi" , "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
    String word = words[(int) (Math.random() * words.length)]; //chooses random word from the word array
    for (int i=1;i<=word.length(); i++) // for method for displaying the correct word as dashes
    {
        marks += "-"; //dashes to represent the correct word.
    }



        System.out.println("lets play hangman, your word is " + marks + "\n" + "enter a letter to guess the word");
        Scanner input = new Scanner(System.in);
        letter = input.next(".").charAt(0); //assign inputted letter to letter variable

     if ((word).contains(""+letter)) //if statement to excute if guessed letter is in word

         // i imagine this is where i put some sort of code to show that guessed letter?

         System.out.println("You guessed a letter!" + marks); //display for correct letter

共 (4) 个答案

  1. # 1 楼答案

    我认为,如果您将单词和标记存储为如下字符数组,这一切都会容易得多:

    char letter = 0; //declares and initailise letter
    String [] words = { "gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi" , "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
    String word = words[(int) (Math.random() * words.length)]; //chooses random word from the word array
    char[] chosenWord = new char[word.length()];
    char[] marks = new char[word.length];
    for (int i=0;i< word.length(); i++) // for method for displaying the correct word as dashes
    {
        chosenWord[i] = word.charAt(i);
        marks[i] = '-';
    }
    
    
    
        System.out.println("lets play hangman, your word is " + new String(marks) + "\n" + "enter a letter to guess the word");
        Scanner input = new Scanner(System.in);
        letter = input.next(".").charAt(0); //assign inputted letter to letter variable
    
     if ((word).contains(""+letter)){ //if statement to excute if guessed letter is in word
          for(int i =0; i < chosenWord.length; i++){
                if(chosenWord[i] == letter){
                        marks[i] = chosenWord[i]
                }
          } 
         // i imagine this is where i put some sort of code to show that guessed letter?
    
         System.out.println("You guessed a letter!" + new String(marks));
    }
    

    我没有检查语法,但是你应该知道我在做什么

  2. # 2 楼答案

    String类包含一个indexOf(String c)方法,该方法返回子字符串第一次出现的索引。例如,如果worddelicious,那么word.indexOf("i")将返回3,即子字符串"i"的第一个索引

    但是子字符串"i"的所有其他出现情况又如何呢?嗯,indexOf方法很容易被覆盖以帮助实现这一点。它还有另一个版本,indexOf(String ch, int fromIndex)接受起始索引。继续我们前面的示例,如果您这次请求word.indexOf("i", 4),您将返回5,即字符串"delicious"中子字符串"i"的第一个索引,如果我们在第四个索引之前贴现每个索引

    这样想,indexOf有点像charAt的反面。charAt接受一个索引并为您提供一个字符,indexOf接受一个字符或字符串并为您提供其第一个索引

  3. # 3 楼答案

    这是我做的。 我认为在这里使用数组要好得多

    public static void main(String[] args) {
        String[] words = {"gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi", "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
        String word[] = (words[(int) (Math.random() * words.length)]).split(""); //chooses random word from the word array and creates a array of letters
        String[] marks = new String[word.length];
        Arrays.fill(marks,"-"); // creates and fills an array with dashes
        Scanner in  = new Scanner(System.in);
        String letter = "";
        int counter = 0;
        while(Arrays.toString(marks).contains("-")) {
            counter++;
            System.out.println("This is your word!: " + String.join("", marks));
            System.out.print("Guess a letter ");
            letter = String.valueOf(in.next(".").charAt(0));
            for (int i = 0; i < word.length; i++) {
                if(word[i].equals(letter)){
                    marks[i] = word[i];
                }
            }
        }
        System.out.println("Congratulations your word is " + String.join("",marks) + "You did it in " + counter + "trials");
    }
    

    }

  4. # 4 楼答案

    这里有一些代码让你开始。从用户那里获取整行内容,而不是使用next(),这一点很重要。。。除非您是一名经验丰富的程序员,并且了解next()迭代输入的方式,否则我强烈建议您使用nextLine(),因为它更易于使用

            char letter = 0; //declares and initailise letter
            String [] words = { "gluttony", "lust", "greed", "pride", "despair", "wrath", "vainglory", "rhythm", "delicious", "better", "jacuzzi" , "ironman", "captainamerica", "thor", "hulk", "spiderman", "antman", "batman"}; //declares and initailise array of words to guess
            String word = words[(int) (Math.random() * words.length)]; //chooses random word from the word array
            String [] marks = new String[word.length()];
            for (int i=0;i<word.length(); i++) // for method for displaying the correct word as dashes
            {
                marks[i] = "-"; //dashes to represent the correct word.
            }
    
            HashSet<Character> lettersGuessed = new HashSet<>(); //keep track of letters guessed
            Scanner input = new Scanner(System.in);
            String userInput = "";
            String currentWord = "";
    
            while(true)
            {
                System.out.print("Word is - ");
                currentWord = "";
                for(int i = 0; i < marks.length; i++)
                {
                    System.out.print(marks[i]);
                }
    
                System.out.print("\nGuess a letter - ");
                userInput = input.nextLine(); //always grab lines
                if(userInput.length() != 1)
                {
                    System.out.println("Invalid guess - " + userInput);
                }
                else if(lettersGuessed.contains(userInput.charAt(0)))
                {
                    System.out.println("You already guess that character - " + userInput);
                }
                else if(word.contains(userInput))
                {
                    lettersGuessed.add(userInput.charAt(0));
                    currentWord = "";
                    for(int i = 0; i < word.length(); i++)
                    {
                        if(word.charAt(i) == userInput.charAt(0))
                        {
                            marks[i] = "" + userInput.charAt(0);
                        }
                        currentWord += marks[i];
                    }
                }
    
                if(currentWord.equals(word))
                    break;
            }
    
            System.out.println("You guessed it! The word was " + word + "!");
    

    输出

    Word is - ----
    Guess a letter - l
    Word is - --l-
    Guess a letter - h
    Word is - h-l-
    Guess a letter - l
    You already guess that character - l
    Word is - h-l-
    Guess a letter - u
    Word is - hul-
    Guess a letter - tg
    Invalid guess - tg
    Word is - hul-
    Guess a letter - k
    You guessed it! The word was hulk!