如果给定字符是元音,则赋值要求方法返回值 true,否则返回 false。然后通过向用户询问字符串来测试该方法,并检查用户输入的字符串的每个字符,如果元音则返回 true,否则返回 false。到目前为止,我已经创建了该方法,并创建了一个循环来读取字符串,但不幸的是,它只检查用户最初输入的字符,而不是字符串的字符。
import java.util.*;
public class Vowels{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = keyboard.next().charAt(0);
boolean result = isVowel(ch);
System.out.println(result);
System.out.println("Please enter a String: ");
String str = keyboard.next();
boolean answer = isVowel(ch);
for(int i = 0; i <= str.length(); i++){
if(str.charAt(i) == ch){
answer = true;
System.out.println(answer);
}
else{
answer = false;
System.out.println(answer);
}
}
}
public static boolean isVowel(char ch){
boolean answer;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
answer = true;
return answer;
}
else{
answer = false;
return answer;
}
}
}