Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
February 23rd, 2013, 02:55 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Homework - Ranking Numbers from a loop

I have to write a problem where I must take test grade inputs and display the following;

Number of each grade (A, B and C etc...)
Highest and Lowest Scores
Average test score.

I am able to do everything but display the highest and lowest scores with this code:

Code:
System.out.println("Enter exam scores from 100 to 0.  Use a negative "
+ "number to end your input.");
boolean areMore = true;
double next, sum = 0, max, min;
Scanner keyboard = new Scanner(System.in);
double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, number = 0;

while (areMore)
{

next = keyboard.nextDouble();

if ((next >= 90) && (next <= 100))
A++;
else if ((next <= 89) && (next >= 80))
B++;
else if ((next <= 79) && (next >= 70))
C++;
else if ((next <= 69) && (next >=60))
D++;
else if ((next <=59) && (next >=0))
F++;
if (next > 0)
number++;

if (next < 0)
areMore = false;
else
sum = sum + next;

}
System.out.println("Total number of grades = " + number);
System.out.println("Number of A's = " + A);
System.out.println("Number of B's = " + B);
System.out.println("Number of C's = " + C);
System.out.println("Number of D's = " + D);
System.out.println("Number of F's = " + F);
System.out.println("Percentage of A's is " + A / number * 100 + "%");
System.out.println("Percentage of B's is " + B / number * 100 + "%");
System.out.println("Percentage of C's is " + C / number * 100 + "%");
System.out.println("Percentage of D's is " + D / number * 100 + "%");
System.out.println("Percentage of F's is " + D / number * 100 + "%");
// System.out.println("The highest score is: " + max);
// System.out.println("The lowest score is: " + min);
System.out.println("The average score is: " + sum / number);

I have tried to get the highest and lowest to display via this code:

Code:
max = keyboard.nextDouble();
min = max;
next = keyboard.nextDouble();
while (areMore)
if (next > max)
max = next;
else if (next < min)
min = next;
next = keyboard.nextDouble();

When I use this in my program the first input is never put into the loop, and consequently everything else gets messed up. Also, the sentinel value I use to end my loop keeps getting recorded as the lowest value. I'm stuck on this one.

#2
February 23rd, 2013, 03:01 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
The last code I posted will work by itself, but I don't know how to implement it in the program I already have.

#3
February 23rd, 2013, 03:58 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
What happens when you put the logic in that code inside the loop so it looks at each number as it is entered?

#4
February 23rd, 2013, 04:06 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
The first input in the loop does not get put into the values...although the max and min will be output correctly.

If I put in:

97
87
77
67
57

The 97 will not show up as an A, I will get 4 results total, and the mean will be wrong. Although, the max will be output as 97.

#5
February 23rd, 2013, 04:49 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
Please post the current version of the code that will compile, execute and show the problem.

Also post the program's output that shows what is wrong and add some comments saying what the output should be.

#6
February 23rd, 2013, 06:18 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
So when I take out the Min & Max parts, this code works perfectly well. Here is the code with the Min and Max code put in:

Code:
System.out.println("Enter exam scores from 100 to 0.  Use a negative "
+ "number to end your input.");
boolean areMore = true;
double next, sum = 0, max, min;
Scanner keyboard = new Scanner(System.in);
double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, number = 0;
max = keyboard.nextDouble();
min = max;
next = keyboard.nextDouble();

while (areMore)
{

next = keyboard.nextDouble();

if ((next >= 90) && (next <= 100))
A++;
else if ((next <= 89) && (next >= 80))
B++;
else if ((next <= 79) && (next >= 70))
C++;
else if ((next <= 69) && (next >=60))
D++;
else if ((next <=59) && (next >=0))
F++;
if (next > 0)
number++;

if (next > max)
max = next;
else if (next < min)
min = next;
next = keyboard.nextDouble();

if (next < 0)
areMore = false;
else
sum = sum + next;

}
System.out.println("Total number of grades = " + number);
System.out.println("Number of A's = " + A);
System.out.println("Number of B's = " + B);
System.out.println("Number of C's = " + C);
System.out.println("Number of D's = " + D);
System.out.println("Number of F's = " + F);
System.out.println("Percentage of A's is " + A / number * 100 + "%");
System.out.println("Percentage of B's is " + B / number * 100 + "%");
System.out.println("Percentage of C's is " + C / number * 100 + "%");
System.out.println("Percentage of D's is " + D / number * 100 + "%");
System.out.println("Percentage of F's is " + D / number * 100 + "%");
System.out.println("The highest score is: " + max);
System.out.println("The lowest score is: " + min);
System.out.println("The average score is: " + sum / number);

Output
Quote:
 Enter exam scores from 100 to 0. Use a negative number to end your input. 97 87 77 67 57 -1 Total number of grades = 2.0 Number of A's = 0.0 Number of B's = 0.0 Number of C's = 1.0 Number of D's = 0.0 Number of F's = 1.0 Percentage of A's is 0.0% Percentage of B's is 0.0% Percentage of C's is 50.0% Percentage of D's is 0.0% Percentage of F's is 0.0% The highest score is: 97.0 The lowest score is: 57.0 The average score is: 33.5

The Min and Max work, but for some reason, everything else is a train wreck. The percents don't even correlate with the number of instances of the grades. If I take out the min and max code I am using, everything works fine.

