假设这是一个单词猜测器,它有 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:
我知道它这样做是因为它对短语中的每个字母都重复。我怎么解决这个问题?