仅显示输出一次 [英] Display output only once

查看:60
本文介绍了仅显示输出一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了我的程序,这是计算程序

但输出如下:



数字总和1259955是36

36的除数是1

36的除数是2

36的除数是3

36的除数是4

36的除数是6

36的除数是9

36的除数是12
36的除数是18

36的除数是36



但我需要它是这样的:



36的除数是:1,2,3,4,6,9,12,18,36


我尝试过:



I done my program which is calculation program
But the output is as follows :

Sum of the digits 1259955 is 36
The divisors of 36 are 1
The divisors of 36 are 2
The divisors of 36 are 3
The divisors of 36 are 4
The divisors of 36 are 6
The divisors of 36 are 9
The divisors of 36 are 12
The divisors of 36 are 18
The divisors of 36 are 36

but i need it to be like this :

The divisors of 36 are: 1,2,3,4,6,9,12,18,36

What I have tried:

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

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

推荐答案

分析你想要的结果

Analyze the result you want
The divisors of 36 are: 1,2,3,4,6,9,12,18,36
-------------------------
                         -------------------
printed 1 time           made of many little chuncks



与您的代码比较,在调试器上运行代码以观察结果是否已构建。



您的代码不表现你期望的方式,或者你不明白为什么!



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

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

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

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



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


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

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



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



调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Compare with your code, run the code on debugger to watch hiw result is built.

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.


你应该在集合中收集除数一个整数列表。

然后你可以使用字符串类 join 方法。

另一个好主意可能是使用字符串插值而不是连接你的方式。

此外,为了理解下面的例子,建议谷歌使用标记为粗体的术语。

You should collect the divisors in a collection e.g. a list of integers.
Then you could use the join method of the string class.
Another good idea might be the usage of string interpolation rather than concatenating the way you do.
Moreover it is recommended to google for the terms marked bold in order to understand the example below.
var divisors = new List<int>();

for (int i = 1; i <= sum; i++)
{
   if (sum % i == 0)
   {
        //Console.WriteLine("The divisors of " + sum + " are as follows " + i);
        divisors.Add(i);
   }
}

Console.WriteLine(


{sum}的除数如下{string.Join(, ,除数)});
"The divisors of {sum} are as follows {string.Join(", ", divisors)}");


这篇关于仅显示输出一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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