将罗马数字转换为阿拉伯语

Rod*_*292 3 java roman-numerals

我是Java的新手,我需要编写一个程序来将罗马数字转换为阿拉伯数字。

我不能使用某些功能,因为不允许更改乞讨或给定代码的结尾。我需要做所有的事情public static void main函数。

我开始在Google上搜索并开始编写代码。从现在开始,我只能将“一个字母”的数字(如X,I,V ...)转换为阿拉伯数字,但是我只能将其转换为复杂的数字(XI,CCC,IX,IV ...)。

有人能帮我吗?我真的是Java新手。这是我的第一门程序语言,我正努力理解它。

这是我的代码:

import java.util.Scanner;

class Roman {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int[] numbers = {1000, 500, 100, 50, 10, 5, 1 };
        String symbols = "MDCLXVI";

        /*******************************************
         * Complete your program
         *******************************************/

        System.out.print("Enter a roman numeral");
         final int MAX = 3999;
            Scanner keyb = new Scanner(System.in);
            String roman = keyb.next();
            roman=roman.toUpperCase();

            if(roman.matches(".*[0-9].*") || !roman.matches("[M|D|C|L|X|V|I]*")){
                System.out.println("Impossible to convert. Wrong roman numeral");
            } 

            int i = 0; //position in the string romain
            int arabic = 0; // Arabic numeral equivalent of the part of the string that
                            // has been converted so far


            int number;
            while (i < roman.length()){

                char letter = roman.charAt(i); // letter at the current position in the string


                if (letter == 'I'){
                    number = 1;
                } else if (letter == 'V'){
                    number = 5;
                } else if (letter == 'X'){
                    number = 10;
                } else if (letter == 'L'){
                    number = 50;
                } else if (letter == 'C'){
                    number = 100;
                } else if (letter == 'D'){
                    number = 500;
                } else if (letter == 'M'){
                    number = 1000;
                } else {
                    number = -1;
                }

                i++; // Move on to next position in the string

                if (i==roman.length()){
                    // There is no letter in the string following the one we have just processed.
                    // So just add the number corresponding to the single letter to arabic.

                    arabic += number;

                } else {
                    // Look at the next letter in the string.  If it has a larger Roman numeral
                    // equivalent than number, then the two letters are counted together as
                    // a Roman numeral with value (nextNumber - number).


                    number = roman.charAt(i);
                    int nextNumber = number;
                    if(nextNumber > number){
                        // Combine the two letters to get one value, and move on to next position in string.
                        arabic += (nextNumber - number);
                        i++;

                    } else {
                        // Don't combine the letters.  Just add the value of the one letter onto the number.
                        arabic += number;

                    }
                }
                System.out.println(number);
            } // end while

        /*******************************************
         * Do not change after this line.
         *******************************************/
    }
}
Run Code Online (Sandbox Code Playgroud)

spr*_*ter 5

我建议对您的单个罗马数字使用枚举。这样可以很好地封装代码。

public enum Roman {
    I(1), V(5), X(10), L(50), C(100), D(500), M(1000);
    private final int value;
    private Roman(int value) {
        this.value = value;
    }
    public int toInt() {
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

将单个罗马数字转换为整数变得微不足道。例如:

Roman.valueOf("X").toInt();
Run Code Online (Sandbox Code Playgroud)

唯一的复杂位是处理“ IX”和“ XC”类型的值。识别这些错误的简单方法是,它们是数字不按值降序排列的唯一时间。可以将其作为方法添加到枚举本身中(以继续封装):

public enum Roman {
    public boolean shouldCombine(Roman next) {
        return this.value < next.value;
    }
    public int toInt(Roman next) {
        return next.value - this.value;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在将它们放在一起:

List<Roman> romans = new ArrayList<>();
input.chars().mapToObj(Character::valueOf)
    .map(Roman::valueOf).forEach(romans::add);
int value = 0;
while (!romans.isEmpty()) {
    Roman current = romans.remove(0);
    if (!romans.isEmpty() && current.shouldCombine(romans.get(0))
        value += current.toInt(romans.remove(0));
    else
        value += current.ToInt();
}
Run Code Online (Sandbox Code Playgroud)

该代码的第一部分使用Java 8功能将字符串转换为罗马数字。让我知道您是否感到困惑,我将其转换为传统迭代。