0

假设这是一个单词猜测器,它有 5 次输入辅音的机会。我在循环中有问题,检查输入的字母是否在单词中并且它是辅音。

public class julia1 {

public static void main(String[] args) {

    System.out.print("enter text to guess: ");
    String w = Keyboard.readString();
    char c1; String temp= "";
    String asterix = "";
    boolean character  = false, vowel = false, consonant =false, number= false;

    for(int c = 0; c < w.length(); c++){
        if(w.charAt(c)==(' ')) asterix = asterix + " ";
        else asterix = asterix + "*";
    }

    System.out.println(asterix);
    for (int trys = 0; trys <=5; trys++){ 
        temp=""; 
        System.out.print("enter a consonant: ");
        c1 = Keyboard.readChar();

        for (int i = 0; i < w.length(); i++)
        {

            if (w.charAt(i) >= 'a' &&w.charAt(i)<='z')
                character = true;

            if (w.charAt(i) >= 'A' && w.charAt(i)<='Z')
                character = true;

            if (character == true){
                switch (w.charAt(i)){
                    case 'a': case 'A': case 'o': case 'O':
                    case 'e': case 'E':
                    case 'i': case 'I':
                    case 'u': case 'U': vowel = true; break;
                    default : consonant = true;
                }       
                if (c1 >= '0' && c1 <='9')
                number=true;        


            }
        }

        for(int c = 0; c < w.length(); c++){ 
            if((w.charAt(c)==c1) && (consonant == true ))
                temp = temp + c1;
            else if (vowel==true) {
                temp = temp + asterix.charAt(c);
                System.out.println("this is a vowel not consonant");
            }
            else {
                System.out.println("this is not a valid letter");
            }     
        }
        asterix = temp; 
        System.out.println(asterix) ;
    } 
}
}

这是输入第一个字母后的输出。

         Player 1 enter text to guess: hello everyone
         ***** ********
         Player 2 enter a consonant: h
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         this is a vowel not consonant
         h**** ********
         Player 2 enter a consonant:

我知道它这样做是因为它对短语中的每个字母都重复。我怎么解决这个问题?

4

1 回答 1

0

我看起来您正在尝试检查字符输入(辅音猜测)是否有有效的辅音输入,但这不是您正在检查的内容,并且还有一些问题是您正在检查输入字符串中的字符的猜测.

我认为,如果您更刻意地命名变量,它将对您有很大帮助。我很难弄清楚哪些变量名为asterix, w, c, 和c1意思,个人,这也可能导致你的一些困惑。

我已将这些名称替换为对我更有意义的名称,并在我看到问题时提供了一些评论。我不相信我已经修改了逻辑。希望这能让你到达某个地方。

public class julia1 {

public static void main(String[] args) {

    System.out.print("enter text to guess: ");
    String phraseToSearch = Keyboard.readString();
    char guessedConsonant; String temp= "";
    String displayWithAsterix = "";
    boolean character  = false, vowel = false, consonant =false, number= false;

    for(int charIndex = 0; charIndex < phraseToSearch.length(); c++){
        if(phraseToSearch.charAt(charIndex)==(' ')) displayWithAsterix = displayWithAsterix + " ";
        else displayWithAsterix = displayWithAsterix + "*";
    }

    System.out.println(displayWithAsterix);
    for (int trys = 0; trys <=5; trys++){ 
        temp=""; 
        //Warning!  When trys = 2, you haven't reset the boolean flags 
        //from the previous iteration!
        //If you entered '2', after a letter last time, then after the checks below, you would have:
        //character = true
        //vowel = false
        //consonant = true
        //number = true
        System.out.print("enter a consonant: ");
        guessedConsonant = Keyboard.readChar();

        //It looks like you want to validate that the guess is a consonant.
        //You don't need a loop to view a single char,
        //and w, or what I have renamed: phraseToSearch
        //is not the guessd char.
        for (int i = 0; i < phraseToSearch.length(); i++)
        {

            if (phraseToSearch.charAt(i) >= 'a' && phraseToSearch.charAt(i)<='z')
                character = true;

            if (phraseToSearch.charAt(i) >= 'A' && phraseToSearch.charAt(i)<='Z')
                character = true;

            if (character == true){
                switch (phraseToSearch.charAt(i)){
                    case 'a': case 'A': case 'o': case 'O':
                    case 'e': case 'E':
                    case 'i': case 'I':
                    case 'u': case 'U': vowel = true; break;
                    default : consonant = true;
                }        
                if (guessedConsonant >= '0' && guessedConsonant <='9')
                number=true;        


            }
        }

        for(int charIndex = 0; charIndex < phraseToSearch.length(); c++){ 
            if((phraseToSearch.charAt(charIndex)==guessedConsonant) && (consonant == true ))
                //Great, we're adding a hit guess to the output
                temp = temp + guessedConsonant;
            else if (vowel==true) {
                //And an whatever is in the developing display string if it's a vowel, good.
                temp = temp + displayWithAsterix.charAt(charIndex);
                //This, and the next print, look like they should be above, validing that the input
                //is a valid consonant.
                System.out.println("this is a vowel not consonant");
            }
            else {
                System.out.println("this is not a valid letter");
                //But what about the output string, don't we still need to add to it  here?                    
            }     
                //And what if it's a consonant, but doesn't match?
                //Shouldn't you still add:
                //temp = temp + displayWithAsterix.charAt(charIndex);
        }
        displayWithAsterix = temp; 
        System.out.println(displayWithAsterix) ;
    } 
}
}
于 2013-01-10T17:33:43.680 回答