我的代码是否正确?还有什么可以改进的 [英] Is my code correct? What else can I improve

查看:70
本文介绍了我的代码是否正确?还有什么可以改进的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢像CodeProject这样的论坛充满了不必要的问题,但是因为我是新手并且正在努力学习代码我需要有人来帮助我并检查我的代码。为此,我找不到比CP更大的地方。



但如果有人可以通过我写的问题和代码那将是非常好的并指出我可以改进的事情。我已经详细说明了每行代码中的内容。



我编写了以下c#代码将十进制数转换为二进制数。



使用System; 

namespace Ch6ExQ16
{
class program
{
static void Main(string [] args)
{
Console。写(输入数字:); //要转换为BINARY的数字
int num = int.Parse(Console.ReadLine()); // PARBERING OF NUMBER
int [ ] revArr = new int [8]; //将阵列设置为与字节IE的大小相等8BITS
bool onlyOnceTrue = true; //因此使用了第二个循环(儿童循环)不再进入循环循环//因为如果它会导致阵列结果(REVARR) )以上只能在其所有元素IE中填写一个REM值在原始父母循环的第一次迭代期间计算的数值//并且仅在填写完整的字节(阵列)之后它将允许第一个,循环的父母进行进一步的计算,因为我们的阵列已经完全重复REM $的价值b $ b int index = -1; //这样就可以使孩子在回归阵列的同时继续使用相应的REM值进行循环并且使用相应的REM值进行初始化//这是有用的这句话((revArr [revArr.Length - index - 1] = rem;))

for(int quot = num; quot> 0;)//这是第一个循环或父母LOOP
{
int rem = quot%2;
quot / = 2;
onlyOnceTrue = true; //这将重置正常情况下正常情况下的正常值变为正确的值//这与上面提到的关于禁止第2个循环的解释有关INTO LOOPING CYCLES

for(index ++; onlyOnceTrue == true;)//这是儿童循环或第二循环
{
revArr [revArr.Length - index - 1 ] = rem; //这是因为这个阵列从最后的指数到第一个I.E.被初始化了。逆转顺序//计算的第一个值将在阵列的最后一个索引中//这个记录在减少索引号码的时候达到ZERO'0'
onlyOnceTrue = false; //在此循环之后运行此值将使其无法再次运行,因为它会改变单独的错误,并且执行将返回到第一循环
}
}
for(int i = 0; i< revArr.Length; i ++)//这是一个用于打印ARRAY值的循环公共
{
Console.Write(revArr [i]);
}
Console.WriteLine();
}
}
}





我尝试了什么:



任何回复都将非常感谢..如果有人可以指向我的某个链接或论坛,我可以分享我的未来代码审查将是一个很大的帮助。 :-)



任何提高我的代码可读性的建议也会有所帮助。因为我目前觉得我已经给出了太多难以理解时尚的信息。



提前谢谢..: - )

解决方案

当输入的数字不大于255(0xff)时,这是正确的。否则你的数组大小会超过。



但它太复杂了,你的第二个循环只会执行一次。



整数已经是二进制的。你只需处理每一位。无需使用模运算。您只需使用位掩码来检查是否设置了特定位。



为避免超出数组大小,最好使用输出的动态存储,以便处理任意数量的位。这可以使用 List 来完成。



全部放在一起:

 List< int> intList =  new 列表< int()> ; 
while (num)
{
// 掩盖最低位并将其附加到列表
intList.Add(num& 1 );
// 移出最低位
num>> = < span class =code-digit> 1 ;
}
intList.Reverse();

现在你可以打印列表中的元素了。



其他选项使用 bool 列表,或者 - 只打印结果时 - String StringBuilder 对象。相应地设置大小(32或64)时,您也可以使用数组而不是列表。


如果输入不是数字,该行将抛出异常

 t num = int.Parse(Console.ReadLine()); // 





使用tryParse在使用值

执行任何操作之前验证您是否有数字

I don't like flooding the forums such as CodeProject with unnecessary questions but because I'm new and trying to learn to code I need someone to help me out and check my codes. And for that, I couldn't find any place greater than CP.

But it would be really nice if someone could go through the question and the code i have written and point out things that i could improve about it. I have given details about what each line is doing in the code.

I have written the following c# code to convert a decimal number to binary.

using System;

