`public class CheckVowelsDigits {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String: ");
String string = sc.next();
int numOfVowels = 0;
int numOfDigits = 0;
string = string.toLowerCase();
for (int i=0;i<string.length();i++){
char letter = string.charAt(i);
switch(letter){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': numOfVowels++; break;
}
if (letter >='0' && letter <='9')
numOfDigits++;
}
double perVowels = (numOfVowels*1.0/string.length())*100.0;
double perDigits = (numOfDigits*1.0/string.length())*100.0;
System.out.println("Number of vowels: " + numOfVowels + " (" + perVowels + "%)");
System.out.println("Number of digits: " + numOfDigits + " (" + perDigits + "%)");
}
}`
输出
输入字符串:test12345
元音数量:1(11.11111111111111%)
位数:5(55.55555555555556%)
但我需要
元音数:1(11.11%)
位数:5(55.56%)
答案 0 :(得分:-1)
double perVowels = Math.round(perVowels * 100.0)/ 100.0;