编写不必要的if else语句(c#)

本文关键字:语句 else if 不必要 | 更新日期: 2023-09-27 18:18:24

我刚接触c#语言几天,想挑战一下自己,写一个将普通整数转换为十六进制的程序。我认为这个程序可以正常工作,但我想摆脱第二组if/else语句,是否有可能重写while循环中的代码?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IntegerToHexadecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter an integer");
            int input = Convert.ToInt32(Console.ReadLine());
            string output = "";
            int answer = input / 16;
            int remainder = input % 16;
            while (answer != 0)
            {
                if (remainder < 10) output = Convert.ToString(remainder) + output;
                else if (remainder == 10) output = "A" + output;
                else if (remainder == 11) output = "B" + output;
                else if (remainder == 12) output = "C" + output;
                else if (remainder == 13) output = "D" + output;
                else if (remainder == 14) output = "E" + output;
                else if (remainder == 15) output = "F" + output;
                input = answer;
                answer = input / 16;
                remainder = input % 16;
            }
            if (remainder < 10) output = Convert.ToString(remainder) + output;
            else if (remainder == 10) output = "A" + output;
            else if (remainder == 11) output = "B" + output;
            else if (remainder == 12) output = "C" + output;
            else if (remainder == 13) output = "D" + output;
            else if (remainder == 14) output = "E" + output;
            else if (remainder == 15) output = "F" + output;

            Console.WriteLine("Your number in hexadecimal is: 0x" + output);
            Console.ReadLine();
        }
    }
}

编写不必要的if else语句(c#)

小技巧:

if (remainder < 10) output = Convert.ToString(remainder) + output;
else if (remainder <= 16) 
{
    int result = 'A' + (remainder - 10);
    output = (char)result + output;
}

无论如何-如果你想转换成十六进制你的代码是错误的。十六进制中没有G。因此,remainder == 16的情况不会发生——remainder % 16的输出属于[0...15]集合。

所以你也可以输入:else if (remainder <= 15)

似乎最后一个if else条件是重复的,您想要摆脱它。所以情况是answer变为0,我们仍然想再运行一次循环。可能有很多选择来处理这个问题,但我想告诉你其中的三个

  1. 你可以为if else创建一个函数,这样你只需要调用这个函数,你的代码就会看起来简单、整洁。但是,是的,这不是一个解决方案。这只会增加可读性。
  2. 你可以使用do while循环而不是while,因为do while稍后会检查条件,但会先运行循环。
  3. 你可以写一个布尔变量来检查循环是否应该运行,在循环中我们可以有一个条件来检查循环是否执行了一次。下面是代码供参考。

这可能解决了if else条件在循环后重复代码的问题。试试吧。

int answer = input / 16;
int remainder = input % 16;
bool shudloop = true;
int cntshudloop = 0;
while(shudloop)
{
    if(remainder < 10)
        output = Convert.ToString(remainder) + output;
    else if(remainder == 10)
        output = "A" + output;
    else if(remainder == 11)
        output = "B" + output;
    else if(remainder == 12)
        output = "C" + output;
    else if(remainder == 13)
        output = "D" + output;
    else if(remainder == 14)
        output = "E" + output;
    else if(remainder == 15)
        output = "F" + output;
    input = answer;
    answer = input / 16;
    remainder = input % 16;
    if(answer == 0)
    {
        if(cntshudloop >= 1)
            shudloop = false;
        cntshudloop++;
    }
}