Skip to content

Commit fad746b

Browse files
committed
added switch example
1 parent c9e4bde commit fad746b

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

ch09/Vowels.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
public class Vowels {
2+
3+
public static void main(String[] args) {
4+
String s = "Ahoy!";
5+
for (int i = 0; i < s.length(); i++) {
6+
7+
// display the next character
8+
char c = s.charAt(i);
9+
System.out.print(c + " is a ");
10+
11+
// if capital, convert to lowercase
12+
if (c >= 'A' && c <= 'Z') {
13+
c += 'a' - 'A';
14+
}
15+
16+
// check if not a lowercase letter
17+
if (c < 'a' || c > 'z') {
18+
System.out.println("symbol");
19+
continue;
20+
}
21+
22+
// output the result of the letter
23+
switch (c) {
24+
case 'a':
25+
case 'e':
26+
case 'i':
27+
case 'o':
28+
case 'u':
29+
System.out.println("vowel");
30+
break;
31+
32+
case 'y':
33+
System.out.println("not sure");
34+
break;
35+
36+
default:
37+
System.out.println("consonant");
38+
break;
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)