#7
February 23rd, 2013, 06:25 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
If I had to guess, something is getting messed up with the keyboard.nextDouble() statements. I have no idea why the 67 does not get recorded. The max and min are working as intended, but how to incorporate them into what I already have.

#8
February 23rd, 2013, 06:41 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
Please post a complete program that will compile, execute and show the problem.

The data that is entered and read into the next variable before the while loop is not used.
The data that is read into the max variable is NOT tested for a grade letter.

There should only be one call to the nextDouble() method. The data entered with the other nextDouble() method calls is not used correctly and/or lost/

#9
February 23rd, 2013, 06:45 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Quote:
 Originally Posted by NormR Please post a complete program that will compile, execute and show the problem.

Thank you very much...I thought I did though...? It is complete, compiles, and shows the problem.

#10
February 23rd, 2013, 06:52 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
Quote:
 It is complete

There are no import statements, class definition or method definition.
The posted code in #6 will not compile.

#11
February 23rd, 2013, 06:58 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Quote:
 Originally Posted by NormR There are no import statements, class definition or method definition. The posted code in #6 will not compile.

Code:
package combo;

import java.util.Scanner;

public class Combo
{

public static void main(String[] args)

{
System.out.println("Enter exam scores from 100 to 0.  Use a negative "
+ "number to end your input.");
boolean areMore = true;
double next, sum = 0, max, min;
Scanner keyboard = new Scanner(System.in);
double A = 0, B = 0, C = 0, D = 0, E = 0, F = 0, number = 0;

max = keyboard.nextDouble();
min = max;

while (areMore)
{
next = keyboard.nextDouble();
if (next > max)
max = next;
else if (next < min)
min = next;

if ((next >= 90) && (next <= 100))
A++;
else if ((next <= 89) && (next >= 80))
B++;
else if ((next <= 79) && (next >= 70))
C++;
else if ((next <= 69) && (next >=60))
D++;
else if ((next <=59) && (next >=0))
F++;
if (next > 0)
number++;

if (next < 0)
areMore = false;
else
sum = sum + next;

}
System.out.println("Total number of grades = " + number);
System.out.println("Number of A's = " + A);
System.out.println("Number of B's = " + B);
System.out.println("Number of C's = " + C);
System.out.println("Number of D's = " + D);
System.out.println("Number of F's = " + F);
System.out.println("Percentage of A's is " + A / number * 100 + "%");
System.out.println("Percentage of B's is " + B / number * 100 + "%");
System.out.println("Percentage of C's is " + C / number * 100 + "%");
System.out.println("Percentage of D's is " + D / number * 100 + "%");
System.out.println("Percentage of F's is " + D / number * 100 + "%");
System.out.println("The highest score is: " + max);
System.out.println("The lowest score is: " + min);
System.out.println("The average score is: " + sum / number);

}
}

Quote:
 run: Enter exam scores from 100 to 0. Use a negative number to end your input. 97 87 77 67 57 -1 Total number of grades = 4.0 Number of A's = 0.0 Number of B's = 1.0 Number of C's = 1.0 Number of D's = 1.0 Number of F's = 1.0 Percentage of A's is 0.0% Percentage of B's is 25.0% Percentage of C's is 25.0% Percentage of D's is 25.0% Percentage of F's is 25.0% The highest score is: 97.0 The lowest score is: -1.0 The average score is: 72.0 BUILD SUCCESSFUL (total time: 4 seconds)

I took out the the next = keyboard.nextDouble() before the loop. That has made things better, but testing the min and max for grades isn't going so well. The sentinel value is appearing in the min which isn't great either.

#12
February 23rd, 2013, 07:02 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
One trick for finding a max value is to initialize max to a very small or negative number.
The first compare will be larger.
For the min value, set min to a very large number. The first compare will be smaller.
the_passenger agrees: tyvm

#13
February 23rd, 2013, 07:16 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Ahh, thats very helpful. I added this code to get the sentinel out of the Min

Code:
else if ((next < min) && (next > 0))
min = next;

Quote:
 run: Enter exam scores from 100 to 0. Use a negative number to end your input. 97 87 77 67 57 -1 Total number of grades = 5.0 Number of A's = 7.0 Number of B's = 1.0 Number of C's = 1.0 Number of D's = 1.0 Number of F's = 1.0 Percentage of A's is 140.0% Percentage of B's is 20.0% Percentage of C's is 20.0% Percentage of D's is 20.0% Percentage of F's is 20.0% The highest score is: 97.0 The lowest score is: 1.0 The average score is: 77.0 BUILD SUCCESSFUL (total time: 6 seconds)

How exactly is my min being determined to be 1?

edit - The min is my only issue now. Everything else is great.

#14
February 23rd, 2013, 07:22 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,952
Time spent in forums: 1 Week 6 Days 2 h 34 m 5 sec
Reputation Power: 345
You should use int values for counting vs double.
Can the scores be floating numbers or are they also all int values?

#15
February 23rd, 2013, 07:25 PM
 the_passenger
Contributing User

Join Date: Feb 2013
Posts: 44
Time spent in forums: 11 h 44 m 41 sec
Reputation Power: 1
Quote:
 Originally Posted by NormR Can the scores be floating numbers or are they also all int values?

The test score inputs have been declared as doubles.

Quote:
 You should use int values for counting vs double.

As in for counting the number of A's, B's, etc? I am doing that.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Homework - Ranking Numbers from a loop