3 Replies - 2756 Views - Last Post: 11 April 2010 - 06:07 PM Rate Topic: -----

#1 zacmac21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-April 10

rounding with printf function

Posted 11 April 2010 - 05:21 PM

I'm trying to get my program to round the average of the exam scores to 2 decimal places at the end of the code and can't get the printf function to work like I think it's supposed to. It's not rounding anything, just putting .00 at end of whatever number it comes up with. Any help would be appreciated. Here is the code.

 import java.util.*;

import java.util.Scanner;   // Needed for the Scanner class

public class ExamScoreWebWorking
{
	public static void main(String[]args)
	{
		Scanner in = new Scanner (System.in);  // create a Scanner object used to

           int score = 0;
           int count = 0;
           int scorea=0, scoreb=0, scorec=0, scored=0, scoref=0;
           double percenta, percentb, percentc, percentd, percentf;
           int total;
           double average;

           Scanner keyboard = new Scanner(System.in);

           total = 0;
           count = 0;

           System.out.print ("Enter final exam scores (-1 to end): ");
           score = keyboard.nextInt();

           while (score >= 0)
           {
               if (score >= 90)
                  {
                	++scorea;
                	total = total + score;
                  }
                  else if (score >= 80)
                  {
                     ++scoreb;
                	 total = total + score;
                  }
                  else if (score >= 70)
                  {
                     ++scorec;
                	 total = total + score;
                  }
                  else if (score >= 60)
                  {
                     ++scored;
                	 total = total + score;
                  }
                  else if (score <= 59)
                  {
                     ++scoref;
                	 total = total + score;
                  }

                ++count;

                  System.out.print ("Enter final exam scores (-1 to end): ");
                  score = keyboard.nextInt();
           }

           /*	 data validation
          		while (score > 100)
           	{ 	System.out.println ("Input error");
           		score = keyboard.nextInt();
				}  */

           System.out.println ("\nTotal number of scores: " + count);

           System.out.println ("\tTotal number of A's:  " + scorea);
           System.out.println ("\tTotal number of B's:  " + scoreb);
           System.out.println ("\tTotal number of C's:  " + scorec);
           System.out.println ("\tTotal number of D's:  " + scored);
           System.out.println ("\tTotal number of F's:  " + scoref);

           percenta = 100 * scorea / count;
           percentb = 100 * scoreb / count;
           percentc = 100 * scorec / count;
           percentd = 100 * scored / count;
           percentf = 100 * scoref / count;

           System.out.println ("\nIndividual grade percentages...");
           System.out.println ("\tA: " + percenta +"%");
		   System.out.println ("\tB: " + percentb +"%");
		   System.out.println ("\tC: " + percentc +"%");
		   System.out.println ("\tD: " + percentd +"%");
		   System.out.println ("\tF: " + percentf +"%");

           average = total/count;

           System.out.printf ("\n The average score: %.2f\n", average);


        }
}



Is This A Good Question/Topic? 0
  • +

Replies To: rounding with printf function

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: rounding with printf function

Posted 11 April 2010 - 05:24 PM

Since total and count are both ints, the result of total/count will be an int. This means that any decimal places will be truncated before the result is assigned to average. So if you have 9/2, the result will be 4, which will then be cast to a double when assigned to average, so the end result is 4.0. If you want the result to be 4.5, you can cast either total or count to double during division, like so: average = (double)total/count;. The cast tells Java to read total's value as a double, so 9.0/2 = 4.5.
Was This Post Helpful? 0
  • +
  • -

#3 zacmac21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 11-April 10

Re: rounding with printf function

Posted 11 April 2010 - 06:07 PM

Thank you! I knew it had to be something like that... much appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: rounding with printf function

Posted 11 April 2010 - 06:07 PM

Not a problem. Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1