如何获得一个字符替换字符串中的部分(JAVA) [英] How to get a char to replace parts in a string (java)

查看:105
本文介绍了如何获得一个字符替换字符串中的部分(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在做我介绍CS类阵列项目,我已经成功地做​​刽子手游戏。我想我开始它关闭相当正确的,但我似乎无法掌握如何更换一个字符为一个字符串。我必须要创建一个随机单词的方法,所以这就是为什么我在我的code的方法。检查出什么,我到目前为止有:

 导入了java.util.Random;
进口java.util.Scanner中;
公共类ProjectNum2 {
//创建者的名字    公共静态无效的主要(字串[] args){        的System.out.println(欢迎来到刽子手文字游戏!);        的String [] = wordKey {
                            循环,
                            对于,
                            而,
                            Java的,
                            开关,
                            扫描器,
                            其他,
                            双,
                            整数,
                            上市,
                            静态的,
                            方法,
                            返回,
                            空值,
                            作废,
                            真正,
                            假,
                            进口,
                            串,
                            字符
                           };
        的String [] = wordSpace {
                              _ _ _ _
                              _ _ _,
                              _ _ _ _ _
                              _ _ _ _
                              _ _ _ _ _ _,
                              _ _ _ _ _ _ _,
                              _ _ _ _
                              _ _ _ _ _ _,
                              _ _ _ _ _ _ _,
                              _ _ _ _ _ _,
                              _ _ _ _ _ _,
                              _ _ _ _ _ _,
                              R _ _ _ _ _,
                              _ _ _ _
                              _ _ _ _
                              _ _ _ _
                              _ _ _ _ _
                              _ _ _ _ _ _,
                              _ _ _ _ _ _,
                              _ _ _ _ _ _ _ _ _
                             };
        的char [] =字母{'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'};        最终诠释猜测= 6;        INT索引= 0;
        指数=(INT)randomGen(wordKey,wordSpace);        扫描仪键盘=新的扫描仪(System.in);
        的System.out.println();
        System.out.print(选择一个字母或输入零猜词:);
        字符字母= keyboard.nextLine()的charAt(0);
    }    私有静态对象randomGen(字符串[] wordKey,字符串[] wordSpace){
        串gameWord;
        随机randIndex =新的随机();
        INT指数= randIndex.nextInt(wordKey.length);
        gameWord = wordSpace [指数]
        System.out.print(gameWord);
        返回(指数);    }}


解决方案

在Java中的字符串是不可变的,这意味着你不能改变它。你必须建立一个新的String对象,使用你的情况例如与string.replace(...)方法。

I am currently doing a array project in my intro CS class and I have to successfully make a game of hangman. I think I am starting it off fairly right, but I can't seem to grasp how to replace a char into a string. I have to have a method to create a random word, so that's why I have a method in my code. Check out what I have so far:

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);

    }

}

解决方案

Strings in Java are immutable, meaning that you cant change it. You have to build a new String object, using in your case for example the String.replace(...) method.

这篇关于如何获得一个字符替换字符串中的部分(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