while循环 - 偶数之和及其平均值

时间:2012-09-20 06:25:02

标签: java

http://pastebin.com/w8KntkE6#

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));
                }
            }

        }
    }
}

代码执行并且它对偶数进行求和。但是,当奇数输入时,它会返回错误或中断。我希望它忽略任何奇数。我的代码出了什么问题?

5 个答案:

答案 0 :(得分:2)

continue奇数的循环而不修改num - 看起来它应该无限循环给我。

答案 1 :(得分:2)

我错过了什么,或者当你有一个奇数时,你错过了nextInt()吗?因为即使你有num = scan.nextInt();。当num是奇数时,你不会。 变化

else if (num % 2 != 0){
    continue;
}

else if (num % 2 !=0){
    num = scan.nextInt();
    continue;
}

答案 2 :(得分:0)

更改为:

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();               
      }
}

答案 3 :(得分:0)

在continue语句之前添加以下代码。我希望它能正常工作。

System.out.print("Enter an integer (0 to quit): ");
            num = scan.nextInt();

答案 4 :(得分:0)

试试这个。我不太确定你想什么时候打印平均值,如果你想在最后打印它,可以将它移出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");
    }
}