Question

In: Computer Science

Write Java code that allows a user to repeatedly enter numbers. Each time the user enters...

Write Java code that allows a user to repeatedly enter numbers. Each time the user enters a number, the program should print out the average of the last 3 numbers (or all numbers if 3 or less have been entered). I would like a detailed explanation so that a beginner level Java programmer could understand.

Solutions

Expert Solution

code:

import java.util.Scanner;

class Main {

public static void main(String[] args) {

//declare a scanner variable to take user input

Scanner sc = new Scanner(System.in);

int a=0,b=0,c=0,sum=0,count=0;

double avg;

//take user input untill -1 entered

/*

repeatedly ask for 3 numbers in a loop, and exit if any input is -1;

and subtract the last entered number in next loop and get next number and find average

*/

while (true){

//let's say we have taken a, b, c => 3 inputs,

//sum variable will be , sum = a+b+c, avg = sum /3;

//while repeats again, now we take 4th input, let's say it as a2,

//now, we need to subtract a from sum and add a2 to it ,

// sum = sum -a, sum = sum+a2, which means sum(b+c+a2), and find new average

sum -= a;

System.out.println("Enter number (-1 to exit): ");

a = sc.nextInt();

if(a == -1) break;

sum += a;

if(count < 3)

avg = (double)sum/1;

else

avg = (double)sum/3;

System.out.printf("Average : %.2f\n",avg);

sum -= b;

System.out.println("Enter number (-1 to exit): ");

b = sc.nextInt();

if(b == -1) break;

sum += b;

if(count < 3)

avg = (double)sum/2;

else

avg = (double)sum/3;

System.out.printf("Average : %.2f\n",avg);

sum -= c;

System.out.println("Enter number (-1 to exit): ");

c = sc.nextInt();

if(c == -1) break;

sum += c;

avg = (double)sum/3;

System.out.printf("Average : %.2f\n",avg);

count += 3;

}

}

}


Related Solutions

Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Write out code for a nested if statement that allows a user to enter in a...
Write out code for a nested if statement that allows a user to enter in a product name, store the product into a variable called product name and checks to see if that product exists in your nested if statement. You must include 5 product names to search for. If it is then assign the price of the item to a variable called amount and then print the product name and the cost of the product to the console. If...
In C# When the user enters an invalid value, ask the user to repeatedly enter the...
In C# When the user enters an invalid value, ask the user to repeatedly enter the value until a valid value has been entered. Gender must be ‘M’ or ‘F’. Residency must be ‘I’ or ‘O’. Existing Code: using System; public class Student {   public int credit;   public String firstname, lastname, gender, residency, edate;   public void input()   {     Console.WriteLine("\nWelcome to the Continental University Registration System!"); Console.WriteLine("\nEnter data about a student"); Console.Write("First Name: "); firstname = Console.ReadLine(); Console.Write("Last Name: "); lastname...
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers....
Write an assembly language program that repeatedly prompts the user to enter signed decimal integer numbers. The program should be run from the command prompt, output a text prompt to the screen, and then wait for the user to type in a number followed by the Enter key. (The legitimate range of user input values is any signed integer that can be represented in 32 bits.) After each number is entered, the program should determine and display the following information...
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code...
Write a C++ code to ask the user to enter 4 values (whole numbers). Your code should print the numbers in descending order.
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or...
(IN PYTHON) Write a program that asks the user repeatedly to enter a student's score or enter -1 to stop. When finished entering all the scores, the program should display the number of scores entered, the sum of the scores, the mean, the lowest and the highest score. (IN PYTHON)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT