Grades program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vadrama
    New Member
    • Mar 2007
    • 2

    #1

    Grades program

    takes a list of grades and counts the amount A's, B's, C's etc...

    I am using a while statement to count the total number of grades which works great, but my if and else statements to count how many A's, B's, etc...it seems the program is going through the While statement totally ignoring my if and else if statements.

    I tried doing muliple while statements but that gives unpredictable results. It seems I am missing one or two lines.
    Here is what I have at the moment.
    Code:
    // main(): application entry point
    	public static void main(String[] args){
     // input stream
    	Scanner stdin = new Scanner(System.in);  
    	// read a list of exam scores total them and categorize them A,B,C,D,F. 
    	
       // initializing int
    	int inputsProcessed = 0;
    	int A = 0;
    	int B = 0;
    	int C = 0;
    	int D = 0;
    	int F = 0;
    			
     System.out.println("Enter a list of grades:");
    
    //get the first value
     double inputs = stdin.nextDouble();
     
     //add total number of grades entered.
    while (inputs >= 0){
    //processed another value
    ++inputsProcessed;
    //get next value
    inputs = stdin.nextDouble();
    	   }
    
    	   //process A's
    		 if (inputs >= 90){	  
    			 //add each occurence
    			  ++A;
    			  
    		 }
    		//process B's
    	    else if (inputs >= 80){
             //add each occurence      
    			 ++B;
    			 
    		 }
    		//process C's
    		 else if (inputs >= 70){
    		 		//add each occurence 
    				 ++C;
    			
    		}	
    		//process D's
    		 else if (inputs >= 60){
    		 //add each occurence
    		 ++D;
    		
    		 
    		 }
    		//process F's
    		 else if (inputs >= 0){
    		//add each occurence
    		++F;
    	
    		  }
    		
    		//else {
    		//System.out.println("Proper input was not input");
    		
    						
    	System.out.println("Total number of grades:" + inputsProcessed); 		
    	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 );
    
    	
    	
    	
    	}
    	
    }
    Last edited by horace1; Mar 7 '07, 08:01 PM. Reason: added code tags
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You should add your if...else tests inside the while...loop. Currently, you get a bunch of data, but only perform the test on the last input (since it is outside the loop). You should

    1) Get the input
    2) Check what grade it is and increment the proper value
    3) Repeat
    4) After loop, report (output) your data.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by vadrama
      takes a list of grades and counts the amount A's, B's, C's etc...

      I am using a while statement to count the total number of grades which works great, but my if and else statements to count how many A's, B's, etc...it seems the program is going through the While statement totally ignoring my if and else if statements.

      I tried doing muliple while statements but that gives unpredictable results. It seems I am missing one or two lines.
      Here is what I have at the moment.
      Code:
      // main(): application entry point
      	public static void main(String[] args){
      // input stream
      	Scanner stdin = new Scanner(System.in); 
      	// read a list of exam scores total them and categorize them A,B,C,D,F. 
       
      // initializing int
      	int inputsProcessed = 0;
      	int A = 0;
      	int B = 0;
      	int C = 0;
      	int D = 0;
      	int F = 0;
       
      System.out.println("Enter a list of grades:");
       
      //get the first value
      double inputs = stdin.nextDouble();
       
      //add total number of grades entered.
      while (inputs >= 0){
      //processed another value
      ++inputsProcessed;
      //get next value
      inputs = stdin.nextDouble();
      	 }
       
      	 //process A's
      		 if (inputs >= 90){	 
      			 //add each occurence
      			 ++A;
       
      		 }
      		//process B's
      	 else if (inputs >= 80){
      //add each occurence 
      			 ++B;
       
      		 }
      		//process C's
      		 else if (inputs >= 70){
      				 //add each occurence 
      				 ++C;
       
      		}	
      		//process D's
      		 else if (inputs >= 60){
      		 //add each occurence
      		 ++D;
       
       
      		 }
      		//process F's
      		 else if (inputs >= 0){
      		//add each occurence
      		++F;
       
      		 }
       
      		//else {
      		//System.out.println("Proper input was not input");
       
       
      	System.out.println("Total number of grades:" + inputsProcessed); 		
      	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 );
       
       
       
       
      	}
       
      }
      There is also a way of using a switch for this.

      Comment

      • vadrama
        New Member
        • Mar 2007
        • 2

        #4
        Thanks for the help!

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by vadrama
          Thanks for the help!
          Did you get it to work then?

          Comment

          Working...