2014-10-10 78 views
1

所以我有一個家庭作業,我必須能夠取Roman numeral,反之亦然。我知道如何從數字到字母,但是給數字的字母讓我感到困惑。我需要幫助,因爲我編寫了我的程序和羅馬數字來編號。例如,如果我嘗試做,XCIX == 99,我得到199。但如果我做的羅馬數字2014,那是有效的。另外如果我只輸入一個字母,我會得到0。我只需要幫助瞭解我需要做什麼來解決問題。將羅馬數字轉換爲數字邏輯錯誤java

import java.util.HashMap; 
import java.util.Scanner; 

public class Apweek2 { 

public static void main(String[] args) { 
    Scanner userinput = new Scanner(System.in); 
    System.out.print("enter a number to convert to roman numerals: "); 
    int input = userinput.nextInt(); 

    String[] rv = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", 
      "D", "CM", "M" }; 
    int[] values_for_rv = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 
      1000 }; 

    System.out.print("enter a roman numeral: "); 
    String roman_numeral = userinput.next(); 

    int sum = 0; 
    String two_spot = null; 
    String last_value = null; 
    for (int i = 1, j = 0; j < roman_numeral.length() 
      && i < roman_numeral.length(); i++, j++) { 

     last_value = roman_numeral.substring(j, i); 

     char roman_noodles = roman_numeral.charAt(i); 
     char raman_noodles = roman_numeral.charAt(j); 

     String roman_values = Character.toString(roman_noodles); 

     two_spot = last_value + roman_values; 

     if (two_spot.contains(rv[1])) { 
      sum = sum + values_for_rv[1]; 
     } 
     if (two_spot.contains(rv[3])) { 
      sum = sum + values_for_rv[3]; 
     } 
     if (two_spot.contains(rv[5])) { 
      sum = sum + values_for_rv[5]; 
     } 
     if (two_spot.contains(rv[7])) { 
      sum = sum + values_for_rv[7]; 
     } 
     if (two_spot.contains(rv[9])) { 
      sum = sum + values_for_rv[9]; 
     } 
     if (two_spot.contains(rv[11])) { 
      sum = sum + values_for_rv[11]; 
     } 

     if (!(two_spot.equals(rv[1])) && !(two_spot.equals(rv[3])) 
       && !(two_spot.equals(rv[5])) && !(two_spot.equals(rv[7])) 
       && !(two_spot.equals(rv[9])) && !(two_spot.equals(rv[11]))) { 

      if (raman_noodles == 'I') { 
       sum = sum + 1; 
      } 
      if (raman_noodles == 'V') { 
       sum = sum + 5; 
      } 
      if (raman_noodles == 'X') { 
       sum = sum + 10; 
      } 
      if (raman_noodles == 'L') { 
       sum = sum + 50; 
      } 
      if (raman_noodles == 'C') { 
       sum = sum + 100; 
      } 
      if (raman_noodles == 'D') { 
       sum = sum + 500; 
      } 
      if (raman_noodles == 'M') { 
       sum = sum + 1000; 
      } 

     } 

    } 
    System.out.println("converted roman numeral is: " + sum); 

    String inputconversion = inputtoroman(input); 

    System.out.print("Converted number is: " + inputconversion); 
} 

public static String inputtoroman(int x) { 

    String s1 = ""; 
    String s2 = ""; 
    String s3 = ""; 
    String s4 = ""; 
    String s5 = ""; 
    String s6 = ""; 
    String s7 = ""; 
    String s8 = ""; 
    String s9 = ""; 
    String s10 = ""; 
    String s11 = ""; 
    String s12 = ""; 
    String s13 = ""; 

    while (x >= 1000) { 
     s1 += "M"; 
     x -= 1000; 
    } 
    while (x >= 900) { 
     s2 += "CM"; 
     x -= 900; 
    } 
    while (x >= 500) { 
     s3 += "D"; 
     x -= 500; 
    } 
    while (x >= 400) { 
     s4 += "CD"; 
     x -= 400; 
    } 
    while (x >= 100) { 
     s5 += "C"; 
     x -= 100; 
    } 
    while (x >= 90) { 
     s6 += "XC"; 
     x -= 90; 
    } 
    while (x >= 50) { 
     s7 += "L"; 
     x -= 50; 
    } 
    while (x >= 40) { 
     s8 += "XL"; 
     x -= 40; 
    } 
    while (x >= 10) { 
     s9 += "X"; 
     x -= 10; 
    } 
    while (x >= 9) { 
     s10 += "IX"; 
     x -= 9; 
    } 
    while (x >= 5) { 
     s11 += "V"; 
     x -= 5; 
    } 
    while (x >= 4) { 
     s12 += "IV"; 
     x -= 4; 
    } 
    while (x >= 1) { 
     s13 += "I"; 
     x -= 1; 
    } 
    String combined = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 
      + s11 + s12 + s13; 
    return combined; 

} 
} 

回答

0

所以,我終於想通了我的問題,在我的代碼,我不得不兩次增加索引整個two_spot時,我找到了一個匹配的羅馬數字是有2個值IV一樣過一個。然後,我還必須找到最後一個值並計算它,因爲for循環只能正確使用。然後,如果它沒有找到像IV那樣的匹配值,我也必須再次增加該值兩次。因此,我通過兩個成對的字符串進行移動,並計算成對,除非字符串具有奇數長度。它計算字符串中的最後一個值,並繼續進行所有偶數值。最後,如果任何人需要幫助轉換羅馬數字,並且是java的初學者,這應該有所幫助。

import java.util.Scanner; 

