我想弄清楚如何將羅馬數字轉換爲整數。這是我的代碼的一部分。當我提示用戶輸入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;
}
}
}
}
你的主題爲「轉換整數成羅馬數字」,但你的代碼表明「轉換羅馬數字爲整數。」你究竟在問哪一個? (他們是對立的。)請[編輯]你的問題,並澄清你問的是什麼。 –
一些建議:當編寫這樣的解決方案時,如果弄清楚你將如何解決問題,那麼將更容易,那麼編碼該解決方案。拿出一張紙並嘗試想出一種方法來正確計算某些樣品的羅馬數字(只是使用數字和東西,而不是在紙上寫代碼!)。然後,編碼解決方案。 –
你能解釋一下你「展望未來」的情況嗎?我可能會幫助你解決問題嗎? PS。如果檢查結果不正確,您可以使用開關/大小寫或者更改所有if語句,但是首先改爲「else if」。 –