如何在字符串中的两个相邻的偶数和奇数之间添加星号和连字符

时间:2017-08-25 14:35:57

标签: c#

我有以下代码来完成此任务但是他们是重构此代码的方法吗?

        //input string
        string numbers = "12467930";
        string result = string.Empty;
        for (int i = 0; i < numbers.Length; i++)
        {

            int value1 = (int)char.GetNumericValue(numbers[i]);
            int value2 = i + 1 < numbers.Length
                    ? (int)char.GetNumericValue(numbers[i + 1])
                    : 0;
            // To check odd numbers
            if (value1 % 2 != 0 && value2 % 2 != 0)
            {
                result += numbers[i] + "-";
            }
            // To check even numbers
            else if (value1 % 2 == 0 && value2 % 2 == 0)
            {
                // skipping 0 if present in the input string
                if (value1 != 0 || value2 != 0)
                    result += numbers[i] + "*";
                else
                    result += numbers[i];
            }
            else
            {
                //for even and odd combination number
                result += numbers[i];
            }
        }

        return result;

2 个答案:

答案 0 :(得分:2)

我要解决的几件事:

1)如果你循环到numbers.Length - 1,你可以避免检查是否有值2.

2)如果使用模运算的结果作为分隔符数组的索引,则可以执行单个数字偶数或偶数检查。

3)在进行一系列字符串连接时,我总是尝试使用StringBulder

string numbers = "12467930";
char[] delimiters = { '-', '*' };
StringBuilder result = new StringBuilder(numbers.Length * 2);

for (int i = 0; i < numbers.Length - 1; ++i)
{
    int value1 = (int)char.GetNumericValue(numbers[i]);
    int value2 = (int)char.GetNumericValue(numbers[i + 1]);
    int mod1 = value1 % 2;
    int mod2 = value2 % 2;

    if (value1 != 0 && mod1 == mod2)
        result.AppendFormat("{0}{1}", value1, delimiters[mod1]);
    else
        result.Append(value1);
}

result.Append(numbers.Last());
return result.ToString();

答案 1 :(得分:0)

重构是一种更改软件系统的过程,它不会改变代码的外部行为,但会改进其内部结构。 (话虽如此,我会改变这个我会怎么做。)

    //input string
    string numbers = "12467930";
    string result = string.Empty;
    for (int i = 0; i < numbers.Length; i++)
    {
        //This should keep it running one more time unnecessarily.
        if(i + 1 < numbers.Length){
             result += numbers[i];
             break;
        }else{
             int value2 = (int)char.GetNumericValue(numbers[i + 1]);
        }
        int value1 = (int)char.GetNumericValue(numbers[i]);
         //check if it's zero
         if(value1 != 0 || value2 !=0){
               //if they are both odd add a hyphen
               if (value1 % 2 != 0 && value2 % 2 != 0){
                     result += numbers[i] + "-";
               //otherwise if they are even and not zero add an asterisk
               }elseif (value1 % 2 == 0 && value2 % 2 == 0){
                     result += numbers[i] + "*";
               //if they are not both odd or even and not zero concatenate the number to the result.
               }else{
                     result += numbers[i];
               }
         //otherwise one number is zero
         }else{
               result += numbers[i];
         }
    }
    return result;

三元组很好但是,它也允许代码最后一次迭代,所以我改变了。我重新安排了if语句。这样它首先检查它是否为0而不需要检查其他条件。这看起来很像你已经拥有的,但它会改善执行。