WAP执行添加 [英] WAP to perform the addition

查看:83
本文介绍了WAP执行添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写程序以在给定范围内添加所有奇数。总和应显示为   0  如果第一个整数 大于第二个整数。







测试用例: 1  
样本输入:

2
10

预期输出:
24


测试用例: 2
样本输出:

8
2

预期输出:
0







< pre lang =C#> int number;
int min,max;
int sum = 0 ;

Console.WriteLine( 输入最小数字);
min = Convert.ToInt32(Console.ReadLine());

Console.WriteLine( 输入最大数字);

max = Convert.ToInt32(Console.ReadLine());


for (number = min; number < = max; number ++)
{
if (number% 2 != 0
sum = sum + number;

}
Console.WriteLine(sum);







我能够做第一个样本输入。但是我无法实现第二个想要的输出?





请帮助。

提前致谢。

解决方案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!


你的代码只需稍加改动。

  int  number; 
int min,max;
int sum = 0 ;

Console.WriteLine( 输入最小数字);
min = Convert.ToInt32(Console.ReadLine());

Console.WriteLine( 输入最大数字);

max = Convert.ToInt32(Console.ReadLine());

if (max > min)
{

for (number = min; number < = max; number ++)
{
if (number% 2 != 0
sum = sum + number;
}
}

else
{
sum = 0 ;
}
Console.WriteLine(sum);



else 就足够了 WriteLine 可以移到最后以避免重复。

很高兴看到你能够自己解决问题。


使用下面的代码c#



随机rnd =  new  Random(); 
Console.WriteLine( \ n20从1到10的随机整数:);

int total = 0 ;
for int X = 1 ; X < = 20 ; X ++)
{
int y = rnd.Next( 1 10 );
if (y% 2 == 1
{
// 添加所有奇数#并显示总和。
总计+ =总+ y;
}
else
{
}
Console.WriteLine( {0},y);
}
Console.ReadLine();


write a program to perform the addition of all odd numbers in the given range. The sum should be displayed as 0 if the first integer is greater than second integer.




Test Cases: 1
Sample input:

2
10

Expected Output: 
24


Test Cases: 2
Sample Output:

8
2

Expected Output:
0




int number;
int min, max;
int sum = 0;

Console.WriteLine("Enter the minimum number");
min = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the maximum number");

max = Convert.ToInt32(Console.ReadLine());


for (number = min; number <= max; number++)
{
    if (number % 2 != 0)
        sum = sum + number;

}
Console.WriteLine(sum);




I am able to do first sample input. but i am not able to acheive second desired output?


Please help.
Thanks in advance.

解决方案

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


Just a little change in your code.

int number;
            int min, max;
            int sum = 0;

            Console.WriteLine("Enter the minimum number");
            min = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the maximum number");

            max = Convert.ToInt32(Console.ReadLine());

            if (max > min)
            {                              
               
                for (number = min; number <= max; number++)
                {
                    if (number % 2 != 0)
                        sum = sum + number;
                }
            }

            else
            {
                sum = 0;
            }
            Console.WriteLine(sum);


the else is enough and the WriteLine can be moved to the end to avoid dupliating.
Nice to see you were able to solve your problem by yourself.


use below code in c#

Random rnd = new Random();
Console.WriteLine("\n20 random integers from 1 to 10:");

int total=0;
for (int X = 1; X <= 20; X++)
{
    int y = rnd.Next(1, 10);
    if (y % 2 == 1)
    {
        // Add all the odd # and display sum.
total+=total+y;
    }
    else
    {
    }
    Console.WriteLine("{0}", y);
}
Console.ReadLine();


这篇关于WAP执行添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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