需要帮助编写一个程序来计算给定范围内的整数之和,并且也可以被给定的整数整除? [英] Need help in writing a program that calculates the sum of integers in a given range and are also divisible by the given integer?

查看:302
本文介绍了需要帮助编写一个程序来计算给定范围内的整数之和,并且也可以被给定的整数整除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个程序来计算给定范围内所有整数的总和,并且也可以被给定的整数整除。用户提供开始,结束和除数整数。



我为此创建了两个类 - 接受来自用户和summation类的输入的程序类做所有的计算。



我无法弄清楚如何在求和类中编写方法的代码!任何帮助将不胜感激!



谢谢! :)



编辑1 =



感谢您的帮助!这不是功课。这就像我正在为下一学期的课程做的练习项目。



我知道如何设置预定义值的循环,但是我无法弄清楚如何使用用户可接受的值来循环。



这是我到目前为止所拥有的:



程序类:

求和

sum = new 求和();

Console.WriteLine( 请输入整数范围的下限。);
sum.LowerLimit = Convert.ToInt16(Console.ReadLine());

Console.WriteLine( 请输入整数范围的上限。);
sum.UpperLimit = Convert.ToInt16(Console.ReadLine());

Console.WriteLine( 请输入除数(要均匀分配的数字)这个范围内的值)。);
sum.Divisor = Convert.ToInt16(Console.ReadLine());

sum.Summation();

Console.WriteLine( 分成您选择的除数的数字范围是:{ 0}。,sum.Sum);





总和类:



  class  

求和
{
public int lowerLimit,upperLimit,divisor,sum;
public int LowerLimit
{
get { return lowerLimit; }
set {lowerLimit = value ; }
}

public int UpperLimit
{
get { return upperLimit; }
set {upperLimit = value ; }
}

public int 除数
{
get { return divisor; }
set {divisor = value ; }
}
public int Sum
{
get { return sum; }
set {sum = value ; }
}

private float restart;

public float 重新启动
{
get { return restart; }
set {restart = value ; }
}

int Sum = 0 ;
for int i = LowerLimit; i < = UpperLimit; i ++)
{
Sum = Sum + i;
}





编辑2:



我试过推杆这段代码(附在下面)但它不起作用。 :/我得到了几个错误。我只是无法弄清楚这段代码。尝试了格里夫所建议的一切。还有什么能帮到我吗?我将永远为任何帮助而感到高兴!

 int Sum = 0; 

for(int i = lowerLimit; i< = upperLimit; i ++)
{
if(i%divisor == 0)
Sum = Sum + i ;
}
返回Sum;

< pre>

解决方案

这很有味道,所以没有代码!



虽然这很简单:



1)设置总数,等于零。

2)设置一个循环,从用户低值开始

3)如果以除数为模的循环值为零,则将循环值添加到总数

4)增加循环值。如果它小于或等于用户的高值,则返回到3

5)总计就是答案。


那么你大部分都在那里它看起来像,但看起来你在格里夫的回答中错过了第3步。您需要的是模数运算符 [ ^ ]。使用这个你可以得到两个数的除法的余数,如果那个结果为0那么它可以均匀地划分(因为,我确定你知道,如果它均匀地划分,那么就没有余数)。

  int  Sum =  0 ; 
for int i = UserInput1; i < = UserInput2; i ++)
{
if (i%divisor == 0
Sum = Sum + i;
}
返回总和;





- -SJ


I have to write a program that calculates the sum of all the integers in a given range and is also evenly divisible by a given integer. The user supplies the beginning, ending and the divisor integer.

I created two classes for this- the program class that accepts the inputs from the user and the summation class that does all the calculations.

I can't figure out how to write the code for the method in the summation class! any help would be appreciated!

Thanks! :)

Edit 1=

Thanks for the help guys! It's not homework. It's like a practice project that I'm doing for a class that I'll take next semester.

I know how to set up the loop for predefined values, but I can't figure out how to work the loop for user acceptable values.

Here's what I have so far:

The program class:

Summation

sum = new Summation();

Console.WriteLine("Please enter the lower limit of the integer range.");
sum.LowerLimit = Convert.ToInt16(Console.ReadLine());

Console.WriteLine("Please enter the upper limit of the integer range.");
sum.UpperLimit = Convert.ToInt16(Console.ReadLine());

Console.WriteLine("Please enter the divisor(the number that is to divide evenly into the values in this range).");
sum.Divisor = Convert.ToInt16(Console.ReadLine());

sum.Summation();

Console.WriteLine("The range of numbers that divide into your chose divisor is:{0}.", sum.Sum);



The summation class:

class

Summation
{
public int lowerLimit, upperLimit, divisor, sum;
public int LowerLimit
{
get { return lowerLimit; }
set { lowerLimit = value; }
}

public int UpperLimit
{
get { return upperLimit; }
set { upperLimit = value; }
}

public int Divisor
{
get { return divisor; }
set { divisor = value; }
}
public int Sum
{
get { return sum; }
set { sum = value; }
}

private float restart;

public float Restart
{
get { return restart; }
set { restart = value; }
}

int Sum = 0;        
        for (int i = LowerLimit; i <= UpperLimit; i++)
        {
            Sum = Sum + i;
        }



EDIT 2 :

I tried putting this code in (attached below) but it doesn't work. :/ I get several errors. I just can't figure out this piece of code. Tried everything Griff suggested too. Anything else that can help me? I will forever be greatful for any help!

  int Sum = 0;        
    
    for (int i = lowerLimit; i <= upperLimit; i++)
{
    if(i % divisor == 0)
    Sum = Sum + i;
}
    return Sum;

<pre>

解决方案

This smells heavily of homework, so no code!

It's pretty simple though:

1) Set up a total, equal to zero.
2) Set up a loop, starting at the user low value
3) If the loop value modulo the divisor is zero, add the loop value to the total
4) Increment the loop value. If it is less than or equal to the user high value, go back round to 3
5) Total is the answer.


Well you're mostly there it looks like, but it looks like you're missing step 3 in Griff's answer. What you need for that is the Modulo Operator[^]. Using this you can get the remainder of the division of two numbers, and if that result is 0 then it can divide evenly (because, as I'm sure you know, if it divides evenly then there is no remainder).


int Sum = 0;        
for (int i = UserInput1; i <= UserInput2; i++)
{
if(i%divisor == 0)
    Sum = Sum + i;
}
return Sum;



--SJ


这篇关于需要帮助编写一个程序来计算给定范围内的整数之和,并且也可以被给定的整数整除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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