转换罗马数字为整数值? [英] Converting Roman Numeral to integer value?

查看:196
本文介绍了转换罗马数字为整数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的编程的学生,我的任务是罗马数字的输入转换为它的整数值。下面是我得到了什么:

I'm a new programming student and my assignment is to convert the input of a Roman Numeral to it's integer value. Here is what I have been given:

编写一个罗马数字转换,如MCMLXXVIII其十进制数再presentation的程序。这个程序必须有3种方法和主要方法!

Write a program that converts a Roman number such as MCMLXXVIII to its decimal number representation. This program must have 3 methods and a main method!


  1. 编写需要来自用户的输入,并把它传递给一个转换方法的方法。

  2. 编写产生每个字母(转换方法)的数值的方法。

  3. 写输出该用户输入的号码和转换的数目的方法。

  4. 写测试3种方法的主要手段。

提示:使用一维数组!

HINT: Use a single dimensional array!

将字符串转换如下:

•查看前两个字符。如果第一具有比第二大的值,则简单地转换第一。

• Look at the first two characters. If the first has a larger value than the second, then simply convert the first.

•用于启动与第二个字符的字符串再次调用转换方法。

• Call the conversion method again for the substring starting with the second character.

•添加这两个值。 o如果第一个具有比第二较小的值,计算的差,并添加到它的尾部的转换。

• Add both values. o If the first one has a smaller value than the second, compute the difference and add to it the conversion of the tail.

现在我在努力试图找出什么对我的转换方法做。以下是我迄今为止写的:

Now I am struggling trying to figure out what to do for my conversion method. Here is what I have written so far:

    public static String romanInput(String number) {

    Scanner numberInput = new Scanner (System.in);
    System.out.print("Enter a roman numeral: ");
    String userInput = numberInput.next();

    return userInput;
}

public static int numberConversion(int number) {

    int romanConv = 0;

    char[] romanChar = {1, 5, 10, 50, 100, 500, 1000};
    for (int i = 0; i < romanChar.length; i++)

}

您可以看到,我已经写了需要从用户输入的方式。我认为我正确地这样做。但是,我不知道该怎么为这个转换方法做。它说,使用单一维数组所以这就是我所做的在这里:

You could see that I have already written the method that takes the input from a user. I think I did this correctly. However, I don't know what to do for this conversion method. It says to use a single dimensional array so that's what I did over here:

char[] romanChar = {1, 5, 10, 50, 100, 500, 1000};

这些都应该是I,V,X,L,C,D的值,M。我真的只是困惑在哪里从那里,我将AP preciate,如果有人能帮助我。

Those are supposed to be the values of I, V, X, L, C, D, and M. I'm really just confused as where to go from there and I would appreciate it if someone can help me out.

推荐答案

如果我是你,我会开始采取一次一小步。例如,如果我担心唯一的输入是我,然后呢?这是微不足道的,当然。

If I were you, I would begin by taking one baby step at a time. For example, if the only input I have to worry about is "I", then what? That is trivial of course.

接下来,如果输入的是II,然后呢?这表明,我需要处理在一个时间输入一个字符。无论是我s为等于每一个,其结果是两个的总和。这意味着,我必须有结果或一些这样的变量,初始化为零,然后用于从输入字符串中的每个字符(Ⅰ,那么I)中,将其转换为数字值(1,然后1),加入他们和返回值。

Next, if the input is "II", then what? This suggests that I need to process the input one character at a time. Both the "I"s are equal to one each, and the result is the sum of the two. That means, I must have a "result" or some such variable, initialized to zero, and then for each character from the input string (I, then I), convert that to its numeric value (1, and then 1), add them up and return the value.

这个逻辑非常适用于三也。

This logic works well for "III" also.

但你面对你与IV的第一个挑战。这是不平凡的,特别是如果你是新来这样的算法。让我把它放在一边,用一张纸条,那是艰难的所以会处理这一点。

But then you face your first challenge with "IV". That is not trivial, specially if you are new to such an algorithm. Let me keep it aside, with a note that that is tough so will deal with this later.

值V,六号,七,八都做工精细上述逻辑。

The values "V", "VI", "VII", "VIII" all work fine with the above logic.

但话又说回来,我将坚持IX。类似的IV的上方。也许我有两个想法了,但那时,也许我会依然保持双方这些不谈暂且。

But then again I would be stuck with "IX". Similar to "IV" above. Maybe I have an idea about these two now, but then, maybe I'll still keep both these aside for the time being.

这工作正常的X,十一,十二,十三,然后再次问题十四。

This works fine for "X", "XI", "XII", "XIII", and then again problem with "XIV".

我会抗拒的诱惑,解决IV,九的问题,十四,让你可以自己尝试一下;记住这些都是不平凡的,至少比上面我所编写的。试试吧。

I'll resist the temptation to solve the problems of "IV", "IX", "XIV" so that you can try them yourself; remember these are non-trivial, at least compared to what I have written above. Try it out.

所以你看,增量此外运作良好,但减少是一个悬而未决的问题。

So you see, incremental addition works well, but reduction is an unresolved problem.

希望这有助于。

这篇关于转换罗马数字为整数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