-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntToRoman.java
More file actions
61 lines (57 loc) · 1.67 KB
/
Copy pathIntToRoman.java
File metadata and controls
61 lines (57 loc) · 1.67 KB
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
package array;
/**
* @author qiaoying
* @date 2018/9/28 16:26
*/
public class IntToRoman {
public static String intToRoman(int num) {
String str = "";
int i = 1, n;
while (num > 0){
n = num % 10;
num /= 10;
str = intoCharacter(n , i).concat(str);
i++;
}
return str;
}
public static String intoCharacter(int n, int i){
String str1, str2, str3;
String str = "";
if (1 == i){
str1 = "I";
str2 = "V";
str3 = "X";
}else if (2 == i){
str1 = "X";
str2 = "L";
str3 = "C";
}else if (3 == i){
str1 = "C";
str2 = "D";
str3 = "M";
}else {
str1 = "M";
str2 = "";
str3 = "";
}
switch(n){ //switch对各数字进行组合以个位上的数举例如下
case 0:break; //空
case 1:str+=str1;break; //I
case 2:str+=str1+str1;break; //II
case 3:str+=str1+str1+str1;break;//III
case 4:str+=str1+str2;break; //IV
case 5:str+=str2;break; //V
case 6:str+=str2+str1;break; //VI
case 7:str+=str2+str1+str1;break; //VII
case 8:str+=str2+str1+str1+str1;break; //VIII
case 9:str+=str1+str3;break; //IX
}
return str;
}
public static void main(String[] args){
int num = 58;
String s = intToRoman(num);
System.out.println(s);
}
}