2014-03-07 16 views
-2

我目前正在編寫一個將整數轉換爲羅馬數字的代碼。我試圖創建一個方法,將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";} 
+5

你似乎有令人難以置信的大量語法錯誤的放入一個循環。 –

+0

分配字符串的雙引號在哪裏? –

+0

請先編譯然後詢問。 –

回答

0

三兩件事:

  1. 字符串文本需要加引號:String roman = "IX";
  2. 語句需要用分號(;
  3. 結束你或許應該使用switch statement
+2

4.重複變量聲明 – Baby

+0

什麼是重複變量刪除的解決方案 – user3250642

+1

@ user3250642解決方案是**不要做**重複變量聲明-_- – Baby

-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

+1

哇,真的幫了很多兩個代碼之間的區別是什麼 – user3250642

+0

請標記答案是正確的,如果它幫助你。您的代碼中的問題在之前的答案中已經指定。快樂學習Java。 –

+1

_您的代碼中的問題在之前的答案中指定。然後,以前的答案應該被標記爲接受答案:) – Baby

2

您可以嘗試使用一些字符串數組..可進一步縮短,並使用二維數組

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; 

} 
}