Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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 - Fatal编程技术网

Java 是否可以将字符串中的字符替换为另一个字符串中的字符?

Java 是否可以将字符串中的字符替换为另一个字符串中的字符?,java,string,Java,String,如果我有一个存储问题答案的字符串和一个用于用下划线隐藏答案的for循环,是否可以用用户的猜测替换字符串答案中的字符并显示它,或者以某种方式更改字符串答案以仅显示用户的正确猜测?以下是我的部分代码: String answer = "December"; // this is the phrase i want hidden with //underscores for loop( int i = 0; i < answer.length(); i++){ System.out.print

如果我有一个存储问题答案的字符串和一个用于用下划线隐藏答案的for循环,是否可以用用户的猜测替换字符串答案中的字符并显示它,或者以某种方式更改字符串答案以仅显示用户的正确猜测?以下是我的部分代码:

String answer = "December"; // this is the phrase i want hidden with 
//underscores
for loop( int i = 0; i < answer.length(); i++){
System.out.println("_"); // hides phrase with underscores
System.out.print("What is your guess for this round?");
String userGuess = console.next();
char ch = answer.charAt(i);
if (userGuess.indexOf(ch) != -1) { // if it is present in phrase then reveal
// then reveal that letter

是和否。字符串是不可变的,因此您不能实际更改它们。你通常做的是用新角色复制一份。像这样

public String replace( String s, char c, int index ) {
  return s.substring( 0, index ) + c + s.substring( index+1, s.length() );
}
尽管这需要检查错误范围

不过,一个可能更好的方法是使用基本上是可变字符串的StringBuilder

public String replace( String s, char c, int index ) {
  StringBuilder sb = new StringBuilder( s );
  sb.setCharAt( index, c );
  return sb.toString();
}

这里有什么问题?我的意思是,字符串是不可变的,所以您可能会想使用StringBuilder,但除此之外,我相信您可以处理这个问题。我正在尝试创建一个新字符串,它将复制答案字符串,但会显示用户的猜测。例如,用户猜测e,然后新字符串将打印出旧字符串,只需要e的revealedPossible重复的尼斯建议。只要保留一个maskedAnswer变量,该变量是一个StringBuilder和setCharAt,无论何时显示字母。是的,OP必须隐藏原始字符串,以便他们可以比较猜测,并向用户显示带有下划线的第二个字符串。他们还必须考虑小写或大写字母,如果允许用户猜测“a”与大写字母“a”不匹配,可能会不公平,而且他们还必须检测下划线字符串何时没有更多的下划线,从而赢得比赛。