I'm very very new to Java so I'll try my best to explain my dilemma.
Basically, as an exercise, we must create a program that asks the user for input of scores and then outputs the scores as grades (ie enter 80, it says you have a distinction, enter 50 you have a pass, etc). The user can type as many grades as they want, and then can stop entering the grades by entering a negative number.
Now, I can do that part with a while loop and if, else if, loops, but i need for Java to keep count of the different scores, then process them into grades. As of now, its just taking the last score entered and then processing that into a grade.
Originally posted by Sebastian Smith: ... i need for Java to keep count of the different scores, then process them into grades. As of now, its just taking the last score entered and then processing that into a grade...
Welcome to JavaRanch!
I'm a little unclear on what you're after. If all you want to do is "keep count" (i.e., a running tally) of scores, then an int counter should do the trick, calling counter++ each time you process input.
On the other hand, maybe you're looking for a way to store each score that's input. The easiest way to do this would be an int used in conjunction with the counter described above. Then -- supposing you call the array "scores" -- you would have scores[counter] = input;
The problem with an array is that you need to set the size when you create it, so you'll have an upper bound on the number of scores it can hold. One way to address this limitation is with an ArrayList (in java.util), which is basically an automatically re-sizable array. However, unlike a simple array, an ArrayList can only hold objects -- not primitives. So you would need to wrap your int scores as Integers to put them into the ArrayList, and then cast them back down to type Integer when you pull them out (because within the ArrayList they're actually stored as type Object). I won't go into too much detail on this unless you decide to go this route and have questions...
"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
Joined: Oct 29, 2005
Cheers for the reply.
It sounds like I may need that second method, the array.
I'll rephrase myself ....Basically, I need for java to read a list of scores that the user types in. When the user is done, java displays the output as a list of grades (converting the scores to grades, so, e.g. the output would be 4 passes, 4 fails, etc)
However, we haven't touched upon arrays (we've mostly been working on loops)yet in the course, so is there an alternative method you can think of?
int score, distinctCount, passCount, failCount; distinctCount=0; passCount=0; failCount=0;
System.out.println("Enter the exam scores you wish to find the grades for."); System.out.println("When you have finished, enter a negative value to end."); Scanner keyboard = new Scanner(System.in); score = keyboard.nextInt();
if (score >= 80) distinctCount++; else if (score >=50) passCount ++; else if (score >=0) failCount ++;
while (score >=0) score = keyboard.nextInt();
System.out.println("Number of distinctions equals " + distinctCount); System.out.println("Number of passes equals " + passCount); System.out.println("Number of fails is " + failCount); } } [ October 30, 2005: Message edited by: Sebastian Smith ]