Drewmann Drewmann - 25 days ago 4x
Java Question

Continuous Loop until Condition is met

So I have to create a java project in eclipse for my class. The Assignment is to create a program that will allow the user to enter integers into the program until a certain integer is enter (42). After the integer (42) is entered, the program will then 1. average all numbers entered. 2. display the min and max number entered. 3. Total number of numbers entered. And these have to be calculated without counting the (42). This is what i have so far. I can get input from the user and once they enter 42 the program stops and will display the total but with 42 included. And i'm not sure how i could add in a "count" for the input so i can use the count to display total numbers entered as well as dividing count from sum to give me the average. I appreciate any help.

package assignment6;

import java.util.Scanner;

public class assignment6 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);

//Enter integer
System.out.println("Enter a integer ");
int data = input.nextInt();

//Accept integers until 42 is entered
int sum = 42;
while (data != 42) {
sum += data;

System.out.println("Enter another integer ");
data = input.nextInt();
//Display sum of numbers entered
System.out.println("The sum of all numbers entered is: " + sum);

//Display Average of numbers entered
System.out.println("Average of all numbers entered is:" + ( sum/ ));



Check Below Code.

package stackoverflow;

import java.util.Scanner;

public class LoopTest {

public static void main(String[] args) 
    // TODO Auto-generated method stub

    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter number");

    int input  = 0;
    double sum = 0;
    double avg = 0;
    int count = 0;

    //Infinite Loop
    while (true)
        //Accept Number
        input  = scanner.nextInt();

        //Check Entered Number
        if (input  != 42)
            sum = sum + input ;
            count = count + 1;
            avg = sum/count;

    System.out.println("Numbers Entered : " + count);
    System.out.println("Sum of Numbers : " + sum);
    System.out.println("Average of Numbers : " + avg);