Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输入另一个字符串后计算元音和辅音_Java - Fatal编程技术网

Java 输入另一个字符串后计算元音和辅音

Java 输入另一个字符串后计算元音和辅音,java,Java,爪哇: 使用接受字符串对象作为参数的构造函数编写一个类。 类应该有一个返回字符串中元音数的方法, 和另一个返回字符串中辅音数的方法。 (空格既不算作元音,也不算作辅音,应该忽略。) 在执行以下步骤的程序中演示该类: 要求用户输入一个字符串 该程序显示以下菜单: a。计算字符串中元音的数量 b。计算字符串中辅音的数量 c。数一数字符串中的元音和辅音 d。输入另一个字符串 e。退出程序 我已经编写了代码:什么时候可以检查我的选项d,当我输入另一个字符串时,它会给出元音和辅音计数0 Scanne

爪哇:

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

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

  • 要求用户输入一个字符串
  • 该程序显示以下菜单:
  • a。计算字符串中元音的数量

    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!");
    

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

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


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

    以下是一个工作程序,它将完成您正在寻找的功能:

    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();
        }
    
    }
    
    

    我希望这有帮助

    你的代码有很多问题。首先,在选项中使用
    switch-case
    概念,然后java字符串中有许多内置字符串可以帮助您实现这一点。例如
    .contains
    .equals
    ,等等。Kumar,最好在StackOverflow上发布一个完整的、可编译的、可运行的程序,您可以将该作业简单地解释为“一个在字符串中计算元音和辅音的程序”。如果这样做,您会得到更好的响应。谢谢,这个概念对我帮助很大,而且效果很好。从下一次开始,我一定会把这件事说清楚,我对这件事真的很陌生。所以在未来几天里我可能会问很多问题。希望通过清晰的概念得到帮助,就像我在这个问题上得到的一样。再次感谢你