VerdommeMan

Here like you requested

Aug 29th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //Description Write a program that calculates the average of
  4. //three user test scores, then display the average and a letter
  5. //grade that corresponds to the average.
  6.  
  7. //Need to: Implement full validation of user input to
  8. //ensure than inputs are not negative; the validation routine should be
  9. //written as a method.
  10.  
  11. //I changd the code like you requested btw you might wanna correct
  12. //the average to 2 decimal else you will have outputs like this: "Your average is 76.66666666666667 or C"
  13. //also this assumes that the input is a grade between 0 and 100 if the input is not always like
  14. //that then pm me i can write it then if you want but try it yourself first
  15. //also if the input is not a double or int or something in its scope like String, char, boolean it will
  16. //throw an error if you wanted that changed to an msg pm me --verd
  17. public class TestAverage {
  18.  
  19.    public static void main(String[] args) {
  20.            
  21.        double score1,score2,score3,
  22.               average;
  23.              
  24.        score1 = scannerInput("Enter score 1:");
  25.        score2 = scannerInput("Enter score 2:");
  26.        score3 = scannerInput("Enter score 3:");
  27.            
  28.        average = (score1 + score2 + score3) / 3;
  29.                  
  30.        if(average >= 90){
  31.            System.out.println("Your average is " + average + " or A");
  32.        }
  33.        else if(average >= 80){
  34.            System.out.println( "Your average is " + average + " or B");
  35.        }
  36.        else if(average >= 70){
  37.            System.out.println("Your average is " + average + " or C");
  38.        }
  39.        else if(average >= 60){
  40.            System.out.println("Your average is " + average + " or D");
  41.        }
  42.        else if(average < 60) {
  43.            System.out.println("Your average is " + average + " or F");
  44.        }
  45.        
  46.      
  47.    }
  48.    public static double scannerInput(String msg) {
  49.       System.out.println(msg); //prints msg
  50.       Scanner s =new Scanner(System.in); //creates scanner object
  51.       double temp = s.nextDouble(); //gets double
  52.      
  53.       while(0>temp || temp>100) { //validation (if temp is lower than 0 OR if temp is higher than 100)
  54.           System.out.println("The grade must be between 0 and 100!");
  55.           temp = s.nextDouble();
  56.       }
  57.        
  58.        return temp; // returns temp
  59.    }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment