入力された文を取り、母音で始まる単語だけで新しい文字列を作成する Java ラボを実行する必要があります。
例:
入力: 蒸し暑い夏の日です。
出力: Itisaand.
編集:私が得る出力はananananananです。
これが私のコードです(コメント付き)。方法は非常に簡単です。なぜこれが機能しないのかわかりません。
import java.util.*;
public class Driver {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Input: ");
String input = console.nextLine();
Class strings = new Class(input);
// class has constructor for input
int beg = 0, end = 0;
for (int j = 0; j < input.length(); j++) {
if (strings.isVowel(j) && (input.charAt(j - 1) == ' ' || j == 0))
beg = j;
// isVowel finds if there is a vowel at the location
else if (strings.endWord(j))
end = j - 1;
// endWord finds if there is a space or punctuation at the location
if (beg == end - 2)
strings.findWord(beg, end);
// findWord adds a substring of input from beg to end to the answer
}
System.out.print("\nOutput: ");
strings.printAnswer();
// printAnswer prints the answer
}
}
編集:これが私のクラスのコードです。
public class Class {
String input = "", answer = "";
public Class(String input1) {
input = input1;
}
public boolean isVowel(int loc) {
return (input.charAt(loc) == 'a' || input.charAt(loc) == 'e' || input.charAt(loc) == 'i'
|| input.charAt(loc) == 'o' || input.charAt(loc) == 'u');
}
public boolean endWord(int loc) {
return (input.charAt(loc) == ' ' || input.charAt(loc) == '.' || input.charAt(loc) == '?'
|| input.charAt(loc) == '!');
}
public void findWord(int beg, int end) {
answer = answer + (input.substring(beg, end));
}
public void printAnswer() {
System.out.println(answer + ".");
}
}