【问题标题】:Counting vowels and consonants after entering another string输入另一个字符串后计算元音和辅音
【发布时间】:2019-12-04 21:19:21
【问题描述】:

JAVA:

编写一个具有构造函数的类,该构造函数接受 String 对象作为其参数。 该类应该有一个返回字符串中元音数量的方法, 以及返回字符串中辅音数量的另一种方法。 (空格既不算元音也不算辅音,应该忽略。)

在执行以下步骤的程序中演示该类:

  1. 要求用户输入字符串。
  2. 程序显示以下菜单:

一个。计算字符串中元音的个数。

b.统计字符串中辅音的个数

c。计算字符串中的元音和辅音

d。输入另一个字符串

e。退出程序

我已经写了代码:你什么时候可以检查我的选项 d,当我输入另一个字符串时,它给元音和辅音计数 0。

   Scanner sc = new Scanner(System.in);
    System.out.println("Enter a String: ");
    String input1 = sc.nextLine();

    VowelsAndConsonants vc = new VowelsAndConsonants(input1.toLowerCase());

    System.out.println("\nWhat would you like to do? Enter:\n" + "'a' to count the vowels\n"
            + "'b' to count consonants\n" + "'c' to count both vowels and consonants\n"
            + "'d' to enter another String\n" + "'e' to exit the program");
    char input2 = sc.next().charAt(0);

    while (input2 != 'e') {

        if (input2 == 'a') {
            System.out.println("Vowels: " + vc.vowelsCount());
        } else if (input2 == 'b') {
            System.out.println("Consonants: " + vc.consonantCount());
        } else if (input2 == 'c') {
            System.out.println("Vowels: " + vc.vowelsCount());
            System.out.println("Consonants: " + vc.consonantCount());
        } else if (input2 == 'd') {
            System.out.println("Enter another string: ");
             input1 = sc.nextLine();
            vc = new VowelsAndConsonants(input1.toLowerCase());
        }
        System.out.println("\nWhat would you like to do? Enter:\n" + "'a' to count the vowels\n"
                + "'b' to count consonants\n" + "'c' to count both vowels and consonants\n"
                + "'d' to enter another String\n" + "'e' to exit the program");
         input2 = sc.next().charAt(0);

    }
    System.out.println("Have a great day!");

【问题讨论】:

  • 你的代码有很多问题。首先使用switch - case 概念作为选项,然后java String 有许多内置的字符串可以帮助你。如.contains.equals等。
  • Kumar,最好在 StackOverflow 上发布一个完整的、可编译的、可运行的程序,并且您可以简单地将分配解释为“一个计算字符串中元音和辅音的程序”。如果你这样做,你会得到更好的回应。

标签: java


【解决方案1】:

当您混合Scannernext()nextLine() 方法时,这是一个众所周知的问题。当您调用 next() 时,它会返回下一个单词,直到换行符为止,但将换行符留在缓冲区中。剩余输入的第一行现在是空行。

然后,当您调用 nextLine() 时,它会返回该换行符之前的所有字符;换句话说,它返回零个字符,一个空字符串。

如果您在调用next()nextInt()nextDouble() 等之后小心使用额外的换行符并额外调用nextLine(),那么您可以毫无问题地混合调用,但最简单的方法是在这种情况下,用户的任何输入都始终使用nextLine()

【讨论】:

  • 谢谢,这个概念对我帮助很大,而且效果很好。从下一次开始,我一定会尽量说清楚,我对此真的很陌生。所以在接下来的几天里我可能会问很多问题。希望能在清晰的概念上得到帮助,就像我得到这个一样。再次感谢您
【解决方案2】:

这是一个可以满足您要求的工作程序:

public class Test {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String: ");
        String input1 = sc.next();

        VowelsAndConsonants vc = new VowelsAndConsonants(input1.toLowerCase());




        boolean flag =true;
        while (flag) {
            System.out.println("\nWhat would you like to do? Enter:\n" + "'a' to count the vowels\n"
                    + "'b' to count consonants\n" + "'c' to count both vowels and consonants\n"
                    + "'d' to enter another String\n" + "'e' to exit the program");
            String input2 = sc.next();
            switch (input2) {
                case  "a":
                    System.out.println("Vowels: " + vc.vowelsCount());
                    break;
                case "b":
                    System.out.println("Consonants: " + vc.consonantCount());
                    break;
                case "c":
                    System.out.println("Vowels: " + vc.vowelsCount());
                    System.out.println("Consonants: " + vc.consonantCount());
                    break;
                case "d":
                    System.out.println("Enter another string: ");
                    input1 = sc.next();
                    vc = new VowelsAndConsonants(input1.toLowerCase());
                    break;
                case "e":
                    flag=false;
                    break;
                default:
                    System.out.println("wrong selection please try again");

           }
        }
        System.out.println("Have a great day!");
    }
}


class VowelsAndConsonants {
    String str;
    public VowelsAndConsonants(String str){
        this.str = str;
    }


    public int vowelsCount(){
        str = str.replaceAll("[\\W]", "");  //remove non-chars
        int strLength = str.length();
        str = str.replaceAll("[aeiou]", "");
        return strLength-str.length();
    }

    public int consonantCount(){
        str = str.replaceAll("[\\W]", "");  //remove non-chars
        int strLength = str.length();
        str = str.replaceAll("[aeiou]", "");
        return str.length();
    }

}

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    相关资源
    最近更新 更多