Java 使用while循环计算奇数1-25和偶数1-50之和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25987137/
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
use while loops to calculate the sum of the odd number 1-25, and even number 1-50
提问by andy lie
i have problems with this program that i have to write. (First of all, english isnt my first language, therefore i have a hard time to understand what exactly i have to do for this program.)
我有这个程序的问题,我必须写。(首先,英语不是我的第一语言,因此我很难理解我到底要为这个程序做什么。)
Create an application Loops.java. The program must do the following:
Use a while loop to calculate the sum of the odd numbers 1-25.
Use a while loop to calculate the sum of the even numbers 1-50.
we are not adding odd number like 1+3+5+7+....+25,
we also not adding odd number up until the total value is 25
.
我们不会添加奇数,就像1+3+5+7+....+25,
我们也不会添加奇数,直到总值为25
。
the total value will be the sum of odd number ranging from 1 to 25
, as for even number will be from 1-50
总值将是从 的奇数之和1 to 25
,至于偶数将从1-50
as for the requirement, there will be many probability,
such as (1 + 3 = 5), (1 + 5 = 7), (1 +7 = 9)
and so on
至于需求,会有很多可能性,比如(1 + 3 = 5), (1 + 5 = 7), (1 +7 = 9)
等等
I wonder, since odd number from 1 to 25
has total 13
odd numbers and even number from 1-50
has 25
even numbers, what is the limit of the while loops there is no limit for the total odd number.
我不知道,因为奇数从1 to 25
已累计13
奇数和偶数1-50
有25
连号,什么是极限了while循环有总奇数数量没有限制。
Can anyone come up with this example?
任何人都可以提出这个例子吗?
Here is what i write at first, but after my professor explain the restriction and what i shouldn't do, I'm totally lost.
这是我一开始写的内容,但是在我的教授解释了限制和我不应该做的事情之后,我完全迷失了。
public class Loops
{
public static void main ( String[] args )
{
int oddNumber = 1;
int evenNumber = 2;
while ( oddNumber < 25 )
{
oddNumber = oddNumber + 2;
}
System.out.printf ( "Total sum of the odd number is %d\n", oddNumber );
while (evenNumber < 50)
{
evenNumber = evenNumber + 2;
}
System.out.printf ( "Total sum of the even number is %d\n", evenNumber );
}
}
采纳答案by DreadHeadedDeveloper
public class Loops
{
public static void main ( String[] args )
{
int oddNumber = 1;
int totalOdd = 0;
int evenNumber = 2;
int totalEven = 0;
while ( oddNumber <= 25 )
{
totalOdd = totalOdd + oddNumber;
oddNumber = oddNumber + 2;
}
System.out.printf ( "Total sum of the odd number is %d\n", totalOdd);
while (evenNumber <= 50)
{
totalEven = totalEven + evenNumber;
evenNumber = evenNumber + 2;
}
System.out.printf ( "Total sum of the even number is %d\n", totalEven);
}
}
Quick question, it says 1-25 and 1-50, does that include 25 and 50, or exclude 25 and 50???
快速提问,它说 1-25 和 1-50,是包括 25 和 50,还是不包括 25 和 50????
If it's excluding, code is fine as is, else, change the < into <= for the 2 while loops. Your question was difficult to understand so let me know if this is what you were looking for! I added the 2 new variables, totalOdd and totalEven to capture the totals of the odds and evens. Before, you were just returning the value of the odd and even number at top, namely, 25 and 50. And the way you got there didn't make sense either, you were just adding by 2 on the odd and even trail until you got to the desired number that would stop the while loop. You weren't even tracking the number of iterations. So I changed the code to just add up every number from 1- 25 that's odd and every number 1-50 that's even, and displayed the results. If anything don't make sense, just ask.
如果它被排除,代码就可以了,否则,将 < 更改为 <= 2个while循环。你的问题很难理解,所以如果这就是你要找的,请告诉我!我添加了 2 个新变量 totalOdd 和 totalEven 来获取赔率和偶数的总和。之前,您只是返回顶部奇数和偶数的值,即 25 和 50。 而且您到达那里的方式也没有意义,您只是在奇数和偶数路径上加 2,直到您达到了可以停止 while 循环的所需数字。您甚至没有跟踪迭代次数。因此,我更改了代码,将 1-25 中的每个奇数和 1-50 中的每个数字相加,然后显示结果。如果有什么不明白的,就问吧。
---EDIT---
- -编辑 - -
You said 25 and 50 ARE included, in which case, I have now updated the while loops to include the end numbers.
你说 25 和 50 包含在内,在这种情况下,我现在更新了 while 循环以包含结束编号。
回答by Darshan Lila
Do it this way:
这样做:
public class Loops
{
public static void main ( String[] args )
{
int oddNumber = 0;
int evenNumber = 0;
int number=1;
while ( number < 50 )
{
if(number <= 25 && number%2!=0){
oddNumber=oddNumber+number;
}else if(number%2==0){
evenNumber=evenNumber+number;
}
number++;
}
System.out.printf ( "Total sum of the odd number is %d\n", oddNumber );
System.out.printf ( "Total sum of the even number is %d\n", evenNumber );
}
}
You simply need a single loop and manage summation of odd
and even
numbers logically.
您只需要一个循环并逻辑地管理odd
和even
数字的总和。