Question

Only used main method please. (Count positive and negative numbers and compute the average, of numbers)...

Only used main method please.

(Count positive and negative numbers and compute the average, of numbers)
Write a program that reads an unspecified number of integers , determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0.
Display the average as a floating-point number.
Display the total as a floating-point number.
Display the lowest number in the list.
Display the largest number in the list
Display how many numbers are in the list
Display how many negative numbers
Display how many positive numbers

SAMPLE RUN #1: java AvgWithLoop

Enter an integer, the input ends if it is 0:-44 -33 -12 15 89 45 113 117 45 0↵
The number of numbers is 9
The number of positives is 6↵
The number of negatives is 3↵
The largest number is 117
The smallest number is -44
The total is 335.0↵
The average is 37.22↵

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class AvgWithLoop {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter an integer, the input ends if it is 0:");
        int num, pos = 0, neg = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, count = 0;
        double total = 0;
        while (true) {
            num = in.nextInt();
            if (num == 0)
                break;
            ++count;
            total += num;
            if (num > 0) pos += num;
            else neg += num;
            if (num > max) max = num;
            if (num < min) min = num;
        }
        System.out.println("The number of numbers is " + count);
        System.out.println("The number of positives is " + pos);
        System.out.println("The number of negatives is " + neg);
        System.out.println("The largest number is " + max);
        System.out.println("The smallest number is " + min);
        System.out.println("The total is " + total);
        System.out.printf("The average is %.2f\n", total / count);
    }
}

Add a comment
Know the answer?
Add Answer to:
Only used main method please. (Count positive and negative numbers and compute the average, of numbers)...
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
  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a...

    Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a program that reads unspecified number of integers , determines how many negative and positive values have been read. Also calculate total and average. Your program will end with input 0. • Output Enter an integer, the input ends if it is 0: 25 34 -89 72 -35 -67 21 48 0 The number of positives is 5 The number of negatives is 3 The...

  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value have been read, and computes the total and average of the input values (not counting zeros. Your program ends with the input 0. Display the average as a floating point number Here are sample runs: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...

    Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...

  • Program 6: Sums and Averages. Assigned Wednesday, 10/05/16 Program Due Wednesday, 10/12/16 , at start of...

    Program 6: Sums and Averages. Assigned Wednesday, 10/05/16 Program Due Wednesday, 10/12/16 , at start of class. Write a program in c++ that asks the user to input a list of numbers, one at a time, until the user is finished. The program should keep track of how many numbers, how many positive numbers, and how many negative numbers were entered. It should also calculate the sums for each of these (positives, negatives, and total). After exiting the loop, the...

  • (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers....

    (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and display how many positive and negative values have been read, the maximum and minimum values, and the total and average of the input values (not counting the final 0).

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • Positive and negative: Return these four results using C++ reference parameter Write a function, named sums(),...

    Positive and negative: Return these four results using C++ reference parameter Write a function, named sums(), that has two input parameters; an array of floats; and an integer, n, which is the number of values stored in the array. Compute the sum of the positive values in the array and the sum of the negative values. Also count the number of positives and negative numbers in each category. Write a main program that reads no more than 10 real numbers...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

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