Hi guys. Tell me this, if you wanted to count the number of A's, B's, C's, D's and so on in a program after the program has calculated the grades, how would you do this? I know it needs some type of counter. My code looks something like this... ___________________________________________________________________ if ((next >= 90) && (next <= 100 )) next = 'A'; counterA++; System.out.println("Number of A's = " + counterA) __________________________________________________________________
And I have declared int CounterA = 0; and so forth. What else do I need to do. Right now my program only shows one of each grade, A's - 1, B's- 1, and so on. What am I doing wrong? Thanks for your help.
I think you'll want to add some braces to your if statement so that counterA only gets incremented if the condition is true. Otherwise, this part looks okay, so I think the answer to your question really depends on what else surrounds this code.
(I'm curious about "next." It appears to be a score at first -- probably an int or double. But then it gets set to the value of a char...?) [ October 14, 2004: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
hi Rose Evans Let me share about my opinion on it First of all, you have declared a variable named "CounterA " please remember that Java is Case-sensitive
Sorry that I do not really understand your question There are several method that you can use. if ---- else if or switch condition that you could use. And you did not learn what is array, you can just use some simple vairables which can help you to store the number of that grade. Just what you did.
Shoot guys, I still must not know what I am doing. It has to be an easy counter that I need, but I can't seem to figure it out. ______________________________________________________________________
if ((next >= 90) && (next <= 100 )) next = 'A'; counterA++; System.out.println("Number of A's = " + counterA);
if ((next >= 80) && (next <= 89)) next = 'B'; counterB++; System.out.println("Number of B's = "+ counterB); ________________________________________________________________________
What in the world am I doing wrong. After someone inputs the grades, which I call next, and after the program runs, the program is supposed to count the number of A's, B's etc. I have set a variable counterA = 0. Each time I run the program, it just outputs the number 1 for each A, B, C and so on. I know there has to be a simple counter to do this. Any ideas? Thanks so much for you help. I think I am brain dead.
I assume there's some kind of loop for testing all the grades? Also, look at marc weber's post - you still need those curly braces... here's your code with some annotations...
if you change your code to this:
I might suggest slowing down a little in your code. don't try and do it all at once - you're garunteed to freak out then when it doesn't work (and it never works the first time - ask anybody here). I might suggest breaking down your problem into smaller pieces, and make sure they work before you start the next.
So, assumeing you have calculated all the grades somewhere, start off by just testing all of them to see what the letter should be, and print them out... something like
A D B A C A ...
and so on. Once THAT works, THEN try and count how many of each, and possibly print it out on every line....
number of A's: 1 number of D's: 1 number of B's: 1 number of A's: 2 number of C's: 1 number of A's: 3 ...
and so on.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors