我想弄清楚如何将罗马数字转换为整数。这是我的代码的一部分。当我提示用户输入 M 时,它显示 1000,但是当我提示用户输入一个罗马数字(如 VM)时,它没有给我 995 而是 1005。这是因为我告诉我的程序这样做。
我想弄清楚的是如何向前看并让它知道它何时添加或减去罗马数字。
我该如何开始做这件事?
class Roman
{
public int inprogress = 0;
public Roman(string roman)
{
char temp = 'Z';
int length;
length = roman.Length;
for (int i = 0; i < length; i++)
{
temp = roman[i];
if (temp == 'M')
{
inprogress = inprogress + 1000;
}
if (temp == 'D')
{
inprogress = inprogress + 500;
}
if (temp == 'C')
{
inprogress = inprogress + 100;
}
if (temp == 'L')
{
inprogress = inprogress + 50;
}
if (temp == 'X')
{
inprogress = inprogress + 10;
}
if (temp == 'V')
{
inprogress = inprogress + 5;
}
if (temp == 'I')
{
inprogress = inprogress + 1;
}
}
}
}