我目前正在编写一个将整数转换为罗马数字的代码。我试图创建一个方法,将int的每个数字分配给十分之一或千分之一并将其转换为字符串,然后将字符串一起添加以获得最终的罗马数字字符串。但是我发现有问题,当我尝试为字符串赋值时它不允许我。这是代码,建议会很棒。
编辑 - 我试图稍微清理一下语法,更新了我的语法。我现在的主要问题是编译器声明字符串赋值是重复的,任何建议如何解决?
private static String ConverttoNumerals (int newinteger){
String Roman = "";
int number = newinteger;
int ones = number % 10;
if (ones == 1){String Roman = "I";}
else if ( ones == 2){String Roman = "II";}
else if (ones == 3){String Roman = "III";}
else if (ones == 4){String Roman = "IV";}
else if (ones == 5){String Roman = "V";}
else if (ones == 6){String Roman = "VI";}
else if (ones == 7){String Roman = "VII";}
else if (ones == 8 ){String Roman = "VII";}
else if (ones == 9){String Roman = "IX";}
else if (ones == 0){String Roman = "";
int tenths = ((number / 10) % 10);
String Romant="";
if (tenths == 1){String Romant = "X";}
else if (tenths == 2){String Romant = "XX";}
else if (tenths == 3){String Romant = "XXX";}
else if (tenths == 4){String Romant = "XL";}
else if (tenths == 5){String Romant = "L";}
else if (tenths == 6){String Romant = "LX";}
else if (tenths == 7){String Romant = "LXX";}
else if (tenths == 8 ){String Romant = "LXXX";}
else if (tenths == 9){String Romant = "XC";}
答案 0 :(得分:2)
您可以尝试使用一些String数组..可以进一步缩短并使用二维数组放入循环
public class Roman {
private static final String[] units = {"", "I","II","III","IV","V","VI","VII","VIII","IX"};
private static final String[] tens = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
private static final String[] hundreds = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
private static final String[] thousands = {"","M","MM","MMM"};
public static String ConverttoNumerals (int newinteger){
int n = newinteger;
int digit = n/1000;
String result = thousands[digit];
n = n - digit*1000;
digit = n/100;
result = result + hundreds[digit];
n = n -digit*100;
digit = n/10;
result = result + tens[digit];
n = n - digit*10;
result = result + units[digit];
return result;
}
}
答案 1 :(得分:0)
三件事:
String roman = "IX";
;
)switch
statement 答案 2 :(得分:-1)
public class Test {
public static void main(String[] args) {
System.out.println(ConverttoNumerals(53));
}
private static String ConverttoNumerals (int newinteger){
String Roman = "";
String Romant="";
String Romanh = "";
String Romanq = "";
int number = newinteger;
int ones = number % 10;
if (ones == 1){ Roman = "I";}
else if ( ones == 2){ Roman = "II";}
else if (ones == 3){ Roman = "III";}
else if (ones == 4){ Roman = "IV";}
else if (ones == 5){ Roman = "V";}
else if (ones == 6){ Roman = "VI";}
else if (ones == 7){ Roman = "VII";}
else if (ones == 8 ){ Roman = "VII";}
else if (ones == 9){ Roman = "IX";}
else if (ones == 0){ Roman = ""; }
int tenths = ((number / 10) % 10);
if (tenths == 1){ Romant = "X";}
else if (tenths == 2){ Romant = "XX";}
else if (tenths == 3){ Romant = "XXX";}
else if (tenths == 4){ Romant = "XL";}
else if (tenths == 5){ Romant = "L";}
else if (tenths == 6){ Romant = "LX";}
else if (tenths == 7){ Romant = "LXX";}
else if (tenths == 8 ){ Romant = "LXXX";}
else if (tenths == 9){ Romant = "XC";}
int hundredths = ((number/100)%10) ;
if (hundredths == 1){ Romanh = "C";}
else if (hundredths == 2){ Romanh = "CC";}
else if (hundredths == 3){ Romanh = "CCC";}
else if (hundredths == 4){ Romanh = "CD";}
else if (hundredths == 5){ Romanh = "D";}
else if (hundredths == 6){ Romanh = "DC";}
else if (hundredths == 7){ Romanh = "DCC";}
else if (hundredths == 8){ Romanh = "DCCC";}
else if (hundredths == 9){ Romanh = "CM";}
int thousandth = ((number/1000)%10);
if (thousandth == 1){ Romanq = "M";}
else if (thousandth == 2){ Romanq = "MM";}
else if (thousandth == 3){ Romanq = "MMMM";}
return Romanq+Romanh+Romant+Roman;
}
}
使用java编程时,请使用命名约定。我没有在这个程序中使用,因为我懒得编辑所有这些。 :-P