我目前正在編寫一個將整數轉換爲羅馬數字的代碼。我試圖創建一個方法,將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";}
你似乎有令人難以置信的大量語法錯誤的放入一個循環。 –
分配字符串的雙引號在哪裏? –
請先編譯然後詢問。 –