【问题标题】:Writing more if else statements than necessary (C#)编写不必要的 if else 语句(C#)
【发布时间】:2017-03-31 14:19:18
【问题描述】:

我刚接触 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();

        }
    }
}

【问题讨论】:

  • 为什么不 ToString("X") 这会告诉你 int 的十六进制
  • @MohitShrivastava 十六进制中没有“G”。
  • @pwas 你是绝对正确的。虽然我还没有看到它,但现在出现的问题是 OP 到处都在写 HEX。 Console.WriteLine("Your number in hexadecimal is: 0x" + output);namespace IntegerToHexadecimal 以及描述中
  • @MohitShrivastava 啊对,所以看来 OP 的代码工作不正常。
  • 我的坏@pwas 新人没有意识到十六进制中没有 G

标签: c# if-statement while-loop


【解决方案1】:

小技巧:

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

无论如何 - 如果您想将 dec 转换为十六进制,您的代码是错误的。十六进制中没有G。 所以remainder == 16 的情况永远不会发生-remainder % 16 的输出属于集合[0...15]

所以你也可以输入:else if (remainder &lt;= 15)

【讨论】:

  • @Joormatt 我已经改进了强制转换 - 之前的答案无法编译,现在它可以肯定 - 经过测试。
【解决方案2】:

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

  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++;
    }

}

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 2012-04-09
    • 2022-08-18
    • 1970-01-01
    相关资源
    最近更新 更多