ユーザーが入力した特定の種類の文字の数をカウントするプログラムを作成することになっています。記号以外の大文字、小文字、数字(0
~9
)、その他の文字の数#
がカウントされます。ユーザーは # を入力して終了します。
import java.util.Scanner;
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: ");
sym = input.next().charAt(0);
if (sym >= 'a' && sym <= 'z')
{
low++;
}
}
System.out.printf("Number of lowercase letters: %d\n", low);
}
}
それは私がこれまでに小文字の数について持っているものです。問題は、プログラムを実行して小文字を 4 文字入力すると、3 文字しかカウントされないことです。