namespace Ch6ExQ16
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the number: ");// THE NUMBER TO BE ENTERED FOR CONVERTING TO BINARY
            int num = int.Parse(Console.ReadLine());// PARSING OF NUMBER
            int[] revArr = new int[8];// SET THE ARRAY EQUAL TO THE SIZE OF A BYTE I.E. 8BITS
            bool onlyOnceTrue = true;// THIS IS USED SO THAT THE NESTED SECOND FOR LOOP (CHILD LOOP) DO NOT GO INTO LOOPING CYCLES MORE THEN ONCE // BECAUSE IF IT DOES THEN THAT WILL RESULT IN THE ARRAY (REVARR) GIVEN ABOVE TO FILL ONLY ONE VALUE OF REM IN ALL ITS ELEMENTS I.E. THE VALUE CALCULATED DURING 1ST ITERATION OF THE ORIGINAL PARENT LOOP // AND ONLY AFTER FILLING THE COMPLETE BYTE (ARRAY) IT WILL ALLOW THE FIRST, PARENT FOR LOOP TO DO FURTHER CALCULATIONS WHICH THEN WILL BE USELESS AS OUR ARRAY WOULD HAVE BEEN FULL WITH REPEATED VALUES OF REM
            int index = -1;// THIS IS USED SO THAT THE VALUE IN THE CHILD FOR LOOP WHILE REVERSING THE ARRAY AND INITIALISING IT WITH CORRECT REM VALUE REMAINS SYNCRONISED WITH THE CORRESPONDING INDEX // IT IS USEFUL FOR THIS SENTENCE ((revArr[revArr.Length - index - 1] = rem;))

            for (int quot = num; quot > 0;)// THIS IS THE 1ST LOOP OR THE PARENT LOOP
            {
                int rem = quot % 2;
                quot /= 2;
                onlyOnceTrue = true;// THIS RESETS THE VALUE OF ONLYONCETRUE BACK TO TRUE WHICH WAS PREVIOUSLY CHANGED FROM TRUE TO FALSE IN THEE 2ND LOOP GIVEN BELOW // THIS RELATES TO THE EXPLANATION GIVEN ABOVE ABOUT DISABLING THE 2ND FOR LOOP TO GO INTO LOOPING CYCLES

                for (index++; onlyOnceTrue == true;)// THIS IS THE CHILD LOOP OR THE 2ND LOOP
                {
                    revArr[revArr.Length - index - 1] = rem; // THIS IS USED SO THAT THE ARRAY IS INITIALISED FROM THE LAST INDEX TO THE FIRST I.E. IN REVERSE ORDER// THE FIRST REM VALUE CALCULATED WILL BE PUT IN THE LAST INDEX OF THE ARRAY // THIS KEEPS ON DECREASING THE INDEX NUMBER TILL IT REACHES ZERO '0'
                    onlyOnceTrue = false; // AFTER THIS LOOP RUNS ONCE THIS VALUE WILL KEEP IT FROM RUNNING AGAIN AS IT CHANGES THE ONLYONCETRUE TO FALSE AND THUS THE EXECUTION GOES BACK TO THE FIRST LOOP
                }
            }
            for (int i = 0; i < revArr.Length; i++) // THIS IS A COMMON FOR LOOP THAT IS USED TO PRINT THE VALUES OF ARRAY
            {
                Console.Write(revArr[i]);
            }
            Console.WriteLine();
        }
    }
}



What I have tried:

Any reply will be greatly appreciated.. And if someone could point me to some link or a forum where I can share my future code for review will be a great help. :-)

And any suggestion for improving the readability of my codes will be also helpful. As i currently feel that i have given too much info that too in hard to understand fashion.

Thanks in advance.. :-)

解决方案

It would be correct when the entered number is not greater than 255 (0xff). Otherwise you array size would be exceeded.

But it is much too complicated and your second loop will be executed always only one time.

Integers are already binary. You just have to process each bit. There is no need to use a modulo operation. You can simply use a bit mask to check if a specific bit is set.

To avoid exceeding the array size it would be better to use a dynamic storage of the output so that you can process any number of bits. This can be done using a List.

Putting it all together:

List<int> intList = new List<int()>;
while (num)
{
    // Mask out the lowest bit and append that to the list
    intList.Add(num & 1);
    // Shift out the lowest bit
    num >>= 1;
}
intList.Reverse();

Now you can print the elements of the list.

Other options are using a list of bool, or - when the result should only be printed - a String or StringBuilder object. You may also use an array instead of a list when setting the size accordingly (32 or 64).


This line will throw an exception if the input is not a number

t num = int.Parse(Console.ReadLine());// 



use tryParse to validate you have a number before doing anything with the value


这篇关于我的代码是否正确?还有什么可以改进的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