java while 循环 - 偶数之和及其平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12507075/
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
while loop - sum of even numbers and its average
提问by etnemo
package javaapplication9;
import java.text.DecimalFormat;
import java.util.Scanner;
public class JavaApplication9 {
public static void main(String[] args) {
// TODO code application logic here
{
int sum = 0, num, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
while (num >= 0) // sentinel num of 0 to terminate loop
{
if (num % 2 == 0) {
count++;
sum += num;
System.out.println("The sum so far is " + sum);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
} else if (num % 2 != 0) {
continue;
}
System.out.println();
if (count == 0) {
System.out.println("No nums were entered.");
} else {
average = (double)sum / count;
DecimalFormat fmt = new DecimalFormat("0.###");
System.out.println("The average is " + fmt.format(average));
}
}
}
}
}
The code executes and it sums even numbers. However, when a odd number enters it returns an error or it breaks. I want it ignore any odd number. What is wrong with my code?
代码执行并且它对偶数求和。但是,当输入奇数时,它会返回错误或中断。我希望它忽略任何奇数。我的代码有什么问题?
回答by John3136
You continue
the loop on odd numbers without modifying num
- looks like it should infinite loop to me.
您continue
无需修改即可循环使用奇数num
- 对我来说似乎应该无限循环。
回答by Danielson
Am I missing something, or are you missing a nextInt()
when you have an odd number? Since in the if even you have num = scan.nextInt();
. You don't when num is odd.
Change
我是否遗漏了什么,或者nextInt()
当你有一个奇数时你是否遗漏了一个?既然在即使你有num = scan.nextInt();
。当 num 为奇数时,您不会。改变
else if (num % 2 != 0){
continue;
}
to
到
else if (num % 2 !=0){
num = scan.nextInt();
continue;
}
回答by CloudyMarble
Change to:
改成:
package javaapplication9;
import java.text.DecimalFormat;
import java.util.Scanner;
public class JavaApplication9 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
{
int sum = 0, num, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
while (num >= 0) // sentinel num of 0 to terminate loop
{
if (num % 2 == 0)
{
count++;
sum += num;
System.out.println("The sum so far is " + sum);
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
if (count == 0) {
System.out.println("No nums were entered.");
} else {
average = (double) sum / count;
DecimalFormat fmt = new DecimalFormat("0.###");
System.out.println("The average is " + fmt.format(average));
}
}
num = scan.nextInt();
}
}
回答by Wiki
just before continue statement add following code. I hope it will work correctly.
在 continue 语句之前添加以下代码。我希望它能正常工作。
System.out.print("Enter an integer (0 to quit): ");
num = scan.nextInt();
回答by scarcer
Try this. I'm not quite sure when do you want to print the average, you could move it out of the while loop if you want to print it at the end.
试试这个。我不太确定你想什么时候打印平均值,如果你想在最后打印它,你可以将它移出 while 循环。
public static void main(String[] args) {
// TODO code application logic here
{
int sum = 0, num, count = 0;
double average;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
while ((num = scan.nextInt())> 0) // sentinel num of 0 to terminate loop
{
if (num % 2 == 0){
count++;
sum += num;
System.out.println("The sum so far is " + sum);
if (count == 0) {
System.out.println("No nums were entered.");
} else {
average = (double) sum / count;
DecimalFormat fmt = new DecimalFormat("0.###");
System.out.println("The average is " + fmt.format(average));
}
}
System.out.print("Enter an integer (0 to quit): ");
}
System.out.println("end");
}
}