How do I approach my homework assignment to convert arabic numerals to roman numerals?
I need to convert Arabic to Roman. I have a method number(int place), that gets each dig开发者_如何学Cit from a certain number.
Example: 5821, where the number method at place 0 = 1; number(2) = 8, etc.
I now need to write a method (with a helper), that converts these characters into roman numerals. This wouldn't normally be difficult, but I can't use arrays, and this method has to work for the three cases (1's, 10's, and 100's); so I can't write a case for each numeral (otherwise I could many switch or if's to cover the cases).
Ideas anyone?
Since this is homework, the pseudo code below is deliberately left incomplete.
string toRomanString (int aNumber)
{
string result = "";
if (aNumber < 1 || aNumber.toString().length() > 4)
throw NotImplementedException();
for(int i=0; i < aNumber.toString().length(); i++)
{
if(i = 0)
{
throw NotImplementedException();
}
elseif(i = 1)
{
throw NotImplementedException();
}
elseif(i = 2)
{
throw NotImplementedException();
}
else
{
throw NotImplementedException();
}
}
}
Maybe instead of thinking about getting a digit, you should think about getting a value. Think of the value you're displaying as a sum of values, each of which can be itself simply expressed in Roman numerals.
convert(5000) + convert(800) + convert(20) + convert(1)
精彩评论