我的Pandigital To Roman Number无效

时间:2017-03-27 07:01:41

标签: c# roman-numerals

我必须制作一个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);

    }
}

这是代码。有什么问题?

2 个答案:

答案 0 :(得分:2)

你正在层层叠加各种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();
    }
}

所以有一个循环。

答案 1 :(得分:1)

一堆 <div class="field"> <%= f.label :branch_address %><br> <%= f.text_field :branch_address ,class: "form-control" ,placeholder: "Enter Branch Address"%> </div> <div class="field"> <%= f.label :employe_id %><br> <%= f.select :employe_id,Employe.all.map{|e| [e.e_name,e.id]} %> </div><br> 看起来很丑陋,他们难以阅读和调试;我建议从代码中提取模型:

if

然后是一个简单的循环:

// Dictionary sorted in descending order -left.CompareTo(... 
private static SortedDictionary<int, string> romans = new SortedDictionary<int, string>(
  Comparer<int>.Create((left, right) => -left.CompareTo(right))) {

  {1000,  "M"}, 
  { 900, "CM"},  
  { 500,  "D"},  
  { 400, "CD"},
  { 100,  "C"},
  {  90, "XC"},
  {  50,  "L"},
  {  40, "XL"},
  {  10,  "X"},
  {   9, "IX"},   
  {   5,  "V"},
  {   4, "IV"},
  {   1,  "I"},
};

试验:

  public static string ToRoman(int number) {
    if (number < 0 || number > 3999) 
      throw new ArgumentOutOfRangeException("number");

    StringBuilder sb = new StringBuilder();

    while (number > 0) {
      var item = romans
        .Where(pair => pair.Key <= number)
        .First();

      number -= item.Key; 
      sb.Append(item.Value);
    }

    return sb.ToString();
  } 

输出

  Console.WriteLine(ToRoman(2999));
  Console.WriteLine(ToRoman(2347));
  Console.WriteLine(ToRoman(432));