-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pr16Number0To100ToText.java
63 lines (58 loc) · 1.66 KB
/
Pr16Number0To100ToText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package L03SimpleConditionalStatements;
import java.util.Scanner;
/**
* Created by Martin on 2/6/2017.
*/
public class Pr16Number0To100ToText {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = Integer.parseInt(scanner.nextLine());
String[] onesInEnglish = new String[]{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
String[] tensInEnglish = new String[]{
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
};
if ((num < 0) || (num > 100)) {
System.out.println("invalid number");
} else if (num < 20) {
System.out.println(onesInEnglish[num]);
} else if (num < 100) {
int tens = (num / 10) - 2;
int ones = num % 10;
if (ones > 0) {
System.out.printf("%s %s%n", tensInEnglish[tens], onesInEnglish[ones]);
} else {
System.out.println(tensInEnglish[tens]);
}
} else {
System.out.println("one hundred");
}
}
}