java 编写一个函数,它接受 int n 并返回小于 n 的奇数之和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5097607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
writing a function that takes int n and returns the sum of odd less than n
提问by Eng.Fouad
Here is my code
这是我的代码
public static int sumOfOddLessThan(int n)
{
int iResult = 0;
for(int i = n - 1; i > 0 && i % 2 != 0; i--)
{
iResult = iResult + i;
}
return iResult;
}
It does not work correctly, I dunno why :\
它不能正常工作,我不知道为什么:\
It should return 4 when I enter 5 but it returns 0
当我输入 5 时它应该返回 4 但它返回 0
回答by Finbarr
Your conditional in the for loop reads:
你在 for 循环中的条件是:
i is greater than 0 and i is not even
.
i is greater than 0 and i is not even
.
When you call the method with 5 as argument, the first value of i will be 4, which is even and therefore the loop does not get evaluated.
当您使用 5 作为参数调用该方法时, i 的第一个值将是 4,这是偶数,因此不会评估循环。
for(i = n-1; i > 0; i++) {
if(i%2==0) {
iResult += i;
}
}
回答by Argote
You're putting the condition i % 2 != 0
in the for loop instead of an if
inside of the loop, hence if it's not met even once it breaks out of the entire loop.
您将条件i % 2 != 0
放在 for 循环中而不是循环if
内部,因此即使它脱离整个循环也没有满足。
Your code should look like this:
您的代码应如下所示:
public static int sumOfOddLessThan(int n)
{
int iResult = 0;
for(int i = n - 1; i > 0; i--)
{
if(i % 2 != 0) {
iResult = iResult + i;
}
}
return iResult;
}
Then again you don't even need a loop, you can evaluate it directly by getting the number of odd numbers lower than N
and squaring that.
然后,您甚至不需要循环,您可以通过使奇数的数量低于N
并对其进行平方来直接评估它。
回答by x.509
you should modify the forumla used for adding the series, all you gotta do is to modify it
您应该修改用于添加系列的论坛,您要做的就是修改它
earlier
早些时候
int i = (n+1)/2;
return (i*i)
modified
修改的
int i = n/2;
return (i*i);
TESTinput 1: return 0;
TEST输入1:返回0;
input 2: return 1;
输入2:返回1;
input 3: return 1;
输入3:返回1;
input 4: return 4;
输入4:返回4;
input 5: return 4;
输入5:返回4;
input 6: return 9;
输入6:返回9;
and so on ..
等等 ..
回答by David Ruttka
The second part of a for loop is a continuation condition. In your case, your continuation condition is i > 0 && i % 2 != 0
.
for 循环的第二部分是继续条件。在您的情况下,您的继续条件是i > 0 && i % 2 != 0
。
For n = 5, the first i is 4, and 4 % 2 is 0. Your continuation condition is not met, and this is why your for loop exits before it begins.
对于 n = 5,第一个 i 是 4,而 4 % 2 是 0。不满足继续条件,这就是 for 循环在开始之前退出的原因。
Try
尝试
for(int i = n - 1; i > 0; i--)
{
if (i % 2 != 0)
{
iResult = iResult + i;
}
}
回答by OmerGertel
The problem is that when the condition in the for is false, the loop exits.
问题是当 for 中的条件为假时,循环退出。
So for 5, i=4
and i % 2 != 0
is false, so the loop isn't accessed at all.
所以对于 5, i=4
andi % 2 != 0
是假的,所以根本没有访问循环。
Try this instead:
试试这个:
for(i=((n-1)%2==0?n-2:n-1 ; i>0; i=i-2)
{
i > 0 && i % 2 != 0;
}
Note that by reducing 2 from i
at each step, you don't have to check parity on every loop.
请注意,通过i
在每一步减少 2 ,您不必在每个循环中检查奇偶校验。
回答by TimCodes.NET
First you are setting i as n-1, so it would be 4 if n is 5, then your condition on the for loop states that i must be odd, which 4 is not, so it doesn't even do one loop. Try this:
首先,您将 i 设置为 n-1,因此如果 n 为 5,它将是 4,那么您在 for 循环上的条件表明 i 必须是奇数,而 4 不是,因此它甚至不会执行一个循环。试试这个:
public static int sumOfOddLessThan(int n)
{
int iResult = 0;
for(int i = n-1; i > 0; i--)
{
if (i % 2 != 0) iResult += i;
}
return iResult;
}