public class Apweek2 { 

public static void main(String[] args) { 
    Scanner userinput = new Scanner(System.in); 
    System.out.print("enter a number to convert to roman numerals: "); 
    int input = userinput.nextInt(); 

    String[] rv = { "I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", 
      "D", "CM", "M" }; 
    int[] values_for_rv = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 
      1000 }; 

    System.out.print("enter a roman numeral: "); 
    String roman_numeral = userinput.next().toUpperCase(); 


    int sum = 0; 

    if (roman_numeral.length() % 2 == 0) { 

    } else { 
     char odd_value = roman_numeral.charAt(roman_numeral.length() - 1); 
     if (odd_value == 'I') { 
      sum = sum + 1; 
     } 
     if (odd_value == 'V') { 
      sum = sum + 5; 
     } 
     if (odd_value == 'X') { 
      sum = sum + 10; 
     } 
     if (odd_value == 'L') { 
      sum = sum + 50; 
     } 
     if (odd_value == 'C') { 
      sum = sum + 100; 
     } 
     if (odd_value == 'D') { 
      sum = sum + 500; 
     } 
     if (odd_value == 'M') { 
      sum = sum + 1000; 
     } 
    } 

    String two_spot = null; 
    String last_value = null; 
    for (int i = 1, j = 0; j < roman_numeral.length() 
      && i < roman_numeral.length(); i++, j++) { 

     last_value = roman_numeral.substring(j, i); 

     char roman_noodles = roman_numeral.charAt(i); 

     String roman_values = Character.toString(roman_noodles); 

     two_spot = last_value + roman_values; 

     if (two_spot.contains(rv[1])) { 
      sum = sum + values_for_rv[1]; 
      i++; 
      j++; 
     } 
     if (two_spot.contains(rv[3])) { 
      sum = sum + values_for_rv[3]; 
      i++; 
      j++; 
     } 
     if (two_spot.contains(rv[5])) { 
      sum = sum + values_for_rv[5]; 
      i++; 
      j++; 
     } 
     if (two_spot.contains(rv[7])) { 
      sum = sum + values_for_rv[7]; 
      i++; 
      j++; 
     } 
     if (two_spot.contains(rv[9])) { 
      sum = sum + values_for_rv[9]; 
      i++; 
      j++; 
     } 
     if (two_spot.contains(rv[11])) { 
      sum = sum + values_for_rv[11]; 
      i++; 
      j++; 
     } 

     if (!(two_spot.equals(rv[1])) && !(two_spot.equals(rv[3])) 
       && !(two_spot.equals(rv[5])) && !(two_spot.equals(rv[7])) 
       && !(two_spot.equals(rv[9])) && !(two_spot.equals(rv[11]))) { 

      for (int k = 0; k < two_spot.length(); k++) { 

       char raman_noodles = two_spot.charAt(k); 

       if (raman_noodles == 'I') { 
        sum = sum + 1; 
       } 
       if (raman_noodles == 'V') { 
        sum = sum + 5; 
       } 
       if (raman_noodles == 'X') { 
        sum = sum + 10; 
       } 
       if (raman_noodles == 'L') { 
        sum = sum + 50; 
       } 
       if (raman_noodles == 'C') { 
        sum = sum + 100; 
       } 
       if (raman_noodles == 'D') { 
        sum = sum + 500; 
       } 
       if (raman_noodles == 'M') { 
        sum = sum + 1000; 
       } 

      } 
      i++; 
      j++; 
     } 

    } 
    System.out.println("Converted roman numeral to number is: "+sum); 

    String inputconversion = inputtoroman(input); 

    System.out.print("Converted number is: " + inputconversion); 
} 

public static String inputtoroman(int x) { 

    String s1 = ""; 
    String s2 = ""; 
    String s3 = ""; 
    String s4 = ""; 
    String s5 = ""; 
    String s6 = ""; 
    String s7 = ""; 
    String s8 = ""; 
    String s9 = ""; 
    String s10 = ""; 
    String s11 = ""; 
    String s12 = ""; 
    String s13 = ""; 

    while (x >= 1000) { 
     s1 += "M"; 
     x -= 1000; 
    } 
    while (x >= 900) { 
     s2 += "CM"; 
     x -= 900; 
    } 
    while (x >= 500) { 
     s3 += "D"; 
     x -= 500; 
    } 
    while (x >= 400) { 
     s4 += "CD"; 
     x -= 400; 
    } 
    while (x >= 100) { 
     s5 += "C"; 
     x -= 100; 
    } 
    while (x >= 90) { 
     s6 += "XC"; 
     x -= 90; 
    } 
    while (x >= 50) { 
     s7 += "L"; 
     x -= 50; 
    } 
    while (x >= 40) { 
     s8 += "XL"; 
     x -= 40; 
    } 
    while (x >= 10) { 
     s9 += "X"; 
     x -= 10; 
    } 
    while (x >= 9) { 
     s10 += "IX"; 
     x -= 9; 
    } 
    while (x >= 5) { 
     s11 += "V"; 
     x -= 5; 
    } 
    while (x >= 4) { 
     s12 += "IV"; 
     x -= 4; 
    } 
    while (x >= 1) { 
     s13 += "I"; 
     x -= 1; 
    } 
    String combined = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 
      + s11 + s12 + s13; 
    return combined; 

    } 

} 
2

你從1開始,所以當你只輸入1個數字時,它根本不會進入循環。應該更改爲與roman_numeral的全長進行比較。

for (int i = 1, j = 0; j < roman_numeral.length() 
     && i <= roman_numeral.length(); i++, j++) { 

這可能會影響它如何循環,但我現在不能自己運行它。

編輯:其實,這又找,我已經可以看到它要崩潰的char roman_noodles = roman_numeral.charAt(i);

你需要添加一些額外的邏輯,如果你想它來檢查只需1個字符。