-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoman.java
67 lines (51 loc) · 1.62 KB
/
Roman.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
64
65
public class Roman {
/*
int I = 1;
int IV = 4;
int V = 5;
int IX = 9;
int X = 10;
int XL = 40;
int L = 50;
int XC = 90;
int C = 100;
int CCC = 300;
int CD = 400;
int D = 500;
int CM = 900;
int M = 1000;
*/
static String N = "";
static String roman(int n) {
// Declare values
int[] figures = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] numerals = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
for (int i = 0; i < figures.length; i++)
if (n >= figures[i]) {
return numerals[i] + roman(n - figures[i]);
}
// Base case
if (n == 0) {
return N;
}
return "";
}
public static void main(String[] args) {
// Behandlung fehlender oder falscher Eingabeparameter
try {
// Try to parse the first argument as an int
int N = Integer.parseInt(args[0]);
if ( 1 <= N && N <= 5000 ) { // && !input.matches("^0+$") ) {
// System.out.println(N); // Print if value is assigned right
System.out.println(roman(N)); // Returns roman String of equivalent argument N
} else {
System.out.println("Die Zahl muss zwischen 1 und 5000 liegen.");
System.exit(2);
}
} catch (Exception ex) {
// If first argument cannot be parsed to int
System.out.println("Bitte eine Zahl als Parameter angeben.");
System.exit(2);
}
}
}