473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. // main(): application entry point
  2.     public static void main(String[] args){
  3.  // input stream
  4.     Scanner stdin = new Scanner(System.in);  
  5.     // read a list of exam scores total them and categorize them A,B,C,D,F. 
  6.  
  7.    // initializing int
  8.     int inputsProcessed = 0;
  9.     int A = 0;
  10.     int B = 0;
  11.     int C = 0;
  12.     int D = 0;
  13.     int F = 0;
  14.  
  15.  System.out.println("Enter a list of grades:");
  16.  
  17. //get the first value
  18.  double inputs = stdin.nextDouble();
  19.  
  20.  //add total number of grades entered.
  21. while (inputs >= 0){
  22. //processed another value
  23. ++inputsProcessed;
  24. //get next value
  25. inputs = stdin.nextDouble();
  26.        }
  27.  
  28.        //process A's
  29.          if (inputs >= 90){      
  30.              //add each occurence
  31.               ++A;
  32.  
  33.          }
  34.         //process B's
  35.         else if (inputs >= 80){
  36.          //add each occurence      
  37.              ++B;
  38.  
  39.          }
  40.         //process C's
  41.          else if (inputs >= 70){
  42.                  //add each occurence 
  43.                  ++C;
  44.  
  45.         }    
  46.         //process D's
  47.          else if (inputs >= 60){
  48.          //add each occurence
  49.          ++D;
  50.  
  51.  
  52.          }
  53.         //process F's
  54.          else if (inputs >= 0){
  55.         //add each occurence
  56.         ++F;
  57.  
  58.           }
  59.  
  60.         //else {
  61.         //System.out.println("Proper input was not input");
  62.  
  63.  
  64.     System.out.println("Total number of grades:" + inputsProcessed);         
  65.     System.out.println("Number of A's = " + A );
  66.     System.out.println("Number of B's = " + B );
  67.     System.out.println("Number of C's = " + C );
  68.     System.out.println("Number of D's = " + D );
  69.     System.out.println("Number of F's = " + F );
  70.  
  71.  
  72.  
  73.  
  74.     }
  75.  
  76. }
Mar 7 '07 #1
4 2824
Ganon11
3,652 Expert 2GB
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.
Mar 7 '07 #2
r035198x
13,262 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. // main(): application entry point
  2.     public static void main(String[] args){
  3. // input stream
  4.     Scanner stdin = new Scanner(System.in); 
  5.     // read a list of exam scores total them and categorize them A,B,C,D,F. 
  6.  
  7. // initializing int
  8.     int inputsProcessed = 0;
  9.     int A = 0;
  10.     int B = 0;
  11.     int C = 0;
  12.     int D = 0;
  13.     int F = 0;
  14.  
  15. System.out.println("Enter a list of grades:");
  16.  
  17. //get the first value
  18. double inputs = stdin.nextDouble();
  19.  
  20. //add total number of grades entered.
  21. while (inputs >= 0){
  22. //processed another value
  23. ++inputsProcessed;
  24. //get next value
  25. inputs = stdin.nextDouble();
  26.      }
  27.  
  28.      //process A's
  29.          if (inputs >= 90){     
  30.              //add each occurence
  31.              ++A;
  32.  
  33.          }
  34.         //process B's
  35.      else if (inputs >= 80){
  36. //add each occurence 
  37.              ++B;
  38.  
  39.          }
  40.         //process C's
  41.          else if (inputs >= 70){
  42.                  //add each occurence 
  43.                  ++C;
  44.  
  45.         }    
  46.         //process D's
  47.          else if (inputs >= 60){
  48.          //add each occurence
  49.          ++D;
  50.  
  51.  
  52.          }
  53.         //process F's
  54.          else if (inputs >= 0){
  55.         //add each occurence
  56.         ++F;
  57.  
  58.          }
  59.  
  60.         //else {
  61.         //System.out.println("Proper input was not input");
  62.  
  63.  
  64.     System.out.println("Total number of grades:" + inputsProcessed);         
  65.     System.out.println("Number of A's = " + A );
  66.     System.out.println("Number of B's = " + B );
  67.     System.out.println("Number of C's = " + C );
  68.     System.out.println("Number of D's = " + D );
  69.     System.out.println("Number of F's = " + F );
  70.  
  71.  
  72.  
  73.  
  74.     }
  75.  
  76. }
There is also a way of using a switch for this.
Mar 8 '07 #3
Thanks for the help!
Mar 8 '07 #4
r035198x
13,262 8TB
Thanks for the help!
Did you get it to work then?
Mar 8 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: waldoruns | last post by:
I need help on this code. I have to write a code that reads in a list of exam scores and outputs the total number of grades and the total number of grades in each letter category. Here is what I...
4
by: hjc | last post by:
i am trying to created a program that will write a grading program for a class with the following policies there are 2 quizzes each graded on the basis of 10 points there is 1 midterm and 1...
2
by: tatum | last post by:
I really just need some pointers... jumping points from here... something the problem: Write a grading program for a class with the following grading policies: 1. There are two quizzes,...
9
by: gdarian216 | last post by:
I have written a c++ program that takes input from a file and outputs the average. The program uses structs and I need to convert the struct to a class. I just dont know how to get started and if...
2
by: gdarian216 | last post by:
the program reads input from a file and then outputs the averages and grade. for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if...
2
by: Richard Hollenbeck | last post by:
I originally wrote my grades program in MS-Access to help community college teachers in the California community colleges keep track of their students' grades and produce reports for the students...
3
by: Energizer100 | last post by:
Hey. I'm new to Java and I'm trying to make a grades program for a class assignment. This is what I have so far. import java.util.Scanner; import static java.lang.System.out; public class...
1
by: Sleepwalker817 | last post by:
Hello, I am trying to create a program that is supposed to calculate and print the average of several grades entered by the user. The output is supposed to look something like this:...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.