Discuss Ranking Numbers from a loop in the Java Help forum on Dev Shed. Ranking Numbers from a loop Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
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;
Total number of grades
Number of each grade (A, B and C etc...)
Percentage of each grade
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.
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.
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.
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/
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.
I'm sorry about that.
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.
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.
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.