【问题标题】:Roman number to decimal in JavaJava中的罗马数字转十进制
【发布时间】:2014-12-07 09:17:59
【问题描述】:

我必须编写一个将罗马数字转换为十进制的程序。我对如何编写罗马数字的条件感到困惑,例如 IV (4)、IX (9)、XL (40) 和 CM(900)。我编写的代码适用于所有其他数字。

public static void main(String[] args) {

    System.out.print("Enter a roman numeral: ");
    Scanner in = new Scanner(System.in);
    String Roman = in.next();
    int largo = Roman.length();
    char Roman2[] = new char[largo];
    int Roman3[] = new int[largo];

    for (int i = 0; i < largo; i++) {
        Roman2[i] = Roman.charAt(i);
    }

    for (int i = 0; i < largo; i++) {
        if (Roman2[i] == 'I') {
            Roman3[i] = 1;
        } else if (Roman2[i] == 'V') {
            Roman3[i] = 5;
        } else if (Roman2[i] == 'X') {
            Roman3[i] = 10;
        } else if (Roman2[i] == 'L') {
            Roman3[i] = 50;
        } else if (Roman2[i] == 'C') {
            Roman3[i] = 100;
        } else if (Roman2[i] == 'M') {
            Roman3[i] = 1000;
        }
    }

    int total = 0;

    for (int m = 0; m < Roman3.length; m++) {
        total += Roman3[m];
    }

    System.out.println("The Roman is equal to " + total);
}

【问题讨论】:

    标签: java netbeans roman-numerals


    【解决方案1】:

    您可以检查前一位数字。

    比如我添加了检测IV的条件:

     if (Roman2[i]=='I'){
       Roman3[i]=1;
     } else if (Roman2[i]=='V'){
       Roman3[i]=5;
       if (i>0 && Roman2[i-1]=='I') { // check for IV
         Roman3[i]=4;
         Roman3[i-1]=0;
       }
     } else if (Roman2[i]=='X'){
       Roman3[i]=10;
     } else if (Roman2[i]=='L'){
       Roman3[i]=50;
     } else if (Roman2[i]=='C'){
       Roman3[i]=100;
     } else if (Roman2[i]=='M'){
       Roman3[i]=1000;
     }
    

    【讨论】:

      【解决方案2】:

      如下定义枚举:

      public enum RomanSymbol {
      
        I(1), V(5), X(10), L(50), C(100), D(500), M(1000);
        private final int value;
        private RomanSymbol(final int value) {
             this.value = value;
        }
      
        public int getValue() {
            return this.value;
        }
      
        public int calculateIntEquivalent(final int lastArabicNumber, final int totalArabicResult) {
          if (lastArabicNumber > this.value) {
             return totalArabicResult - this.value;
          } else {
            return totalArabicResult + this.value;
          }  
        }
      }
      

      并像 RomanSymbol.I.getValue() 一样使用它,它将返回 1,其他类似。

      所以如果你接受用户的字符,你可以得到如下的值:

      char symbol = 'I';//lets assume this is what user has entered.
      RomanSymbol rSymbol = RomanSymbol.valueOf(String.valueOf(symbol));
      int invalue = rSymbol.getValue();
      

      如果你有像 IV 这样的字符串,那么你可以计算如下:

      int lastValue = rSymbol.calculateIntEquivalent(intValue, 0);
      lastValue = rSymbol.calculateIntEquivalent(intValue, lastValue); //and so on
      

      【讨论】:

        猜你喜欢
        • 2017-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-23
        • 2012-10-09
        • 2017-02-26
        相关资源
        最近更新 更多