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の変数を個人的に理解するのは非常に困難であり、それがあなたの混乱につながる可能性もあります。wcc1

私はそれらの名前を私にとってより意味のある名前に置き換え、問題が発生した場合にいくつかのコメントを提供しました。ロジックを変更したとは思いません。うまくいけば、それはあなたをどこかに連れて行きます。

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 に答える