• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

counters

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming the grades go like this
90 - 100 = A
80 - 89 = B
70 - 79 = C
60 - 69 = D
< 60 = F

you could set up an array
int[] counter = new int[5];

Inside a for loop (looping the scores array) your if statement can be like
this (no need for both >= && <=)

if(score >= 90) counter[0]++;
else if(score >= 80) counter[1]++;
etc

then you loop through counter[] to get the results
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for all your help!! I'll see if I can do something different with my code..

Rose
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred,
Thank you!! This will really help!!! You seem to be really good at java. Keep up the great work.
Thanks again.
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic