Having a little trouble

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rosicky2010
    New Member
    • Feb 2010
    • 3

    Having a little trouble

    Ok, so I was looking at how to display the total number of scores that are inputted, but I have yet to find one. So far I have found the average, high, and low but i can't figure out how to show how many scores are inputted. if you have any suggestions feel free to explain or point me in the right direction.

    This is what I have so far:
    Code:
    import java.util.*;
    
    class ScoreGrader{
      
       public static void main(String[] args){
         Scanner kb = new Scanner(System.in);
         
         System.out.println("Enter all the scores for the assignment followed by a -1.");
         
         int userNum;
         int high=0;
         int low=0;
         int count=0;
         int sum=0;
    
         
     userNum=kb.nextInt();
      if(userNum>=0){
       high=userNum;
       low=userNum;
       sum=userNum;
       count++;
    
      }
      while(userNum>=0){
       userNum=kb.nextInt();
       if(userNum>=0){
        if(userNum>high){
         high=userNum;
        }
        if(userNum<low){
         low=userNum;
        }
        sum=sum+userNum;
        count++;
    
       }
       
    
      }
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    You're iterating a counter variable...that 's the total number of inputs right there.

    Comment

    • Rosicky2010
      New Member
      • Feb 2010
      • 3

      #3
      Ok ty, after working the problem all night I have made some modifications due to the fact that mine was to basic.

      I think this is right. so if I am wrong please tell me:
      Code:
      import java.util.*;
      
      public class Project2{
        public static void main(String[] args){
          
           Scanner input = new Scanner(System.in);
               
           double grade = 0;
           double sum = 0;
           double high = 0;
           double low = 100;
           int counter = 0;
           int Acounter = 0;
           int Bcounter = 0;
           int Ccounter = 0;
           int Dcounter = 0;
           int Fcounter = 0;
           double Apercent = 0;     
           double Bpercent = 0;
           double Cpercent = 0;
           double Dpercent = 0;
           double Fpercent = 0;
          
           
      
           System.out.println( "Enter as many grades as you want, enter a -1 when finished: ");
           
          if (grade>=0 && grade<=100){ 
            
              while ( grade != -1 && (grade >=0 && grade<=100)){
         System.out.print("Input a grade: ");  
         grade = input.nextDouble();
               
            if (grade<=100 && grade>=90){
           Acounter++;
           counter++;
           sum = sum+grade;
          }
          else if (grade<=89 && grade>=80){
           Bcounter++;
           counter++;
           sum = sum+grade;
          }
          else if (grade<=79 && grade>=70){
           Ccounter++;
           counter++;
           sum = sum+grade;
          }
          else if (grade<=69 && grade>=60){
           Dcounter++;
           counter++;
           sum = sum+grade;
          }
          else if (grade<=59 && grade>=0){
           Fcounter++;
           counter++;
           sum = sum+grade;
         }    
               
          if (high<grade){
          high=grade;
          }
          if ((low>grade) && (grade> -1 )){
          low=grade; 
          }
       
        }
       }
        
          
          Apercent = (((double)Acounter/counter)*100.0);
          Bpercent = (((double)Bcounter/counter)*100.0);
          Cpercent = (((double)Ccounter/counter)*100.0);
          Dpercent = (((double)Dcounter/counter)*100.0);
          Fpercent = (((double)Fcounter/counter)*100.0);
          
          double average = (sum/counter);
          
      
       System.out.println("\n\nThe total amount of scores: " + counter);
       
       System.out.println("As:" + Acounter + " " + (Apercent) + "%");
       System.out.println("Bs:" + Bcounter + " " + (Bpercent) + "%");
       System.out.println("Cs:" + Ccounter + " " + (Cpercent) + "%");
       System.out.println("Ds:" + Dcounter + " " + (Dpercent) + "%");
       System.out.println("Fs:" + Fcounter + " " + (Fpercent) + "%");
       System.out.println("The highest valid grade you entered was: " + high);
       System.out.println("The lowest valid grade you entered was: " + low);
       System.out.println("The average of the valid grades you entered is: " + average);
       System.out.println("Want to enter another assignment (yes/No)? ");
       
      
       }
         
      }

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        At a glance it looks fine to me.

        I'm assuming this is homework for a programming class? If it isn't, I'd suggest using an array to store each number and then loop through them later, but if you haven't covered arrays in your class, it's a bad idea to use them in your homework :P

        Anyway, one change I will suggest. In the part where you are figuring out if it's an A, B, C, etc...you iterate a grade counter inside the if statement, which is good, but then you also iterate the overall counter, and add it to the sum. Those two steps aren't dependent on what's in the if statement; you will do those two steps regardless of what the number is. What you've done is include the code to do it in each if, but you could move it under this line:
        Code:
        grade = input.nextDouble();
        and only have to do it once.

        Ex:
        Code:
        grade = input.nextDouble();
        counter++;
        sum = sum+grade;
        if (grade<=100 && grade>=90){
        	Acounter++;
        }
        else if (grade<=89 && grade>=80){
        	Bcounter++;
        }
        else if (grade<=79 && grade>=70){
        	Ccounter++;
        }
        .
        .
        .
        It's just a simplification of logic. You only want things that are dependent on a condition inside a conditional statement.

        Comment

        • Rosicky2010
          New Member
          • Feb 2010
          • 3

          #5
          Ok thats a good idea, that will shorten the amount of programming I am doing and overall look better. And yes it is for a class, I didn't want someone to tell me what to do, I was just looking a getting a little help on certain parts. Thank you insertAlias

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Never too early to start learning best practices. Glad I could help.

            Comment

            Working...