import java.util.Random;
public class Test{
public static void main(String[] args){
final Random r = new Random();
String ch = "aeiouycbdfgh";
int len = r.nextInt(10) + 10;
StringBuffer sb = new StringBuffer();
for (int i=0; i<len; i++){
sb.append(ch.charAt(r.nextInt(ch.length())));
}
System.out.println("String:" + sb);
System.out.println("Vowels:");
outputVowels(sb.toString());
}
public static void outputVowels(String s){
如何制作一个for循环,将每个ch字符串的元音输出到另一行?
编辑:该程序是输出 一个 Ë 一世 Ø û
答案 0 :(得分:2)
首先准备全球元音集 - 用于快速访问:
Set<Character> vowelSet = new HashSet<>();
vowelSet.addAll(Arrays.asList('a', 'e', 'i', 'o', 'u'));
其次,您可以像这样编写扫描循环:
String str = "ahjuekjdf";
for (int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if(vowelSet.contains(c)) {
System.out.println(c);
}
}
答案 1 :(得分:1)
您可以直接使用Regex
public class Test {
public static void main(String[] args) {
String s = "Ankur";
s = s.replaceAll("[^aeiouAEIOU]", "");
System.out.println(s);
}
}
<强>输出强>
Au
答案 2 :(得分:0)
将你的循环改为
for (int i=0; i<len; i++) {
char c = sh.charAt(i);
if ((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')) {
sb.append(ch.charAt(r.nextInt(ch.length())));
}
}
答案 3 :(得分:0)
for(char v : vowel){
if(ch == v){
cout <<ch<<" is vowel";
break;
}
else{
cout <<ch<<" is consonant";
break;
}
}