C#循环 - 按用户输入计算范围之间所有整数的总和! ! [英] C# loop - calculating sum of all integers between a range by user input! !

查看:199
本文介绍了C#循环 - 按用户输入计算范围之间所有整数的总和! !的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个基于控制台的应用程序

(使用循环)



- 提示用户输入两个值

-lowerBoundary和upperBoundary

-显示来自lowerBoundary和upperBoundary(包括)的所有整数的总和

例如:如果用户输入2和6,您将显示总和2 + 3 + 4 + 5 + 6



请帮助!!我真的不知道该怎么做!



我尝试过的事情:



Write a console-based application that
(using loops)

-prompts the user for two values
-lowerBoundary and upperBoundary
-displays the sum of all integers from lowerBoundary and upperBoundary (inclusive)
Ex: if the user enters 2 and 6, you will display the sum of 2+3+4+5+6

Please help!! I really have no idea what to do!

What I have tried:

int start, end;

Console.Write("Enter lower integer >> ");
start = int.Parse(Console.ReadLine());
Console.Write("Enter higher integer >> ");
end = int.Parse(Console.ReadLine());

int sum = 0;
for (int x = start; x <= end; x++)
{
    sum += x;
    Console.WriteLine("The sum of the integers between these two numbers is {0} ", sum);
}





}



}

推荐答案

在循环外移动Console.Writeline:

Move the Console.Writeline outside the loop:
for (int x = start; x <= end; x++)
    {
    sum += x;
    }
Console.WriteLine("The sum of the integers between these two numbers is {0} ", sum);





如果用户输入2和6,如何显示你将显示总和2 + 3 + 4 + 5 + 6 = N

这是一个不同的问题,它有点复杂。

但是......这是你的作业,所以你需要自己做!

我会告诉你怎么做,但是由你来编写代码,好吗?

请记住,这不是我这样做的方式 - 但是我用来做它的类不是你可能已经涵盖的任何东西:StringBuilder或List< T> - 无论如何我都不会用循环来完成这项任务! :笑:

但是......

在循环外声明一个字符串,称之为 separator 并设置使用Console.Write输出总和
在循环中,使用Console.Write写入<$ c $的值c> separator 后跟x的值。 (您可以使用格式化字符串在一行中执行此操作)

分隔符的值设置为+

循环之后,使用Console.Writeline写入=后跟值 sum



尝试那你看到了多远!



"How do I display "if the user enters 2 and 6, you will display the "sum of 2+3+4+5+6=N"""
That's a different question, and it's a little more complex.
But...this is your homework, so you need to do it yourself!
I'll tell you how, but it's up to you to write the code, OK?
Bear in mind that this isn't the way I'd do it - but the classes I'd use to do it aren't anything you are likely to have covered yet: StringBuilder or List<T> - and I wouldn't use a loop to do this task anyway! :laugh:
But...
Declare a string outside the loop, call it "separator" and set to to an empty string.
Use Console.Write to output "The sum of "
Inside the loop, use Console.Write to write the value of separator followed by the value of x. (You can do this in one line with a formatting string)
Set the value of separator to "+"
After the loop, use Console.Writeline to write "=" followed by the value of sum

Try that and see how far you get!


这不是一个问题。我能看到的主要问题是:您必须使用 Console.WriteLine 我们的循环移动该行。您还可以检查结束是否大于或等于 start



但问题不需要循环。要以合理的方式解决它,请注意第一个和最后一个成员具有与第二个和第二个成员相同的总和,依此类推。所以,如果你有偶数个成员N,解决方案将是第一个和最后一个的一些乘以这样的对的数量:(开始+结束)* N / 2; 对于奇数个成员,中间成员的值将被添加到此总和中。请自行详细说明。



-SA
This is not a question. The major problem I can see is: you have to move the line with Console.WriteLine our of the cycle. You also can check up that end is greater or equal to start.

But the problem does not require loop. To solve it in a reasonable way, notice that first and last member has the same sum as second and second last, and so on. So, if you have even number of the members N, the solution will be the some of first and last multiplied by number of such pairs: (start + end) * N / 2; for odd number of members, a value of the middle member will be added to this sum. Please elaborate the rest by yourself.

—SA


使用系统;



名称空间SumOfnton

{

班级计划

{

static void Main(string [] args)

{

int start = 0,end = 0,sum = 0;



Console.WriteLine(输入第一个整​​数);

start = int.Parse(Console.ReadLine());

Console.WriteLine( 输入第二个整数);

end = int.Parse(Console.ReadLine());



if(start<结束)

{

for(int i = start; i< = end; i ++)

{

sum = sum + i;

}

Console.WriteLine({0}和{1}之间的数字之和为{2},开始,结束,总和);

}

其他

{

Console.WriteLine(第二个数字必须大于第一个数字);

}



Console.ReadKey();

}

}

}
using System;

namespace SumOfnton
{
class Program
{
static void Main(string[] args)
{
int start=0, end=0, sum=0;

Console.WriteLine("Enter The First Integer");
start = int.Parse(Console.ReadLine());
Console.WriteLine("Enter The Second Integer");
end = int.Parse(Console.ReadLine());

if (start < end)
{
for (int i = start; i <= end; i++)
{
sum = sum + i;
}
Console.WriteLine("Sum Of The Numbers Between The {0} and {1} is {2} ", start, end, sum);
}
else
{
Console.WriteLine("Second Number Must Be Bigger Than The First Number");
}

Console.ReadKey();
}
}
}


这篇关于C#循环 - 按用户输入计算范围之间所有整数的总和! !的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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