【发布时间】:2019-12-04 21:19:21
【问题描述】:
JAVA:
编写一个具有构造函数的类,该构造函数接受 String 对象作为其参数。 该类应该有一个返回字符串中元音数量的方法, 以及返回字符串中辅音数量的另一种方法。 (空格既不算元音也不算辅音,应该忽略。)
在执行以下步骤的程序中演示该类:
- 要求用户输入字符串。
- 程序显示以下菜单:
一个。计算字符串中元音的个数。
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