Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //Description Write a program that calculates the average of
- //three user test scores, then display the average and a letter
- //grade that corresponds to the average.
- //Need to: Implement full validation of user input to
- //ensure than inputs are not negative; the validation routine should be
- //written as a method.
- //I changd the code like you requested btw you might wanna correct
- //the average to 2 decimal else you will have outputs like this: "Your average is 76.66666666666667 or C"
- //also this assumes that the input is a grade between 0 and 100 if the input is not always like
- //that then pm me i can write it then if you want but try it yourself first
- //also if the input is not a double or int or something in its scope like String, char, boolean it will
- //throw an error if you wanted that changed to an msg pm me --verd
- public class TestAverage {
- public static void main(String[] args) {
- double score1,score2,score3,
- average;
- score1 = scannerInput("Enter score 1:");
- score2 = scannerInput("Enter score 2:");
- score3 = scannerInput("Enter score 3:");
- average = (score1 + score2 + score3) / 3;
- if(average >= 90){
- System.out.println("Your average is " + average + " or A");
- }
- else if(average >= 80){
- System.out.println( "Your average is " + average + " or B");
- }
- else if(average >= 70){
- System.out.println("Your average is " + average + " or C");
- }
- else if(average >= 60){
- System.out.println("Your average is " + average + " or D");
- }
- else if(average < 60) {
- System.out.println("Your average is " + average + " or F");
- }
- }
- public static double scannerInput(String msg) {
- System.out.println(msg); //prints msg
- Scanner s =new Scanner(System.in); //creates scanner object
- double temp = s.nextDouble(); //gets double
- while(0>temp || temp>100) { //validation (if temp is lower than 0 OR if temp is higher than 100)
- System.out.println("The grade must be between 0 and 100!");
- temp = s.nextDouble();
- }
- return temp; // returns temp
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment