我找到元音數量的方法是錯誤的,但我不知道如何解決它,你能幫我嗎?這是我的程式:
import java.util.Scanner;
公共類 Array2 {
公共靜態無效主要(字串[]引數){
Scanner keyboard=new Scanner(System.in);
int vowels = 0, length = 0;
String[] values = new String[5];
for (int i = 0; i<values.length; i )
{
System.out.print("Enter the string:");
values[i] = keyboard.nextLine();
if(values[i].charAt(i) == 'a' ||values[i].charAt(i) == 'e' || values[i].charAt(i) == 'i' || values[i].charAt(i) == 'o' || values[i].charAt(i) == 'u')
{
vowels ;
}
}
System.out.printf("s s s s\n","Number","Value", " Length", " Number of Vowels");
int[] number = new int[5];
for(int n=0; n<number.length;n )
{
System.out.printf("s s s s\n", n 1,values[n],values[n].length(),vowels );
System.out.println();
}
} }
輸出:
Number Value Length Number of Vowels
1 wonderland 10 1
2 program 7 1
3 school 6 1
4 hello 5 1
5 mouse 5 1
我不知道為什么我的元音數是錯誤的。
uj5u.com熱心網友回復:
您可以定義一個函式來計算元音的數量,如下所示,然后在回圈中使用它:
int vowelsCount(String str) {
int vowels = 0;
for (int i = 0; i < str.length(); i )
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
|| str.charAt(i) == 'u')
vowels ;
return vowels;
}
完整演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String[] values = new String[5];
for (int i = 0; i < values.length; i ) {
System.out.print("Enter the string:");
values[i] = keyboard.nextLine();
}
System.out.printf("s s s s\n", "Number", "Value", " Length", " Number of Vowels");
for (int n = 0; n < values.length; n ) {
System.out.printf("s s s s%n", n 1, values[n], values[n].length(), vowelsCount(values[n]));
}
}
static int vowelsCount(String str) {
int vowels = 0;
for (int i = 0; i < str.length(); i )
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o'
|| str.charAt(i) == 'u')
vowels ;
return vowels;
}
}
輸出:
Number Value Length Number of Vowels
1 wonderland 10 3
2 program 7 2
3 school 6 2
4 hello 5 2
5 mouse 5 3
uj5u.com熱心網友回復:
使用 Java 8
您可以使用 java 8 概念嘗試這種方法,如下所示:
方法在這里:
我已經使用 Arrays.asList 將輸入陣列轉換為串列,然后將每個輸入字串轉換為字符流并找到每個字串中元音的計數。
public class Test {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String[] values = new String[5];
for (int i = 0; i < values.length; i ) {
System.out.print("Enter the string:");
values[i] = keyboard.nextLine();
}
AtomicInteger counter = new AtomicInteger(1);
System.out.printf("s s s s\n", "Number", "Value", " Length", " Number of Vowels");
Arrays.asList(values).forEach(x -> {
long count = x.chars().filter(ch -> (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' || ch == 'u')).count();
System.out.printf("s s s s\n", counter.getAndIncrement(), x, x.length(), count);
});
}
}
輸出:
Enter the string:wonderland
Enter the string:program
Enter the string:school
Enter the string:hello
Enter the string:mouse
Number Value Length Number of Vowels
1 wonderland 10 3
2 program 7 2
3 school 6 2
4 hello 5 2
5 mouse 5 3
uj5u.com熱心網友回復:
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
int vowels = 0, length = 0;
String[] values = new String[5];
for (int i = 0; i<values.length; i )
{
System.out.print("Enter the string:");
values[i] = keyboard.next();
}
System.out.printf("s s s s\n","Number","Value", " Length", " Number of Vowels");
int[] number = new int[5];
for(int n=0; n<number.length;n ) {
vowels = values[n].length() - values[n].replaceAll("[aeiouAEIOU]", "").length();
System.out.printf("s s s s\n", n 1,values[n],values[n].length(),vowels );
System.out.println();
}
}
我將 theString的長度與String沒有元音的長度進行比較。
replaceAll()期望正則運算式作為第一個引數。因為我將第二個引數留空,所以將洗掉所有元音。
輸出:
Number Value Length Number of Vowels
1 Hello 5 2
2 My 2 0
3 name 4 2
4 is 2 1
5 Bumblebee 9 4
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516175.html
標籤:爪哇数组
上一篇:Java中介面的回傳串列
