我正在构建一个程序来计算输入数字的总和和除数,但我得到了一些错误 [英] I am building a program to calculate sum and divisors of a number entered but I am getting some bugs

查看:59
本文介绍了我正在构建一个程序来计算输入数字的总和和除数,但我得到了一些错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在构建一个计算程序,程序应该向用户询问最多7位数的数字然后计算数字的总和例如:number = 1259955,sum = 36这样的程序应该列出所有除数以上总和的例子:

So i am building a calculation program the program should ask the user a number with at most 7 digits and then calculate the sum of the numbers For example: number = 1259955 , sum= 36 like this then the program should list all divisors of the sum above Example:

sum=8 , divisors = 1,2,3,4,6,9,12,18,36

喜欢这个



我写的代码是执行总和部分而不是除数部分



此外还有行Console.Writeline( + number +的数字之和为+ sum正在执行输出为:数字0的总和为36为什么说0



我尝试过:



Like this

My code i wrote is executing the sum part but not the divisor part

Also when the line Console.Writeline("Sum of the digits of" + number + "is"+ sum) is executing The output is : Sum of the digits 0 is 36 Why is it saying 0

What I have tried:

int number;
        int sum = 0;
        Console.WriteLine("Enter a number with at most 7 digits: ");
        number = Convert.ToInt32(Console.ReadLine());

        while (number != 0)
        {
            sum += number % 10;
            number /= 10;
        }

        Console.WriteLine("Sum of the digits of " + number + " is " + sum);



        for (int i = 1; i <= number; i++)
        {
            if (number % i ==0)
            {

                Console.WriteLine("The divisors of" + sum + " are " + i.ToString());
            }


        }

        Console.ReadLine();
    }
}

推荐答案

你好,

希望你做得好。



您的算法中有一点问题是您使用'数字'作为整数来从用户获取数据并在while循环中使用相同的变量它值0,



这就是为什么它说

Hello,
Hope you are doing well.

there is a bit problem in your algorithm you are using a 'number' as an integer to get data from a user and using the same variable inside the while loop makes it value 0,

that's why it says
Sum of the digits 0 is 36





这就是因为数字= 0而没有执行for循环的原因。






解决方案很简单



取另一个变量保存原始值,就是这样。





and this is the reason the for loop is not executing because of the number=0.



Solution is simple

take another variable save the original value in it and that's it.

int number,val;
            int sum = 0;
            Console.WriteLine("Enter a number with at most 7 digits: ");
            number = Convert.ToInt32(Console.ReadLine());
            val = number;
            while (number != 0)
            {
                sum += number % 10;
                number /= 10;
            }

            Console.WriteLine("Sum of the digits of " + val + " is " + sum);



            for (int i = 1; i <= val; i++)
            {
                if (val % i == 0)
                {

                    Console.WriteLine("The divisors of " + sum + " are " + i.ToString());
                }


            }

            Console.ReadLine();


你的代码没有按照你期望的方式运行,或者你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于我正在构建一个程序来计算输入数字的总和和除数,但我得到了一些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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