随机生成字母, 推断是元音字母还是辅音字母.

[0,26) + 偏移量’a’就能够生成小写字母.


代码:

//: Main.java

import java.util.Random;

/**
* 推断元音辅音
*/
class Main {
public static void main(String[] args) {
Random rand = new Random(47);
for (int i=0; i<10; i++) {
int c = rand.nextInt(26) + 'a';
System.out.println((char)c + ", " + c + ": ");
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("vowel");
break;
case 'y':
case 'w':
System.out.println("Sometimes a vowel");
break;
default:
System.out.println("consonant");
}
}
}
}
/**
* Output:
y, 121:
Sometimes a vowel
n, 110:
consonant
z, 122:
consonant
b, 98:
consonant
r, 114:
consonant
n, 110:
consonant
y, 121:
Sometimes a vowel
g, 103:
consonant
c, 99:
consonant
f, 102:
consonant
*///:~

Java - 推断元音辅音-LMLPHP

05-11 09:41