[英]How do I replace a certrain char in a string of “****” with a char taken as input? Java
这些是该方法的说明:
// Given a character, a String containing a word, and a String containing a 'guess'
// for that word, return a new String that is a modified version of the 'guess'
// string where characters equal to the character inChar are uncovered.
// For example, given the following call:
// modfiyGuess('G',"GEOLOGY", "**O*O*Y")
// This functions should return the String "G*O*OGY".
这是我的代码:
private static String modifyGuess(char inChar, String word, String currentGuess) {
int count= 0;
int occur= 0;
while (count < word.length()-1)
{
if(!(word.charAt(count)== inChar)){
count++;
}
else if(word.charAt(count)==inChar)
{
currentGuess= currentGuess.replace(currentGuess.charAt(count), inChar);
occur++;
count++;
}
}
System.out.println("The character " +inChar +" occurs in " +occur +" positions");
System.out.println(" ");
return currentGuess;
}
由于某些原因,如果在字符串单词中找到了char inChar,则将currentGuess作为“ GGGGGG”之类的字符串返回,用inChar替换字符串中的所有字符,而不是返回诸如“ G *** G ”的内容。您可能有任何答案!
如果是我,我会选择使用for循环,而不是使用一段时间,这确实是个人喜好,但由于您需要遍历整个字符串,因此当读者使用a循环时,它可能会更直观,更易于读者理解用于。
如果条件彼此相反,也似乎是您的问题。 您检查它是否不等于char,然后检查它是否为 。 为什么不只是检查它是否为真,那么您就知道不是 。 在这里,字符串构建器用于很好地构建字符串。 如果要替换,则替换,如果不替换,我们将附加结果(名称已调整,因此您必须做些事才能使用它)。
public static String modifyGuess(char letter, String word, String result) {
int occurrences = 0;
StringBuilder builder = new StringBuilder();
for(int i = 0; i < word.length(); ++i) {
if(word.charAt(i) == letter) {
builder.append(letter);
occurrences++;
} else {
builder.append(result.charAt(i));
}
}
System.out.println("\nThe character " + letter +" occurs in " + occurrences +" positions\n");
return builder.toString();
}
public static void main(String[] args){
String word = "GEOLOGY";
String result = "**OLO**";
result = modifyGuess('G', word, result);
System.out.println(result);
result = modifyGuess('E', word, result);
System.out.println(result);
result = modifyGuess('Y', word, result);
System.out.println(result);
}//SSCCE1
输出:
字符G出现在2个位置
G * OLOG *
字符E出现在1个位置
GEOLOG *
字符Y出现在1个位置
地质学
有几种方法可以实现此目的。 要记住的最重要的事情是String
是不可变的,即它的内容不能更改。
在您的原始代码中,您正在使用
currentGuess= currentGuess.replace(currentGuess.charAt(count), inChar);
根据您的示例,这就像是说,将“ *”替换为“ G”,结果是"GGOGOGY"
,而不是您想要的。
相反,您需要创建某种临时位置,例如,可以从中建立新的“猜测”。
public static String modifyGuess(char inChar, String word, String currentGuess) {
String newGuess = "";
for (int index = 0; index < word.length(); index++) {
if (word.charAt(index) == inChar) {
newGuess += word.charAt(index);
} else {
newGuess += currentGuess.charAt(index);
}
}
return newGuess;
}
现在,尽管可行,但是在创建大量临时String
对象时遇到了问题。 在小型,短期的应用程序中,这可能不是问题,但是在大型,运行时间较长的应用程序中,这会成为问题,因为它会随着时间的流逝而降低性能,相反,您可以采取一些措施...
public static String modifyGuess(char inChar, String word, String currentGuess) {
char[] newGuess = new char[word.length()];
for (int index = 0; index < word.length(); index++) {
if (word.charAt(index) == inChar) {
newGuess[index] = word.charAt(index);
} else {
newGuess[index] = currentGuess.charAt(index);
}
}
return new String(newGuess);
}
每次创建时,只会创建一个新的String
。 我对此可能唯一的问题是, word
长度可能newGuess[index] = currentGuess.charAt(index);
currentGuess
/您可以通过更改newGuess[index] = currentGuess.charAt(index);
来防止出现此情况newGuess[index] = currentGuess.charAt(index);
以newGuess[index] = "*"
为例...
最后,我的首选方法是使用StringBuilder
,因为它具有char
数组的优点,因为它不会创建很多临时String
,但是不受char
数组的限制,例如
public class GuessString {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(modifyGuess('G',"GEOLOGY", "**O*O*Y"));
}
public static String modifyGuess(char inChar, String word, String currentGuess) {
StringBuilder sb = new StringBuilder(currentGuess);
for (int index = 0; index < word.length(); index++) {
if (word.charAt(index) == inChar) {
sb.replace(index, index, Character.toString(inChar));
}
}
return sb.toString();
}
}
基本上,我使用currentGuess
并使用StringBuilder
创建它的副本。 然后,我继续执行匹配检查,在找到单个匹配项时将其替换
我想我理解你的问题。 如果我错了,请纠正我:您有一个正确答案的字符串和一个代表到目前为止的猜测的字符串。 我假设“ *”是通配符,表示任何字母。 下次请尝试更具体一些,以便我们更快地答复。 试试下面的代码(它可以正确地与您的示例一起使用):
private static String modifyGuess(char inChar, String word, String currentGuess) {
int occurences = 0;
char[] wordArray = word.toCharArray();
char[] guessArray = currentGuess.toCharArray();
for (int i = 0; i < guessArray.length; i++) {
if (guessArray[i] == '*') {
if (wordArray[i] == inChar) {
guessArray[i] = inChar;
occurences++;
}
}
}
System.out.println("The character " + inChar + " occurs in " + occurences + " positions.");
System.out.println(" ");
return new String(guessArray);
}
此解决方案获取word
和currentGuess
的char
数组。 然后,它遍历数组或currentGuess
并检查当前字符是否为通配符。 如果是,它将检查word
的相同位置是否与inChar
相同。 这意味着该字符的猜测是正确的。 然后,它更新guess
以将通配符更改为inChar
并增加出现次数。 您也可以使用StringBuilder
,但是我不确定是否允许这样做。
顺便说一下,无论它们在哪里, String.replace(char, char)
将所有“ *”替换为“ G”。
希望这对您有所帮助! :)
猜猜你迟到了功课吗?
//The StringBuilder lets you set a character at a given index
StringBuilder b = new StringBuilder(currentGuess);
//Let's look at every single character in word...
for(int i = 0; i < word.length(); i++) {
//If the character in word is equal to the character to be uncovered, ...
if(word.charAt(i) == inChar) {
//we change the character in the resulting word.
b.setCharAt(i, inChar);
}
}
//And return the result
return b.toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.