Question

In this exercise, write a complete Java program that reads integer numbers from the user until...

In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to your code. Two samples of how your program should run are shown below (user input is in bold):

Please enter some numbers (a negative to stop):

7

3

5

2

-1

Average: 4.25

Please enter some numbers (a negative to stop): -2

Error: no positive numbers provided

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.util.Scanner;

public class Main
{
public static void main(String[] args) {
//scanner object creation
Scanner sc = new Scanner(System.in);
float sum=0,c=0,avg=0,temp=0;
  
System.out.println("Please Enter Some Numbers(a negative to stop): ");
//runs the loop
do
{
//reads input from user and store into temp variable
temp = sc.nextInt();
//checks the condition if entered number is negative ,if block will be executed
if(temp<0)
{
//prints the statement
System.out.println("Error: no positive numbers provided");
//breaks the while loop
break;
}
//entered number is not negatie then else block is executed
else
{
//calculates the sum of entered numbers   
sum+=temp;
// c variable keep count of how many numbers is entered by user, it is used in calculating average
c++;
}
}while(true);
//checks the condition sum is greater than 0 or not
if(sum>0)
{
//calculating the average entered numbers
avg = sum/c;
//statement prints the average
System.out.println("average: "+avg);
}
}
}

1 7 9 { { import java.util.*; 2 import java.util.Scanner; 3 4 public class Main 5-{ public static void main(String[] args) {

Add a comment
Know the answer?
Add Answer to:
In this exercise, write a complete Java program that reads integer numbers from the user until...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. The program must be in one file. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to...

  • Write a c++ complete program to meet the specifications. The program should prompt the user for...

    Write a c++ complete program to meet the specifications. The program should prompt the user for a positive integer. The program should print a message whether the integer is even or odd. The looping should end when the user enters a negative number. The negative number will not be tested for even or odd. The program will print out a message of how many numbers were entered (not counting the negative number) and how many even and odd numbers were...

  • Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done"...

    Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. Enter a number: 4 Enter a number: 5 Enter a number: bad data Invalid input Enter a number: 7 Enter a number: done 16 3...

  • Write a java program that keeps asking the user to enter a number until the user...

    Write a java program that keeps asking the user to enter a number until the user quits and prints how many even and how many odd numbers entered by the user. The loop terminates if the user enters -1 (it indicates that there will be no more number entering by the user). In other words, as long as the user doesn’t enter -1, the loop will be executed. Please add comments to the program to understand/ explain the code.

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • Please write a C++ program that will accept 3 integer numbers from the user, and output...

    Please write a C++ program that will accept 3 integer numbers from the user, and output these 3 numbers in order, the sum, and the average of these 3 numbers. You must stop your program when the first number from the user is -7. Design Specifications: (1) You must use your full name on your output statements. (2) You must specify 3 prototypes in your program as follows: int max(int, int, int); // prototype to return maximum of 3 integers...

  • Part A Write a Java program that reads an integer n from the keyboard and loops...

    Part A Write a Java program that reads an integer n from the keyboard and loops until −13 ≤ n ≤ 13 is successfully entered. A do-while loop is advised. Part B Write a Java program that reads an integer n from the keyboard and prints the corresponding value n!. [This is n factorial]. You must verify that the input integer satisfies the constraint 0 ≤ n ≤ 13; keep looping until the constraint is satisfied.   Will give thumbs up...

  • Write a Java program that reads in a positive integer from the user and outputs the...

    Write a Java program that reads in a positive integer from the user and outputs the following triangle shape composed of asterisk characters. For example, if user enters 5 as the input, then your program should display the following shape: ***** **** *** ** *

  • using c++ write a program that reads numbers from the user until the user enters a...

    using c++ write a program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: 1. Output the sum of all even numbers

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT