Question

In Java please:Write a program that will prompt the user to enter GPA values one per line, stopping when the user enters a negative Input: value. Print the following on separate lines: . The number of vaild GPAs entered » The sum of the GPAs 4.0 3.7 2.9 3.5 -1 The average GPA

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

CODE:

import java.util.*;

import java.lang.*;

import java.io.*;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); //scanner

int count = 0; //to store the number of valid gpas

System.out.println("Enter the GPA values one per line:");

double gpa = sc.nextDouble();

double sum = 0;

while(gpa >= 0){ //if gpa is non negative

count++; //increment the number of valid gpa

sum = sum + gpa; // add the gpa to sum

gpa = sc.nextDouble();

}

System.out.println("The number of valid GPAs entered: "+count);

System.out.println("The sum of the GPAs: "+sum);

System.out.println("The average GPA: "+sum/count);

}

}

OUTPUT SNIPPET:

Enter the GPA values one per line: 5 5.5 6.5 8.2 3 The number of valid GPAs entered: 4 The sum of the GPAs: 25.2 The average

Add a comment
Know the answer?
Add Answer to:
In Java please: Write a program that will prompt the user to enter GPA values one...
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
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
ADVERTISEMENT