Java 如何将阿拉伯数字转换成罗马数字?
我需要把阿拉伯语转换成罗马语。我有一个方法编号(int-place),它从某个数字中获取每个数字 示例:5821,其中0处的数字方法=1;数字(2)=8等 我现在需要编写一个方法(使用助手),将这些字符转换为罗马数字。这通常并不困难,但我不能使用数组,这种方法必须适用于三种情况(1、10和100);所以我不能为每个数字写一个案例(否则我可以用许多开关或if来覆盖案例)Java 如何将阿拉伯数字转换成罗马数字?,java,roman-numerals,Java,Roman Numerals,我需要把阿拉伯语转换成罗马语。我有一个方法编号(int-place),它从某个数字中获取每个数字 示例:5821,其中0处的数字方法=1;数字(2)=8等 我现在需要编写一个方法(使用助手),将这些字符转换为罗马数字。这通常并不困难,但我不能使用数组,这种方法必须适用于三种情况(1、10和100);所以我不能为每个数字写一个案例(否则我可以用许多开关或if来覆盖案例) 有人知道吗?因为这是家庭作业,下面的伪代码故意不完整 string toRomanString (int aNumber) {
有人知道吗?因为这是家庭作业,下面的伪代码故意不完整
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();
}
}
}
string toRomanString(整数个)
{
字符串结果=”;
如果(数量<1 | |数量toString().length()>4)
抛出NotImplementedException();
对于(int i=0;i
由于这是家庭作业,下面的伪代码故意不完整
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();
}
}
}
string toRomanString(整数个)
{
字符串结果=”;
如果(数量<1 | |数量toString().length()>4)
抛出NotImplementedException();
对于(int i=0;i
也许你不应该考虑得到一个数字,而应该考虑得到一个值。将您显示的值想象为一个值的总和,每个值本身都可以用罗马数字表示
convert(5000) + convert(800) + convert(20) + convert(1)
也许你不应该考虑得到一个数字,而应该考虑得到一个值。将您显示的值想象为一个值的总和,每个值本身都可以用罗马数字表示
convert(5000) + convert(800) + convert(20) + convert(1)
可能会有所帮助。我认为将数字拆分为数字在这里并不是很有用-只需使用模运算符
%
即可获得所需的内容。是的,使用数组是我的第一个想法,但我不能使用它们;我应该得到每个数字并将其转换。@Paul,我认为只需要减法和比较。可能会有所帮助。我认为将数字拆分为数字在这里并不是很有用-只需使用模运算符%
即可获得所需的内容。是的,使用数组是我的第一个想法,但我不能使用它们;我应该得到每个数字并将其转换。@Paul,我认为只需要减法和比较。