繁体   English   中英

如何获取字符以替换字符串中的部分(java)

[英]How to get a char to replace parts in a string (java)

我目前正在CS入门课程中做一个数组项目,我必须成功制作一个子手游戏。 我想我从很正确的开始,但是我似乎无法掌握如何将char替换为字符串。 我必须要有一个方法来创建一个随机单词,所以这就是为什么我的代码中有一个方法。 看看我到目前为止有什么:

import java.util.Random;
import java.util.Scanner;
public class ProjectNum2 {
//creator's name

    public static void main(String[] args) {

        System.out.println("Welcome to the Hangman Word game!");

        String[] wordKey = {
                            "loop",
                            "for",
                            "while",
                            "java",
                            "switch",
                            "scanner",
                            "else",
                            "double",
                            "integer",
                            "public",
                            "static",
                            "method",
                            "return",
                            "null",
                            "void",
                            "true",
                            "false",
                            "import",
                            "string",
                            "character"
                           };
        String[] wordSpace = {
                              "_ _ _ _",
                              "_ _ _",
                              "_ _ _ _ _",
                              "_ _ _ _",
                              "_ _ _ _ _ _",
                              "_ _ _ _ _ _ _",
                              "_ _ _ _",
                              "_ _ _ _ _ _",
                              "_ _ _ _ _ _ _",
                              "_ _ _ _ _ _",
                              "_ _ _ _ _ _",
                              "_ _ _ _ _ _",
                              "r _ _ _ _ _",
                              "_ _ _ _",
                              "_ _ _ _",
                              "_ _ _ _",
                              "_ _ _ _ _",
                              "_ _ _ _ _ _",
                              "_ _ _ _ _ _",
                              "_ _ _ _ _ _ _ _ _"
                             };
        char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

        final int guesses = 6;

        int index = 0;
        index=(int) randomGen(wordKey, wordSpace);

        Scanner keyboard = new Scanner(System.in);
        System.out.println();
        System.out.print("Choose a letter or enter zero to guess the word: ");
        char letter = keyboard.nextLine().charAt(0);
    }

    private static Object randomGen(String[] wordKey, String[] wordSpace) {
        String gameWord;
        Random randIndex = new Random();
        int index = randIndex.nextInt(wordKey.length);
        gameWord = wordSpace[index];
        System.out.print(gameWord);
        return (index);

    }

}

Java中的字符串是不可变的,这意味着您无法更改它。 您必须构建一个新的String对象,例如,使用String.replace(...)方法。

字符串是不可变的,这意味着一旦创建它们就无法更改它们。 请改用StringBuilder类。 这是一个入门的简单示例

    StringBuilder sb = new StringBuilder(30);
    sb.append("Hello Matthew");

    // append a character
    char c = '!';
    sb.append(c);

为了使您在正确的轨道上同时又不破坏所有的乐趣,以下是一种从字符串"loop"中获取的方法,而字符'o'派生并打印了字符串_oo_

class Test {                                                                 
  public static void main(String[] args) {                                   
    String word="loop";         

    char[] array = new char[word.length()];                                  
    for(int i=0; i<array.length; i++) {                                      
      array[i] = '_';                                                        
    }                                                                        

    char guess = 'o';                                                        
    for(int i=0; i<word.length(); i++) {                                     
      if(word.charAt(i) == guess) {                                          
        array[i] = guess;                                                    
      }                                                                      
    }                                                                        

    System.out.println(new String(array));                                   
  }                                                                          
}         

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM