Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将单词中的字母替换为刽子手错误_Java_String_Object_Char - Fatal编程技术网

Java 将单词中的字母替换为刽子手错误

Java 将单词中的字母替换为刽子手错误,java,string,object,char,Java,String,Object,Char,这是我的演示课: Example: ???? w wwww (disguise) word (secret) what I want: ???? w w??? word import java.util.Scanner; 公共类HangmanDemo{ 公共静态void main(字符串[]args){ 字符输入; 刽子手游戏=新刽子手(); 扫描仪键盘=新扫描仪(System.in); System.out.println(game.getconvergedword()); 对于(int

这是我的演示课:

Example:
????
w
wwww (disguise)
word (secret)

what I want:
????
w
w???
word
import java.util.Scanner;
公共类HangmanDemo{
公共静态void main(字符串[]args){
字符输入;
刽子手游戏=新刽子手();
扫描仪键盘=新扫描仪(System.in);
System.out.println(game.getconvergedword());

对于(int i=0;i您的不安词是全部???,因此无论您输入什么,代码
伪装。字符(i)
将始终是一个
,因此每个?都将被替换

你想要的是
secret.charAt(i)
如下

import java.util.Scanner;
public class HangmanDemo {

    public static void main(String[] args) {
        char input;
        Hangman game = new Hangman();
        Scanner keyboard = new Scanner(System.in);
        System.out.println(game.getDisguisedWord());
        for (int i=0;i<10;i++){
            String line=keyboard.nextLine();
            input = line.charAt(0);

            game.makeGuess(input);
            game.guessCount();
            game.getDisguisedWord();
            game.isFound();
            System.out.println(game.getDisguisedWord());
            System.out.println(game.getSecretWord());
        }
    }
}
public void makeGuess(字符输入){
字符串温度;
临时=伪装;
char[]disqueisechars=disqueise.tocharray();//已添加

对于(int i=0;i您的不安词是全部???,因此无论您输入什么,代码
伪装。字符(i)
将始终是一个
,因此每个?都将被替换

你想要的是
secret.charAt(i)
如下

import java.util.Scanner;
public class HangmanDemo {

    public static void main(String[] args) {
        char input;
        Hangman game = new Hangman();
        Scanner keyboard = new Scanner(System.in);
        System.out.println(game.getDisguisedWord());
        for (int i=0;i<10;i++){
            String line=keyboard.nextLine();
            input = line.charAt(0);

            game.makeGuess(input);
            game.guessCount();
            game.getDisguisedWord();
            game.isFound();
            System.out.println(game.getDisguisedWord());
            System.out.println(game.getSecretWord());
        }
    }
}
public void makeGuess(字符输入){
字符串温度;
临时=伪装;
char[]disqueisechars=disqueise.tocharray();//已添加

对于(int i=0;i只需将循环中的replace函数替换为
converge[i]=input;
,随着循环的继续,所有更改都会根据需要进行:)

这里的问题正如JavaDevil所建议的,您正在用输入替换所有的“?”

编辑:对不起,当我说
伪装[I]
时,我在想.NET

将伪装声明为
StringBuilder
作为使用
converge.setCharAt(i,输入);
或执行
converge=converge.substring(0,i)+input+converge.substring(i+1,converge.length());

第二个版本也适用于字符串。您首先创建2个子字符串,从0到要更改位置的索引,然后添加新字符&将第二个子字符串从i+1连接到末尾

希望能有帮助


干杯

只需将循环中的replace函数替换为一个
伪装[i]=input;
随着循环的继续,所有更改都会根据需要进行:)

这里的问题正如JavaDevil所建议的,您正在用输入替换所有的“?”

编辑:对不起,当我说
伪装[I]
时,我在想.NET

将伪装声明为
StringBuilder
作为使用
converge.setCharAt(i,输入);
或执行
converge=converge.substring(0,i)+input+converge.substring(i+1,converge.length());

第二个版本也适用于字符串。您首先创建2个子字符串,从0到要更改位置的索引,然后添加新字符&将第二个子字符串从i+1连接到末尾

希望能有帮助

干杯

看看这些文档,你会发现它用newChar替换了oldChar的每个实例。这意味着每个“?”都将被“w”替换。你要做的是只替换给定位置的字符

要做到这一点,您可以使用char[]而不是字符串,并且可以轻松地用索引替换字符。要从字符串中获取char[],只需对机密词调用该方法

public void makeGuess(char input) {
    if(diquise.contains(String.valueOf(input))
    {
        System.out.println("You have already guessed " + input);
        wrongGuess++; // if you decide
        return;
    }
    String temp;
    temp=disguise;
    char[] disquiseChars = disquise.toCharArray();
    for (int i=0; i<secret.length(); i++) {
        if (secret.charAt(i)==input) 
        {
           disquiseChars[i] = input;
        }
    }
    disquise = disquiseChars;
    if (temp.equals(disguise))
        wrong++;
}
公共级刽子手{
私人字符串秘密;
私人伪装;
私人整数猜测计数;
私密性错误;
公众刽子手(){
secret=“word”;
converge=“???”。toCharArray();//查看文档,您将看到它将用newChar替换oldChar的每个实例。这意味着每个“?”都将替换为“w”。您要做的是只替换给定位置的字符

要做到这一点,您可以使用char[]而不是字符串,并且可以轻松地用索引替换字符。要从字符串中获取char[],只需对机密词调用该方法

public void makeGuess(char input) {
    if(diquise.contains(String.valueOf(input))
    {
        System.out.println("You have already guessed " + input);
        wrongGuess++; // if you decide
        return;
    }
    String temp;
    temp=disguise;
    char[] disquiseChars = disquise.toCharArray();
    for (int i=0; i<secret.length(); i++) {
        if (secret.charAt(i)==input) 
        {
           disquiseChars[i] = input;
        }
    }
    disquise = disquiseChars;
    if (temp.equals(disguise))
        wrong++;
}
公共级刽子手{
私人字符串秘密;
私人伪装;
私人整数猜测计数;
私密性错误;
公众刽子手(){
secret=“word”;
因为您的“伪装”只包含
替换(char oldchar,char newChar)
实际上替换了所有出现的
oldchar
,所以整个字符串都被替换

您真正想要的是在特定位置替换字符,为此,您可以使用
StringBuilder\setCharAt
。请参见下面的示例:

public class Hangman {

    private String secret;
    private char[] disguise;
    private int guessCount;
    private int wrong;

    public Hangman() {
        secret="word";
        disguise="????".toCharArray(); // <-- Basically this changes
        guessCount=0;
        wrong=0;
    }

    public void makeGuess(char input) {
        boolean found = false;
        for (int i=0; i<secret.length(); i++) {
            if (secret.charAt(i)==input) {
                disguise[i] = input; // <-- Basically this changes
                found = true;
            }
        }
        if (!found)
            wrong++;
    }
public void makeGuess(字符输入){
StringBuilder temp=新StringBuilder(伪装);
布尔错误猜测=真;
对于(int i=0;i由于您的“伪装”仅包含
replace(char oldchar,char newChar)
实际上替换了所有出现的
oldchar
,因此将替换整个字符串

您真正想要的是在特定位置替换字符,为此,您可以使用
StringBuilder\setCharAt
。请参见下面的示例:

public class Hangman {

    private String secret;
    private char[] disguise;
    private int guessCount;
    private int wrong;

    public Hangman() {
        secret="word";
        disguise="????".toCharArray(); // <-- Basically this changes
        guessCount=0;
        wrong=0;
    }

    public void makeGuess(char input) {
        boolean found = false;
        for (int i=0; i<secret.length(); i++) {
            if (secret.charAt(i)==input) {
                disguise[i] = input; // <-- Basically this changes
                found = true;
            }
        }
        if (!found)
            wrong++;
    }
public void makeGuess(字符输入){
StringBuilder temp=新StringBuilder(伪装);
布尔错误猜测=真;
for(int i=0;iString chartAt(i)返回非索引字符。如果替换该字符,则所有出现的字符都将被替换

如果您阅读Java文档,那么您必须了解为什么replace(char,char)方法不替换单个字符

公共字符串替换(char oldChar、char newChar)

返回替换所有出现的 此字符串中的oldChar与newChar。如果字符oldChar不匹配 出现在此字符串对象表示的字符序列中, 然后返回对该字符串对象的引用 创建表示字符序列的字符串对象 与此字符串对象表示的字符序列相同, 除了每次出现的oldChar都被替换为