程序方法,计算元音

时间:2014-10-30 19:46:57

标签: java

我的代码工作正常,但我不知道我的代码是否在程序方法中。

我知道你应该有两种方法,但我不确定我是否采用了正确的方法。那么,你能帮帮我吗?

public static void main(String args[]){
    vowels();
}

public static void vowels(){
    Scanner sc = new Scanner(System.in);
    int countVowel=0;
    int countVowelA=0;
    int countVowelE=0;
    int countVowelI=0;
    int countVowelO=0;
    int countVowelU=0;
    char ch;
    String str;
    System.out.println("Please enter the string : ");
    str = sc.nextLine();
    System.out.println();

    for(int i = 0; i<str.length(); i ++){
        ch = str.charAt(i);
        if(ch == 'a' || ch =='A'){
            countVowelA++;                 
            countVowel++;
        }
        if(ch == 'e' || ch =='E'){
            countVowelE++;
            countVowel++;
        }
        if(ch == 'i' || ch =='I'){
            countVowelI++;
            countVowel++;
        }
        if(ch == 'o' || ch =='O'){
            countVowelO++;
            countVowel++;
        }
        if(ch == 'u' || ch =='U'){
            countVowelU++;
            countVowel++;
        }
    }
    System.out.println("Occurances of A in given string  : " +countVowelA);
    System.out.println("Occurances of E in given string  : " +countVowelE);
    System.out.println("Occurances of I in given string  : " +countVowelI);
    System.out.println("Occurances of O in given string  : " +countVowelO);
    System.out.println("Occurances of U in given string  : " +countVowelU);
    System.out.println("Number of vowels in strings are  : " +countVowel);
}

2 个答案:

答案 0 :(得分:0)

你所拥有的只是一些改进。

import java.lang.String;
import java.util.*;

class CountVowels{
    static int countVowelA=0,countVowelE=0,countVowelI=0,countVowelO=0,countVowelU=0,countConsonants=0;
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String str="";
        System.out.println("Please enter the string : ");
        str = sc.nextLine().trim();
        System.out.println();
        countVowels(str);
        printVowelsCount();
    }

    public static void countVowels(String str){
        char ch;
        for(int i = 0; i<str.length(); i ++){
            ch = str.charAt(i);
            if(ch == 'a' || ch =='A') {
                countVowelA++;
            } else if(ch == 'e' || ch =='E') {
                countVowelE++;
            } else if(ch == 'i' || ch =='I') {
                countVowelI++;
            } else if(ch == 'o' || ch =='O') {
                countVowelO++;
            } else if(ch == 'u' || ch =='U') {
                countVowelU++;
            } else if(Character.isLetter(ch)){
                countConsonants++;
            }
        }
    }

    public static void printVowelsCount(){
        System.out.println("Occurances of A in given string  : " +countVowelA);
        System.out.println("Occurances of E in given string  : " +countVowelE);
        System.out.println("Occurances of I in given string  : " +countVowelI);
        System.out.println("Occurances of O in given string  : " +countVowelO);
        System.out.println("Occurances of U in given string  : " +countVowelU);
        System.out.println("Number of vowels in strings are  : " +(countVowelA+countVowelE+countVowelI+countVowelO+countVowelU));
        System.out.println("Number of Consonants : " +countConsonants);
    }
}

答案 1 :(得分:0)

建议另一项改进,你可以不用任何循环和很少的变量

 class SourceBee{
    public static void main(String args[]){
            Scanner sc = new Scanner(System.in);
            System.out.println("Please enter the string : ");
            String str = sc.nextLine();
            System.out.println();
            printVowelsCount(str);
        }

    public static void printVowelsCount(String vowels) {
        System.out.println(vowels);
        int length = vowels.length();
        System.out.println("a count: " + (length - vowels.replace("a", "").length()));
        System.out.println("e count: " + (length - vowels.replace("e", "").length()));
        System.out.println("i count: " + (length - vowels.replace("i", "").length()));
        System.out.println("o count: " + (length - vowels.replace("o", "").length()));
        System.out.println("u count: " + (length - vowels.replace("u", "").length()));
    }
  } 

纯粹简化了计数元音部分,而不是讨论方法是否应该是