This program is meant to take user input until a zero is entered, and then print out information on the integers. It’s also meant to read if the input in even/odd, calculate the sum, find the largest and smallest entered integers, tally the total integers entered, and find the average. It won’t stop when the user enters 0, and it won’t print the “No data entered” line if there’s no input. It also isn’t figuring even numbers properly.
import java.util.Scanner; public class Lab4 { public static void main(String[] args) { int counter = 0; double even = 0; double odd = 0; double sum = 0; int input = 0; int large = 0; int small = 0; double average; System.out.print("Enter a series of values (0 to quit): "); Scanner in = new Scanner(System.in); while ((input = in.nextInt()) != 0) { small = in.nextInt(); large = in.nextInt(); if (input != 0) sum = input + sum; counter++; if (input > large) large = input; if (input < small) small = input; if (input % 2 == 0) even = even + 1; else odd = odd + 1; } if (counter > 0) { average = sum / counter; System.out.println("The smallest integer is: " + small); System.out.println("The largest integer is: " + large); System.out.println("Total number of integers entered is " + counter); System.out.println("Total even numbers entered is " + even); System.out.println("Total odd numbers entered is " + odd); System.out.println("The average value is: " + average); } else { System.out.println("No data was entered."); } } }
Advertisement
Answer
When reading the first numbers to populate large and small we only need to do that once. And without re-reading the numbers with in.nextInt()
because that will eat up the next inputs that are entered, which was probably causing the not terminating at zero error.
while ((input = in.nextInt()) != 0) { if (counter == 0) small = large = input; if (input != 0) sum = input + sum; counter++; if (input > large) large = input; if (input < small) small = input; if (input % 2 == 0) even = even + 1; else odd = odd + 1; }