Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    The last code I posted will work by itself, but I don't know how to implement it in the program I already have.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    What happens when you put the logic in that code inside the loop so it looks at each number as it is entered?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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.
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    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/
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    It is complete
    There are no import statements, class definition or method definition.
    The posted code in #6 will not compile.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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);
                    
        }
    }
    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.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    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.

    Comments on this post

    • the_passenger agrees : tyvm
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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;
    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.
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    You should use int values for counting vs double.
    Can the scores be floating numbers or are they also all int values?
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    44
    Rep Power
    2
    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.

    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.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo