3 Replies - 199 Views - Last Post: 02 February 2018 - 05:50 PM Rate Topic: -----

#1 kaendria01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-February 18

Problem with getting the average

Posted 02 February 2018 - 05:15 PM

I have a problem. I made a program that asks the user to enter three scores. The program returns the average grade but doesn't display the average. So what am I doing wrong that I can't get the average?

import java.util.Scanner;

public class TestScores {
  public static void main (String[] args) {
    
    Scanner keyboard = new Scanner(System.in);
    
    System.out.print("Enter the first Score");
            String score1;
            score1 = keyboard.nextLine();
            
            System.out.print("Enter the second score");
            String score2;
            score2 = keyboard.nextLine();
            
            System.out.print("Enter the third score");
            String score3;
            score3 = keyboard.nextLine();
            
            int answer;
            answer = (Integer.parseInt(score1) + Integer.parseInt(score2) + Integer.parseInt(score3) / 3);
            
            if (answer >= 90)
            
           System.out.println("The average score is + answer + .  You earned an A.");   
              
              else if (answer >= 80 && answer <=89)
              
              System.out.println("The average score is + answer + .  You earned a B.");
            
              else if (answer >= 70 && answer <= 79)

             System.out.println("The average score is + answer + .  You scored a C.");   
              
               else if (answer >= 60 && answer <= 69)

             System.out.println("The average score is + answer + .  You scored a D.");
               
               else
                 
             System.out.println("The average score is + answer + .  You scored an F.");    
                 
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with getting the average

#2 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Problem with getting the average

Posted 02 February 2018 - 05:37 PM

System.out.println("The average score is + answer + .  You earned an A.")


This is printing the literal line "The average score is + answer + . You earned an A." If you want it to include the value of answer you need to close the quoted portions like so:

System.out.println("The average score is " +  answer + ".  You earned an A.")



In general, it is helpful if you can be more specific about what the problem is. In this case, showing the bad output would have been useful - better still if you included a corrected version showing what you want it to look like. Thanks!
Was This Post Helpful? 1
  • +
  • -

#3 kaendria01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-February 18

Re: Problem with getting the average

Posted 02 February 2018 - 05:43 PM

That helped me greatly, thank you! But now that I fixed it, it gives me "The average score is 93 You earned an A." When I have put the test scores as all 40's

This post has been edited by ndc85430: 02 February 2018 - 08:59 PM
Reason for edit:: Removed quote of previous post. Please just press "Reply" in future.

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Problem with getting the average

Posted 02 February 2018 - 05:50 PM

Consider this line. What is 40 + 40 + 40 / 3? (think about operator precedence)

answer = (Integer.parseInt(score1) + Integer.parseInt(score2) + Integer.parseInt(score3) / 3);

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1