【发布时间】:2018-07-11 18:40:23
【问题描述】:
如果我有一个存储问题答案的字符串和一个用于用下划线隐藏答案的 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
【问题讨论】:
-
这里有什么问题?我的意思是,String 是不可变的,所以你可能想要使用
StringBuilder,但除此之外,我相信你可以处理这个。 -
好吧,我正在尝试创建一个新字符串,该字符串将复制答案字符串,但会显示用户的猜测。例如,用户猜测 e,那么新字符串将打印出旧字符串,只是显示 e