我刚刚开始学习C#,我无法弄清楚为什么这个示例问题以这种方式布局。也许我不理解操作顺序如何工作的基础知识。有人可以解释这个程序的“while”部分是如何工作的吗?我不明白在这行之后没有引用num时“num = num / 10”是什么意思。它会影响上一行吗?感谢您的任何见解。
/*
* C# Program to Get a Number and Display the Sum of the Digits
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Program
{
class Program
{
static void Main(string[] args)
{
int num, sum = 0, r;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
r = num % 10;
num = num / 10;
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : "+sum);
Console.ReadLine();
}
}
}
答案 0 :(得分:3)
它会影响上一行吗?
如果之前你的意思是while condition
,那么是,只是通过循环中的下一次检查来影响检查。
代码只是对所有数字数字进行总结
就像输入
一样1235
然后输出
1+2+3+5 = 11
如果条件为
,则会永远执行while循环num != 0
在循环中,您只需修改num
,以便在下一步条件检查中再次将其与0
进行比较。
如果总是如此,那就是
num != 0
然后你会得到一个无限循环,这在某些情况下是可能的。
欢迎来到编码世界:D
答案 1 :(得分:1)
Example We Input a Number "123"
When it goes to the condition While Number(123) != (is not equal to) 0 Then it will perform the code.
First get the remainder by using modulus %.
Second get the Whole number by using division.
Third Get the sum.
1.) r = num % 10 | The value of r now is 3
2.) num = num / 10 | The value of num now is 12.
3.) sum = sum + r | The value of sum here is 0 + the remainder 3.
It will go the the while statement again. Is the number(12) != (is not equal to) 0 then it will perform the code.
Take note the value now of Num is 12 ok.
Perform again the code.
1.) r = num % 10 | The value of r now is 2
2.) num = num / 10 | The value of num now is 1.
3.) sum = sum + r | The value of sum here now is 3 + the remainder now is 2.
So the sum now is 5. Then it will loop again because Num now is equal to 1. Then
Perform the code again.
I think this explains the best for you.
答案 2 :(得分:0)
我认为你将在该守则中无限循环。使用while循环时,只要num
变量具有值,它就始终在while循环内执行代码。
在您的while循环代码中,您的sum
值与公式无关。
r = num % 10;
num = num / 10;
sum = sum + r;
而是将其更改为sum
设置为num / 10
r = num % 10;
sum = num / 10;
sum = sum + r;
而是使用if
语句。因为每次用户输入数字都保证它只执行一次。所以不需要while循环。
答案 3 :(得分:0)
class Program
{
static void Main(string[] args)
{
int num, sum = 0, r;
Console.WriteLine("Enter a Number : ");
num = int.Parse(Console.ReadLine());
while (num != 0)
{
/* Important: The logic behind is to add the numbers from the last one
* to the first one.
*
* For example, 123 will be added in this way: 3 + 2 + 1
* We will use number 123 as example.*/
/* Comment:
*
* 1st run: 123 % 10, remainder will be 3. Anything modulus 10 will
* always get the final digit. Now, we have r = 3, which will be used
* in the final sum below.
*
* 2nd run: 12 % 10, remainder will be 2. Now, we have r = 2, which
* will be used in the final sum below.
*
* 3rd run: 1 % 10, remainder will be 1. Now, we have r = 1, which will
* be used in the final sum below.
*/
r = num % 10;
/* Comment:
*
* 1st run: 123 / 10, answer will be 12. If you are wondering why it
* isn't 12.3, then it is because the datatype in used is "int", so the
* answer will always be a round number, so 12.3 in round number will
* be 12, which is the 2 numbers that have not been added, so now,
* num = 12, and we have managed to discard 3 because we no longer need
* it because it will be added in the final sum below.
*
* 2nd run: 12 / 10, again answer is not 1.2, but num = 1.
* We have managed to discard 2 because we no longer need it because it
* will be added in the final sum below.
*
* 3rd run: 1 / 10, again answer is not 0.1, but num = 0.
* We have managed to discard 1 because we no longer need it because it
* will be added in the final sum below. This will be the final run.
*/
num = num / 10;
/* Comment:
* 1st run: 0 + 3 (1st run remainder)
* 2nd run: 3 + 2 (2nd run remainder)
* 3rd run: 5 + 1 (3rd run remainder)
*/
sum = sum + r;
}
Console.WriteLine("Sum of Digits of the Number : " + sum);
Console.ReadLine();
}
}
是的,“while”循环使用“num”变量来检查当前条件。如果“num”不是“0”,则逻辑将继续运行。希望这会有所帮助。