etnemo
etnemo

Reputation: 3

while loop - sum of even numbers and its average

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

        }
    }
}

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?

Upvotes: 0

Views: 12179

Answers (5)

Danielson
Danielson

Reputation: 2696

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

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

to

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

Upvotes: 2

scarcer
scarcer

Reputation: 233

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.

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

Upvotes: 0

CloudyMarble
CloudyMarble

Reputation: 37566

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

Upvotes: 0

Wiki
Wiki

Reputation: 29

just before continue statement add following code. I hope it will work correctly.

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

Upvotes: 0

John3136
John3136

Reputation: 29266

You continue the loop on odd numbers without modifying num - looks like it should infinite loop to me.

Upvotes: 2

Related Questions