2014-04-05 26 views
0

對於某些課堂作業,我需要創建一個將羅馬數字轉換爲十進制形式的程序。只要沒有異常字符(如IV或IX),我就可以很好地轉換。我如何檢查這些例外情況?我試圖將當前字符和下一個字符都轉換爲十進制,然後比較它們,如果下一個字符(從右向左)變小,然後減去它。問題是我從這裏出來的界限錯誤。將十進制的羅馬數字例外隱藏到十進制

我當前的代碼是這樣的:

Scanner keyboard = new Scanner(System.in); 
    String roman; 
    int decimal = 0; 
    int number = 0; 

    System.out.print("Enter a Roman Numeral to convert to decimal form: "); 

    roman = keyboard.next(); 
    roman = roman.toUpperCase(); 

    for (int count = roman.length()-1; count >= 0; count--) 
    { 
     char numeral = roman.charAt(count); 
     switch (numeral){ 
     case 'I': 
     decimal = 1; 
     break; 
     case 'V': 
     decimal = 5; 
     break; 
     case 'X': 
     decimal = 10; 
     break; 
     case 'L': 
     decimal = 50; 
     break; 
     case 'C': 
     decimal = 100; 
     break; 
     case 'D': 
     decimal = 500; 
     break; 
     case 'M': 
     decimal = 1000; 
     break; 
     default: 
     System.out.println("Error: Invalid character detected."); 
     break; 
     } 
     number = number + decimal; 
    } 

    System.out.println("The decimal equivalent is: " + number); 
    System.out.println("Later!"); 

我還是個初學者,大多數的我對這樣的問題中看到的信息採用了先進的解決方案,我根本看不懂。我知道我需要比較角色,但我不知道如何以最終不會超出界限的方式來做到這一點。

編輯:解決!在發佈這個問題後,我被洞察力所震撼,並自己解決了這個問題。此代碼有效,但我將不勝感激有關如何改進它的任何見解!

Scanner keyboard = new Scanner(System.in); 
    String roman; 
    int decimal = 0; 
    int number = 0; 
    int last = 0; 

    System.out.println("This program converts Roman Numerals to decimal form."); 
    System.out.println("Note: Roman Numerals are I, V, X, L, C, D and M."); 
    System.out.println("All letters entered will be treated as capitalized."); 
    System.out.print("Enter a Roman Numeral to convert to decimal form: "); 

    roman = keyboard.next(); 
    roman = roman.toUpperCase(); 

    for (int count = roman.length()-1; count >= 0; count--) 
    { 
     char numeral = roman.charAt(count); 
     switch (numeral){ 
     case 'I': 
     decimal = 1; 
     break; 
     case 'V': 
     decimal = 5; 
     break; 
     case 'X': 
     decimal = 10; 
     break; 
     case 'L': 
     decimal = 50; 
     break; 
     case 'C': 
     decimal = 100; 
     break; 
     case 'D': 
     decimal = 500; 
     break; 
     case 'M': 
     decimal = 1000; 
     break; 
     default: 
     System.out.println("Error: Invalid character detected."); 
     System.exit(0); 
     break; 
     } 
     if (decimal >= last){ 
     number = number + decimal; 
     } 
     else { 
     number = number - decimal; 
     } 
     last = decimal; 
    } 

    System.out.println("The decimal equivalent is: " + number); 
    System.out.println("Later!"); 
+0

您發佈的代碼似乎沒有任何問題。 – aliteralmind

+0

問題是,如果一個條目IV,它會打印6而不是4,因爲它會添加I和V,而不是從V中減去I。我現在已經解決了它,然後我會立即發佈解決方案同一條船。 – user3501859

+1

你應該發佈你的工作代碼作爲答案。 – Gorgsenegger

回答

0

好,小數是##.##和整數##,那麼你或許應該改變demicalnum

設置您已經做的號碼,並添加一個檢查後面的異常字符。但首先要確保它存在:

if(this character is not the first && the previous character is an exception) 
    adjust the number as necessary 

這樣可以避免出現越界異常。

+0

感謝您的回答!你認爲它比我在編輯原始文章時使用的那個更有用嗎? – user3501859

+0

你的實現比我的建議更好,更優雅。用你的方式,根本不需要檢查'(當前字符-1)',因爲你的*當前*字符是在*之前讀入的*之前的字符。它避免了索引超出範圍例外的全部可能性。聰明。 – aliteralmind

+0

我剛剛提出了你的問題,當你發佈它時,我也會贊成你的回答。 – aliteralmind

相關問題