Help with Java Programming

07/10/2013 08:29 ilovephonoodles#1
this is what I need to do :

One of the principal stumbling blocks in the early development of mathematics was the notation used for numbers before the “Arabic” or “decimal” number system was introduced, since some of the older number systems were quite unsuited for arithmetic. As an example, the Roman system of numbers makes use of the symbols M (with value 1000), D (value 500), C (value 100), L (value 50), X (value 10), V (value 5), and I (value 1).

In the “old Roman” or “Classical” system a number is a sequence of M’s, D’s, C’s, L’s, X’s, V’s, and I’s in that order, the value of a number just being the sum of the values of the individual symbols.

Write a program that will convert a classical Roman numeral to a decimal number.

kinda lost help please
07/10/2013 13:46 Conrew#2
Im sorry but this is the wrong section.
07/10/2013 17:27 xxfabbelxx#3
moved
07/11/2013 17:55 hallo6#4
Easy, but maybe not the best solution:
I = userinput;
if(I>=5)
{
V = Math.round(I/5);
I=I%5;
}
else if(V>=2)
{
X=Math.round(V/2);
V=V%2;
}
didn't try it, just wrote it here quickly.
Sure, there are better solutions.
07/19/2013 10:53 Aegir112#5
int[] roman = {...};
int dec = 0;
for(int i = 0; i < input.length; i++)
{
dec+=roman[substring(input, i, i+1)];
}

Create an array => roman["V"] = 5; ...
07/28/2013 21:49 lolor2#6
Quote:
Originally Posted by Aegir112 View Post
int[] roman = {...};
int dec = 0;
for(int i = 0; i < input.length; i++)
{
dec+=roman[substring(input, i, i+1)];
}

Create an array => roman["V"] = 5; ...
Not completly right if an lower roman char is infront of an higher one u would need subtract
07/29/2013 17:45 RGrand#7
Code:
public static int convert(string nr)
{
        if (nr == string.Empty) return 0;
        if (nr.StartsWith("M")) return 1000 + convert(nr.Remove(0, 1));
        if (nr.StartsWith("CM")) return 900 + convert(nr.Remove(0, 2));
        if (nr.StartsWith("D")) return 500 + convert(nr.Remove(0, 1));
        if (nr.StartsWith("CD")) return 400 + convert(nr.Remove(0, 2));
        if (nr.StartsWith("C")) return 100 + convert(nr.Remove(0, 1));
        if (nr.StartsWith("XC")) return 90 + convert(nr.Remove(0, 2));
        if (nr.StartsWith("L")) return 50 + convert(nr.Remove(0, 1));
        if (nr.StartsWith("XL")) return 40 + convert(nr.Remove(0, 2));
        if (nr.StartsWith("X")) return 10 + convert(nr.Remove(0, 1));
        if (nr.StartsWith("IX")) return 9 + convert(nr.Remove(0, 2));
        if (nr.StartsWith("V")) return 5 + convert(nr.Remove(0, 1));
        if (nr.StartsWith("IV")) return 4 + convert(nr.Remove(0, 2));
        if (nr.StartsWith("I")) return 1 + convert(nr.Remove(0, 1));
        throw new ArgumentOutOfRangeException("bad input or bad code");
}
~RGrand