計算機程式設計A組1030實習課練習


Problems

Problem Points AC Rate Users
Count Vowels and Consonants 10 58.8% 54

Comments


  • 0
    scu14156135  commented on Oct. 30, 2025, 12:03 p.m.

    import java.util.Scanner;

    public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int vowels = 0, consonants = 0;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
    
            // check if it's a letter
            if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
                // convert to lowercase for easy checking
                char lower = Character.toLowerCase(c);
    
                // check vowels
                if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u')
                    vowels++;
                else
                    consonants++;
            }
        }
    
        System.out.println(vowels + " " + consonants);
    }

    }


  • 0
    scu14156135  commented on Oct. 30, 2025, 12:03 p.m.

    import java.util.Scanner;

    public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); int vowels = 0, consonants = 0;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
    
            // check if it's a letter
            if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
                // convert to lowercase for easy checking
                char lower = Character.toLowerCase(c);
    
                // check vowels
                if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u')
                    vowels++;
                else
                    consonants++;
            }
        }
    
        System.out.println(vowels + " " + consonants);
    }

    }