gpt4 book ai didi

java - Java输出下划线的逻辑错误

转载 作者:行者123 更新时间:2023-12-02 01:44:39 25 4
gpt4 key购买 nike

我想编写一个代码,将字符串“thing”中的所有内容都输出为下划线,除了存储在guessChar中的字母之外。目前,该代码是在其自身之上添加的。谁能帮我解决这个逻辑错误?目前,代码给出了一长串下划线。所需的输出是“_e___ _____ E”。

String thing  = "Hello World E";
String phrase = "";
String guessChar = "e";
String phraseCorrectGuessList = "";
boolean l = true;

String alphabet = "abcdefghijklmnopqrstuvwxyz";//String that holds the alphabet that is later used as indexing
phrase = thing.toLowerCase();//String that holds the phrase (lower case)
String hiddenPhrase = "";//String that holds the parts of the phrase that has not yet been unvealed
String phraseOnlyChar = "";//String that holds phrase without all the punctuation
String hiddenPhraseOnlyChar = "";//String that hold the parts of the phrase that has not yet been unvealed without all the punctuation

for (int i = 0; i < thing.length(); i++){
for (int pos = 0; pos < phrase.length(); pos++){//for loop that checks if there is a space or not
if (alphabet.indexOf(phrase.charAt(pos)) >= 0){//if there is a letter
hiddenPhrase = hiddenPhrase + "_ ";//saves to hidden phrase (changes letters to underscrore)
phraseOnlyChar += phrase.charAt(pos);
}//end of if
else{
hiddenPhrase = hiddenPhrase + phrase.charAt(pos) + " ";//saves area as punctuation
}//end of else
}//end of for loop

if (phrase.indexOf(guessChar) >= 0){//if user enters in a consonant that is in the phrase
System.out.println("Congratulations, you are correct");//prints out that the user guessed correctly
phraseCorrectGuessList += guessChar;//adds character to correctly guessed letters string

System.out.println(hiddenPhrase);//prints the phrase that is hidden
}

for (int count = 0; count < phrase.length(); count++){//for loop that goes through every letter in the phrase checks to see if the user inputted character is any character in the phrase
if (alphabet.indexOf(phrase.charAt(count)) >= 0 && phraseCorrectGuessList.indexOf(phrase.charAt(count)) < 0){//if user inputted character isn't in the phrase
hiddenPhrase = hiddenPhrase + "_ ";//saves hidden phrase as is
}//end of if

else{//if user inputted character is in the phrase
hiddenPhrase = hiddenPhrase + phrase.charAt(count) + " ";//saves hidden phrase but with all instances of that character revealed
}//end of else

}//end of for loop

for (int index = 0; index < hiddenPhrase.length(); index++){
if (alphabet.indexOf(hiddenPhrase.charAt(index)) >= 0){
hiddenPhraseOnlyChar += hiddenPhrase.charAt(index);
}

}

}

最佳答案

实际上有一种更简单的方法来做到这一点,

public static void main(String[] args) {
String thing = "Hello World E";
String phrase = "";
char guessChar = 'e';
String finalstr = "";
phrase = thing.toLowerCase();
for (int i = 0; i < thing.length(); i++){
char test = phrase.charAt(i);
if (test == ' ')
{
finalstr += " ";
}
else if (test == guessChar)
{
finalstr += thing.charAt(i);
}
else
{
finalstr += "_";
}
}
System.out.println(finalstr);
}

输出

_e___ _____ E

关于java - Java输出下划线的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53860876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com