我在java中创建了一个程序来计算字符串中的小写元音和大写元音。但是输出没有以正确的方式显示。
我的代码:
import java.util.Scanner;
public class Vowelscount {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
System.out.println("Give a string ");
String input=keyboard.nextLine();
char []chars=input.toCharArray();
int upperCase = 0;
int lowerCase = 0;
int vowelcount=0;
for(char c : chars) {
switch(c){
case 'a':case 'A':
case 'e':case 'E':
case 'i':case 'I':
case 'o':case 'O':
case 'u':case 'U':
vowelcount++;
break;
}
}
for (int k = 0; k < input.length(); k++) {
// Check for upper case letters.
if (Character.isUpperCase(input.charAt(k))) upperCase++;
// Check for lower case letters.
if (Character.isLowerCase(input.charAt(k))) lowerCase++;
}
System.out.printf("There are %d uppercase letters and %d lowercase letters.",upperCase,lowerCase);
System.out.printf("Number of vowels="+vowelcount);
}}
答案 0 :(得分:3)
使用java 8功能可以轻松完成:
String str = "lalfalk;jwerLSKJDFLKJWEROIURE";
long upperCase = str.chars()
.mapToObj(c -> (char) c) // getting Stream<Character> with each char in the String
.filter(c -> "AEIOU".indexOf(c) > -1) // filter just to leave the upper case vowels
.count(); // count the number of vowels found in the String
// using the same logic
long lowerCase = str.chars()
.mapToObj(c -> (char) c)
.filter(c -> "aeiou".indexOf(c) > -1)
.count();
// the total number of vowels is the sum of the number of upper and lower found in the string
long vowelcount = upperCase + lowerCase;
System.out.printf("There are %d uppercase letters and %d lowercase letters. ", upperCase, lowerCase);
System.out.printf("Number of vowels = " + vowelcount);
输出:
有5个大写字母和3个小写字母。数量 元音= 8
答案 1 :(得分:1)
你的第二个循环不会过滤辅音。首先,您需要检查字符是否为元音,然后检查它是大写还是小写。
SELECT name, sum(qty*rate) FROM salesdetails as s JOIN sales as ss on (ss.invno=s.invno)
JOIN customer as c ON (ss.customerno = c.custno) GROUP BY(c.name)
HAVING sum(qty*rate) IN (SELECT max(exp) FROM (SELECT sum(qty*rate)
AS exp from salesdetails as s JOIN sales as ss on (ss.invno = s.invno)
JOIN customer as c ON (ss.customerno = c.custno) GROUP BY(c.name) ORDER BY sum(qty*rate)) aa);
答案 2 :(得分:1)
正如其他人所提到的,你不仅要计算元音,你也可以使用select case来计算大写和小写,请查看下面的代码。我稍微修改了你的代码。
import java.util.Scanner;
public class Vowelscount {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
System.out.println("Give a string ");
String input=keyboard.nextLine();
char []chars=input.toCharArray();
int upperCase = 0;
int lowerCase = 0;
int vowelcount=0;
for(char c : chars) {
switch(c){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowelcount++;
lowerCase++;
break;
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowelcount++;
upperCase++;
break;
}
}
System.out.printf("There are %d uppercase letters and %d lowercase letters.",upperCase,lowerCase);
System.out.printf("Number of vowels="+vowelcount);
}
}
答案 3 :(得分:0)
您可以非常轻松地使用某些Java 8流执行此操作:
@Test
public void shouldCountUpperAndLowerCaseVowels() {
final String input = "Some test INPUT string";
final Long upper = input.chars().filter(i -> Arrays.asList('A','E','I','O','U').contains((char)i)).count();
final Long lower = input.chars().filter(i -> Arrays.asList('a','e','i','o','u').contains((char)i)).count();
System.out.printf("Vowels:%nUppercase: %d%nLowercase: %d%nTotal: %s%n", upper, lower, upper + lower);
}
输出:
元音:
大写:{uc}
小写:{lc}
总计:{uc + lc}