このプログラムは、ユーザーが入力した文字数をカウントすることになっています。other は、!、@、$ などの他の文字です。# をカウントすることは想定されていません。これを行うための私のコードは次のとおりです。
public class countchars {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
char sym;
int up = 0;
int low = 0;
int digit = 0;
int other = 0;
System.out.print("Enter a character # to quit: ");
sym = input.next().charAt(0);
while (sym != '#') {
System.out.print("Enter a character # to quit: ");
if (sym >= 'a' && sym <= 'z') {
low++;
}
if (sym >= 'A' && sym <= 'Z') {
up++;
}
if (sym >= '0' && sym <= '9') {
digit++;
}
if (sym >= '!' && sym <= '=') {
other++;
}
sym = input.next().charAt(0);
}
System.out.printf("Number of lowercase letters: %d\n", low);
System.out.printf("Number of uppercase letters: %d\n", up);
System.out.printf("Number of digits: %d\n", digit);
System.out.printf("Number of other characters: %d\n", other);
}
}
問題は「その他」カウンターにあります。!、@、$ を入力すると、入力した 3 文字のうち 2 文字だけがカウントされます。何が悪いの?