有没有更好的方法来写这个 [英] is there a better way to write this

查看:68
本文介绍了有没有更好的方法来写这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务说得到两个数字,这是我老师发给我的。是否有更好的方式来写这个并不会让人感到困惑?


if(firstNum%2 == 0)

counter = firstNum + 1;

else

counter = firstNum;


while(counter< = secondNum)

{

sumSquareOdd = sumSquareOdd + counter * counter;

counter = counter + 2;

}


cout<< 奇数之间的奇数之和的平方和。

<< firstNum<< "和 << secondNum<< " =

<< sumSquareOdd<<结束;

cout<< endl;



我正在考虑的问题


for(n = firstNum; n< = secondNum; n ++)

sumSquareOdd = sumSquareOdd + n * n;

n = n + 2;

{

if(n%2 == 0)

{

}

否则

{

cout<< 奇数的平方和是: << sumSquareOdd<<结束;

}

}


看起来不错吗?

解决方案

对不起!我刚刚根据论坛规则删除了我的答案。但代码可以减少到3行。

这里有一些建议:

你在for循环的正确轨道上,

你不需要if语句,bool可以被视为1或0,所以如果你多了false,结果将为零,如果你乘以true,结果将保持不变,所以你只需要要多做(n%2)

太多自我添加使用+ =而不是''foo = foo + bar''

您正在使用分支机构错了,这就是它们应该如何使用

展开 | 选择 | Wrap | L. ine数字


这是你的意思吗?

我有什么事吗?


for(n = firstNum; n< = secondNum; n ++)

{

sumSquareOdd = sumSquareOdd + n * n;

n = n + 2;

{

cout< ;< 奇数的平方和是: << sumSquareOdd<< endl;

}

}



这是你的意思吗?

我有什么事吗?


for(n = firstNum; n< = secondNum; n ++)

{

sumSquareOdd = sumSquareOdd + n * n;

n = n + 2;

{

cout< ;< 奇数的平方和是: << sumSquareOdd<<结束;

}

}



不完全。如果数字是奇数并且n = n + 2在for循环中是不合格的,那么你就不再打气了,因为你有n ++为每一个规则增加1到n。

所以切掉了n = n + 2将n * n乘以(n%2)以检查数字是否为奇数并且你不应该在for循环中进行cout调用(正如你现在所做的那样)。


the assignment says to get two numbers, here is what my teacher sent me. Is there a better way of writing this that is not as confusing?

if (firstNum % 2 == 0)
counter = firstNum + 1;
else
counter = firstNum;

while (counter <= secondNum)
{
sumSquareOdd = sumSquareOdd + counter * counter;
counter = counter + 2;
}

cout << "Sum of the squares of odd integers between "
<< firstNum << " and " << secondNum << " = "
<< sumSquareOdd << endl;
cout <<endl;


I was thinking on the lines of

for(n=firstNum;n<=secondNum;n++)

sumSquareOdd = sumSquareOdd + n * n;
n = n + 2;
{
if(n%2==0)
{
}
else
{
cout << "The sum of the square of the odd numbers are: " << sumSquareOdd << endl;
}
}

does that look right?

解决方案

sorry! I just deleted my answer according to the forum rules. but the code can be redused to 3 lines.
Here are some advice in stead:
You''r on the right track with the for loop,
you dont need the if statement, a bool can be treated as 1 or 0, so if you muliply with false the result will be zero, and if you multiply with true, the result will be unchanged, so all you need to do is muliply the square with (n % 2)
too self add use += instead of ''foo = foo + bar''
You are using the branches wrong, this is how they should be used

Expand|Select|Wrap|Line Numbers


Is this what you meant?
do I have anything right?


for(n=firstNum;n<=secondNum;n++)
{
sumSquareOdd = sumSquareOdd + n * n;
n = n + 2;
{
cout << "The sum of the square of the odd numbers are: " << sumSquareOdd << endl;
}
}


Is this what you meant?
do I have anything right?


for(n=firstNum;n<=secondNum;n++)
{
sumSquareOdd = sumSquareOdd + n * n;
n = n + 2;
{
cout << "The sum of the square of the odd numbers are: " << sumSquareOdd << endl;
}
}

Not quite. You are no longer cheching if the number is odd and the n = n + 2 is unnessesary in a for loop, because you have the n++ wich adds 1 to n for every runthough.
So cut out the n=n+2 multiply n*n with (n % 2) to check if the numer is odd and you shouldn''t have the cout call in the for loop (as you have now).


这篇关于有没有更好的方法来写这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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