将String转换为字符数组

时间:2014-04-15 19:43:11

标签: java arrays string methods character

所以我正在为java编程课做一个实验室。我在jedit上使用控制台玩刽子手游戏。他们只允许一次猜一个字母。开始时,仅显示星号以显示字母数。例如,如果单词是elephant,它将显示 ********* 。一旦他们猜出字母e,它就会显示e * e *****。我创建了一个方法,返回一个字符数组,这个字符是基于单词中字母数的星号。我无法弄清楚如何用正确的字母替换星号。请帮忙!

 public static char[] asterisk(String x) {
    char[] word = new char[x.length()] ;
    for( int i=0; i< x.length(); i++) {
        word[i] = '*' ;

    }// end for loop
    return word ;
} // end asterisk method

3 个答案:

答案 0 :(得分:2)

我们宣布两个字符串。一个是单词,另一个是以一串星号开头。

String word = "hello"; 
String obfuscatedWord = "";

for (int i = 0; i < word.length; i++)
    obfuscatedWord += "*"

我们从用户那里得到猜测。我们传入混淆的单词,因为用户需要它来猜测。

char guess = getGuessFromUser(obfuscatedWord);

我们将单词,混淆的单词和猜测传递给函数并返回一个新的字符串。

static String replaceWithGuess(String word, String obfuscatedWord, char guess) {
    // Pseudocode here. You solve the rest.
    for i = 0 to word length:
        if word[i] equals guess:
            replace obfuscatedWord[i] with guess
    return obfuscatedWord;
}

现在你只需要增加猜测次数,确定用户是赢还是输等等。

答案 1 :(得分:0)

String answer="dodo";
char[] ans=new char[answer.lenth];
for(char a:ans)
{
a='*';
}


//take input from user and then do this
char[] q=answer.toCharArray();
for (int i=0;i<answer.lenth;i++)
{

if(q[i]==theinput)
{
ans[i]=theinput;
}
}

答案 2 :(得分:-1)

您应该创建一个单独的Hangman类。我在代码中添加了注释,以便您了解每个部分。以下是其背后的想法摘要:

<强>变量

  • unmasked - 这是我们存储单词的地方
  • 蒙面 - 这是我们将存储我们蒙面***版本的单词
  • 透露 - 根据用户的猜测,这将慢慢从屏蔽过渡到未屏蔽

<强>方法

  • 构造函数 - 我们将在这里创建所有变量,以适应​​新版本的屏蔽方法
  • 猜猜 - 这是我们将猜测的字母与未屏蔽的变量
  • 进行比较的地方
  • isOver-这就是我们停止询问用户猜测的方法

您的大部分问题基本上都是在 Guess 方法中回答的,我将简要解释在循环每个字符时执行的逻辑:

  1. 如果信件是*,请检查信件是否被猜到

  2. 如果猜测错误,则输入*

  3. 如果这封信已经透露,那就说明了 信(可能忽略的连续性的重要一步)

  4. ===========以下是以下代码:===========

    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
        Scanner input = new Scanner(System.in); //provides a way to ask for input
        Hangman t = new Hangman("pajama"); //see the Hangman class I created at the bottom
        System.out.println(t.masked); //returns the word "pajama" masked with *
        while (!t.isOver()) { //keep asking the user for a char until you reveal the whole word
            System.out.print("Guess a character: "); //asks the user to guess a character
            try { 
            System.out.println(t.Guess(input.nextLine().charAt(0))); //accepts first character typed
            } catch (Exception e) {
            }
        }
        System.out.println("Congradulations you won!"); //tell the user he won by revealing the word
        input.close(); //close the input
        }
    
        public static class Hangman { //this class will make your objective easier
        public final String unmasked, masked; //final means it cannot be edited after it is assigned
        private String revealing; //this will be the string we will slowly reveal based on guesses
        private StringBuilder temp = new StringBuilder(); //this allows us to build strings out of characters in an efficient manner
    
        public Hangman(String word) { //constructor takes in the word for the game
            unmasked = word; //save the word in a separate variable unmasked
            for (int i = 0; i < word.length(); i++) //loop the length of the word
            temp.append('*'); //generate a string only with * the length of the word
            masked = revealing = temp.toString();// set masked and revealing to the *** string
            temp.setLength(0); //reset our StringBuilder for later use
        }
    
        public String Guess(char g) { //this method allows the user to guess a character
            for (int i = 0; i < masked.length(); i++)//check each letter
            temp.append(revealing.charAt(i) == '*' ? unmasked.charAt(i) == g ? g //this is called a ternary operator (condition)?(if true):(if false) and it is a shorthand for if/else
                : '*'
                : revealing.charAt(i)); 
            revealing = temp.toString(); //this updates the variable revealing so we can keep track of last correct guesses
            temp.setLength(0); //reset our StringBuilder for future use
            return revealing; //return the *** string with all the previous guesses revealed
        }
    
        public Boolean isOver() { //this checks if there is any * left to reveal
            return !revealing.contains("*");
        }
        }
        }
    

    还有很多你可以修改和添加到这个类,但我想确保我主要只是涵盖了你如何用猜测字母替换*的确切问题。希望它有所帮助!