Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

Write a java program that takes inputs from the user in a loop until three consecutive…

  1. User

    Write a java program that takes inputs from the user in a loop until three consecutive zeros are entered, then displays the sum, average, minimum and maximum of those numbers. You can consider the zeros in the input to be excluded in the sum / max / min / average. 1st Sample Input: 4 1 12 54 0 0 37 4 0 0 0

    1st Sample Output: Sum = 112
    Minimum = 1
    Maximum = 54 Average = 18.666666666666668

    2nd Sample Input: 0 0 0

    2nd Sample Output: No numbers found

  2. Assistant

    Sure! Below is a Java program that accomplishes what you described. The program reads integers from the user in a loop until three consecutive zeros are entered. It then calculates and displays the sum, average, minimum, and maximum of the entered numbers while ignoring the zeros used for termination.

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class ConsecutiveZeros {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            ArrayList<Integer> numbers = new ArrayList<>();
    
            int zeroCount = 0;
            while (zeroCount < 3) {
                System.out.print("Enter a number: ");
                int number = scanner.nextInt();
                
                if (number == 0) {
                    zeroCount++;
                } else {
                    zeroCount = 0;
                    numbers.add(number);
                }
            }
    
            if (numbers.isEmpty()) {
                System.out.println("No numbers found");
            } else {
                int sum = 0;
                int min = Integer.MAX_VALUE;
                int max = Integer.MIN_VALUE;
                
                for (int num : numbers) {
                    sum += num;
                    if (num < min) {
                        min = num;
                    }
                    if (num > max) {
                        max = num;
                    }
                }
                
                double average = sum / (double) numbers.size();
    
                System.out.println("Sum = " + sum);
                System.out.println("Minimum = " + min);
                System.out.println("Maximum = " + max);
                System.out.println("Average = " + average);
            }
            
            scanner.close();
        }
    }
    

    Explanation:

    1. Data Structures:

      • We use an ArrayList<Integer> to store the numbers entered by the user.
    2. Loop for Input:

      • The loop continues to read integers from the user until three consecutive zeros are entered.
      • zeroCount keeps track of the number of consecutive zeros. When a non-zero number is entered, zeroCount is reset to zero.
    3. Condition to Check Numbers:

      • After exiting the loop, the program checks if the numbers list is empty to print "No numbers found".
      • If the list is not empty, it calculates the sum, minimum, maximum, and average of the numbers.
    4. Output:

      • Finally, it prints the calculated sum, minimum, maximum, and average.

    Sample Run:

    Here's how a sample run of the program might look:

    Enter a number: 4
    Enter a number: 1
    Enter a number: 12
    Enter a number: 54
    Enter a number: 0
    Enter a number: 0
    Enter a number: 37
    Enter a number: 4
    Enter a number: 0
    Enter a number: 0
    Enter a number: 0
    Sum = 112
    Minimum = 1
    Maximum = 54
    Average = 18.666666666666668
    

    This program is straightforward to use and flexible in reading multiple inputs until the user inputs "000" consecutively.