Java Hangman,如何在秘密字符串的正确位置替换字符串的下划线 公共类Test1{ 公共静态void main(字符串[]args){ 字符串s=“ciao”; 字符串下划线=s.replaceAll(“.”,“”).trim(); 如果(s.包含(“a”)){ 对于(int i=0;i
您好,如何在下划线的正确位置替换字符串“a”?我正在做一个刽子手游戏,所以我必须实现的算法是通用的,而不仅仅是这种情况。问题是我的下划线索引不同,格式是“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。。输出为“\uuua\uu”您应该保留字符串中每个字母位置的引用。 您可以使用toCharArray方法Java Hangman,如何在秘密字符串的正确位置替换字符串的下划线 公共类Test1{ 公共静态void main(字符串[]args){ 字符串s=“ciao”; 字符串下划线=s.replaceAll(“.”,“”).trim(); 如果(s.包含(“a”)){ 对于(int i=0;i,java,algorithm,Java,Algorithm,您好,如何在下划线的正确位置替换字符串“a”?我正在做一个刽子手游戏,所以我必须实现的算法是通用的,而不仅仅是这种情况。问题是我的下划线索引不同,格式是“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。。输出为“\uuua\uu”您应该保留字符串中每个字母位置的引用。 您可以使用toCharArray方法 public class Test1 { public s
public class Test1 {
public static void main(String[] args) {
String s = "ciao";
String underscore = s.replaceAll(".", "_ ").trim();
if (s.contains("a")){
for (int i = 0; i< underscore.length(); i++){
}
}
System.out.println(underscore);
}
}
String s=“ciao”;
字符串下划线=s.replaceAll(“.”,“”).trim();
char[]sC=s.toCharArray();
如果(s)包含(“a”){
StringBuilder MyUnderline=新的StringBuilder(下划线);
对于(int i=0;i
希望有帮助,这是我的第一个答案!另一种方法,如果您不想使用
StringBuilder
类:
String s = "ciao";
String underscore = s.replaceAll(".", "_ ").trim();
char[] sC = s.toCharArray();
if(s.contains("a"){
StringBuilder myUnderscore = new StringBuilder(underscore);
for(int i = 0; i < sC.length; i++){
if(sc[i] == 'a'){
myUnderscore.setCharAt(i, 'a');
}
}
}
myUnderscore.toString();
String s=“ciao”;
字符串下划线=s.replaceAll(“.”,“”).trim();
System.out.println(下划线);
如果(s.包含(“a”)){
char[]ca=下划线.toCharArray();
对于(int i=0;i
此解决方案计算字符数组下划线中s
的字符位置
参考资料:
- 讨论
在charAt(整数索引)退房
方法。我为什么要使用此方法?您可以使用它将s
的第I个
位置的字符与用户提供的字符进行比较。如果是,请将下划线的第I个
字符替换为用户提供的任何字符。这样做可能更好:我使用猜测单词th的数组en if not guess数组中的元素do uu if有替换为单词..但字符串下划线=s.replaceAll(“.”,“”);和myunderline.setCharAt(2*i+1,'a');myunderline.toString().trim();我不能给你增加声誉,因为我有-7我不知道为什么笑..但非常感谢。。
String s = "ciao";
String underscore = s.replaceAll(".", "_ ").trim();
System.out.println(underscore);
if (s.contains("a")) {
char[] ca = underscore.toCharArray();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != 'a') {
continue;
}
int j = i * 2;
ca[j] = 'a';
}
// one way:
underscore = String.valueOf(ca);
// second way:
// underscore = new String(ca);
}
System.out.println(underscore);