我必须编写一个 C# 程序,在该程序中,我根据输入的数字生成罗马数字。出于某种原因,我无法在输入时获取代码来给我一个值。
namespace RomaiSzam
class ToRomanNumber
{
string s = "";
public string ToRoman(int number)
{
if ((number < 0) || (number > 3999))
{
s = s + "Invalid input";
}
if (number < 1) return s;
if (number >= 1000) { s = s + "M"; ToRoman(number - 1000); }
if (number >= 900) { s = s + "CM"; ToRoman(number - 900); }
if (number >= 500) { s = s + "D"; ToRoman(number - 500); }
if (number >= 400) { s = s + "CD"; ToRoman(number - 400); }
if (number >= 100) { s = s + "C"; ToRoman(number - 100); }
if (number >= 90) { s = s + "XC"; ToRoman(number - 90); }
if (number >= 50) { s = s + "L"; ToRoman(number - 50); }
if (number >= 40) { s = s + "XL"; ToRoman(number - 40); }
if (number >= 10) { s = s + "X"; ToRoman(number - 10); }
if (number >= 9) { s = s + "IX"; ToRoman(number - 9); }
if (number >= 5) { s = s + "V"; ToRoman(number - 5); }
if (number >= 4) { s = s + "IV"; ToRoman(number - 4); }
if (number >= 1) { s = s + "I"; ToRoman(number - 1); }
return s;
}
}
命名空间 RomaiSzam
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Write the number");
int num = int.Parse(Console.ReadLine());
ToRomanNumber tr = new ToRomanNumber();
string opt = tr.ToRoman(num);
Console.WriteLine(opt);
}
}
这是代码。有什么问题?
您正在遍历所有各种if
...您不应该...
这可能是使其正常工作所需的最小逻辑更改。
class ToRomanNumber
{
string s = "";
public string ToRoman(int number)
{
if ((number < 0) || (number > 3999))
{
s = s + "Invalid input";
}
if (number < 1) return s;
if (number >= 1000) { s = s + "M"; ToRoman(number - 1000); return s; }
if (number >= 900) { s = s + "CM"; ToRoman(number - 900); return s; }
if (number >= 500) { s = s + "D"; ToRoman(number - 500); return s; }
if (number >= 400) { s = s + "CD"; ToRoman(number - 400); return s; }
if (number >= 100) { s = s + "C"; ToRoman(number - 100); return s; }
if (number >= 90) { s = s + "XC"; ToRoman(number - 90); return s; }
if (number >= 50) { s = s + "L"; ToRoman(number - 50); return s; }
if (number >= 40) { s = s + "XL"; ToRoman(number - 40); return s; }
if (number >= 10) { s = s + "X"; ToRoman(number - 10); return s; }
if (number >= 9) { s = s + "IX"; ToRoman(number - 9); return s; }
if (number >= 5) { s = s + "V"; ToRoman(number - 5); return s; }
if (number >= 4) { s = s + "IV"; ToRoman(number - 4); return s; }
if (number >= 1) { s = s + "I"; ToRoman(number - 1); return s; }
return s;
}
}
请注意,您的写法非常难看。像这样使用实例变量 s
让我起鸡皮疙瘩。
至少你应该这样做static
,像这样:
class ToRomanNumber
{
public static string ToRoman(int number)
{
if ((number < 0) || (number > 3999))
{
return "Invalid input";
}
if (number >= 1000) { return "M" + ToRoman(number - 1000); }
if (number >= 900) { return "CM" + ToRoman(number - 900); }
if (number >= 500) { return "D" + ToRoman(number - 500); }
if (number >= 400) { return "CD" + ToRoman(number - 400); }
if (number >= 100) { return "C" + ToRoman(number - 100); }
if (number >= 90) { return "XC" + ToRoman(number - 90); }
if (number >= 50) { return "L" + ToRoman(number - 50); }
if (number >= 40) { return "XL" + ToRoman(number - 40); }
if (number >= 10) { return "X" + ToRoman(number - 10); }
if (number >= 9) { return "IX" + ToRoman(number - 9); }
if (number >= 5) { return "V" + ToRoman(number - 5); }
if (number >= 4) { return "IV" + ToRoman(number - 4); }
if (number >= 1) { return "I" + ToRoman(number - 1); }
return string.Empty;
}
}
像这样使用它
string str = ToRomanNumber.ToRoman(5);
而且我认为它仍然有点难看,因为它是递归的...为此使用递归是.... bleah :-)
我通常会这样写:
class ToRomanNumber
{
public static string ToRoman(int number)
{
if ((number < 0) || (number > 3999))
{
return "Invalid input";
}
var sb = new StringBuilder();
while (number > 0)
{
if (number >= 1000) { sb.Append("M"); number -= 1000; }
else if (number >= 900) { sb.Append("CM"); number -= 900; }
else if (number >= 500) { sb.Append("D"); number -= 500; }
else if (number >= 400) { sb.Append("CD"); number -= 400; }
else if (number >= 100) { sb.Append("C"); number -= 100; }
else if (number >= 90) { sb.Append("XC"); number -= 90; }
else if (number >= 50) { sb.Append("L"); number -= 50; }
else if (number >= 40) { sb.Append("XL"); number -= 40; }
else if (number >= 10) { sb.Append("X"); number -= 10; }
else if (number >= 9) { sb.Append("IX"); number -= 9; }
else if (number >= 5) { sb.Append("V"); number -= 5; }
else if (number >= 4) { sb.Append("IV"); number -= 4; }
else if (number >= 1) { sb.Append("I"); number -= 1; }
}
return sb.ToString();
}
}
所以有一个循环。
我是一名优秀的程序员,十分优秀!