Results 1 to 15 of 15
Thread: Trouble with arrays..
- 11-11-2011, 04:17 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Trouble with arrays..
Ok so here is my problem. I have to write a program where a user can input UP to 25 test scores. The program will then display the scores in a list, then the TOTAL number of test scores that were entered, and then display the highest, lowest, average, etc.
I have a lot of it done, but now I am having trouble with the following: I have to display the total number of test scores entered and I can't figure out how to do that. Then I have the average done however I need the average to be calculated by the number of scores the user entered, not the total number of scores the user CAN input (i hope that makes sense) Then I can't figure out the above average code.
Any help would so be greatly appreciated.
Java Code:public class TestData { // class fields private String testName; private String input; private final int MAXSIZE = 25; private int count; private int scores[]; public TestData() { testName = JOptionPane.showInputDialog(null, "Input the name for the test. For example, Test1."); count = 0; scores = new int[MAXSIZE]; loadArray(scores); } private void loadArray(int[] array) { // STORE INPUT IN ARRAY int testsScore = Integer.parseInt(JOptionPane.showInputDialog ("Input test score and press ENTER (-1 to end)")); while(testsScore != -1 && count < MAXSIZE) { scores[count] = testsScore; count++; if(count < MAXSIZE) { testsScore = Integer.parseInt(JOptionPane.showInputDialog( "Input test score and press ENTER (-1 to end)")); } } } public void displayResults() { // DISPLAY CONTENTS OF ARRAY AND REST OF OUTPUT System.out.println(" The results for " + testName); for(int count = 0; count < scores[count]; count++){ System.out.printf("%5d", scores[count]); if(count % 10 == 0) { System.out.println(); } } System.out.println(" "); System.out.printf("THE TOTAL NUMBER OF TEST SCORES %d\n", scores[count]); System.out.printf("THE NUMBER OF OUTSTANDING SCORES %10d\n", determineOutstanding()); System.out.printf("THE NUMBER OF SATISFACTORY SCORES %10d\n", determineSatisfactory()); System.out.printf("THE NUMBER OF UNSATISFACTORY SCORES %10d\n", determineUnsatisfactory()); System.out.printf("THE AVERAGE SCORES %20f\n", determineAverage()); System.out.printf("THE NUMBER OF ABOVE AVERAGE SCORES %10d\n", determineAboveAverage()); System.out.printf("THE NUMBER OF BELOW AVERAGE SCORES %10d\n", determineBelowAverage()); System.out.printf("THE NUMBER OF HIGHEST SCORES %10d\n", determineHighest()); System.out.printf("THE NUMBER OF LOWEST SCORES %10d\n", determineLowest()); } private int determineOutstanding() { // COUNT OUTSTANDING SCORES int outstanding = 0; for (int i = 0; i < count; i++) if(scores[i] >= 90) outstanding++; return outstanding; } private int determineSatisfactory() { // COUNT SATISFACTORY SCORES int satisfactory = 0; for (int i = 0; i < count; i++) if(scores[i] >= 60 && scores[i] <= 89) satisfactory++; return satisfactory; } private int determineUnsatisfactory() { // COUNT UNSATISFACTORY SCORES int unsatisfactory = 0; for (int i = 0; i < count; i++) if(scores[i] >= 0 && scores[i] <= 59) unsatisfactory++; return unsatisfactory; } private double determineAverage() { // CALCULATE AVERAGE double total = 0; double average; for (int i = 0; i < count; i++) total += scores[i]; average = total / scores.length; return average; } private int determineAboveAverage() { // COUNT SCORES ABOVE AVERAGE double total = 0; double aboveAverage; for (int i = 0; i < count; i++) if(scores[i]) return 0; // so it compiles }
- 11-11-2011, 04:27 AM #2
Re: Trouble with arrays..
What is the purpose of the count variable?
- 11-11-2011, 04:31 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Trouble with arrays..
To be honest, I'm not sure. My professor put that in the directions to use. Everything runs fine - it's just the problems I listed. I feel like when the user enters their scores I should be returning some sort of variable that I can use in the get average method and to help list the total number of scores entered. But I cannot think of how to go about that.
- 11-11-2011, 04:33 AM #4
- 11-11-2011, 04:36 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Trouble with arrays..
I understand what it is doing - I just don't know why he assigned the count variable.
and I can't figure out what variables I would use to calculate above average.
- 11-11-2011, 04:45 AM #6
Re: Trouble with arrays..
If you understand what it is doing then you can answer your own question. It is counting how many scores are entered.
- 11-11-2011, 05:09 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Trouble with arrays..
But when I display the total number of scores entered its outputting 25 which is the MAXSIZE not how many the user actually entered
- 11-11-2011, 05:14 AM #8
Re: Trouble with arrays..
Then the loop must be wrong. Put a debugging statement inside the loop to see how many times it gets executed and only input 1 value. If it still iterates 25 times instead of once then it is definately wrong.
- 11-11-2011, 05:16 AM #9
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Trouble with arrays..
what is a debugging statement? Sorry, I never heard of that
- 11-11-2011, 05:18 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Trouble with arrays..
Did you happen to look at my code - this is why I am so confused. I sent my prof. an email asking about the [count] because I'm not sure about it but anyway, my for loop with the scores being inputted works fine then the display method has to list the scores the user inputted and it does that just fine - but then I try to print the total number of scores entered and that comes up as "0"
I don't understand why that is.
- 11-11-2011, 05:18 AM #11
Re: Trouble with arrays..
Use a print statement to output something to the screen. Or have a dialog box popup to display a message.
- 11-11-2011, 05:24 AM #12
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Trouble with arrays..
Yes I have the println statement and all of my lines are printing. I guess my question is what variable do I call in order to display the total number of tests scores entered? I have the program displaying the scores individually but then I have to print a line with just the total number of scores the user put it but its coming up as 0
- 11-11-2011, 05:31 AM #13
Re: Trouble with arrays..
:headdesk:
The count variable is counting how many scores are getting entered. But you claim it is 25 regardless of how many scores are entered. So either you are wrong and are not using count correctly or the loop is executing 25 times regardless and count ends up being 25. I have suggested how to test what is actually happening. So now it is up to you to actually debug your code.
- 11-11-2011, 05:33 AM #14
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Trouble with arrays..
I asked how you debug the code
- 11-11-2011, 05:58 AM #15
Similar Threads
-
Trouble with arrays Help
By mrjaeyun in forum New To JavaReplies: 3Last Post: 11-07-2011, 10:14 PM -
More trouble with classes and arrays
By CuddlyKittens11 in forum Advanced JavaReplies: 9Last Post: 04-27-2011, 01:25 AM -
Trouble with arrays and classes
By CuddlyKittens11 in forum Advanced JavaReplies: 3Last Post: 04-25-2011, 12:42 AM -
Arrays trouble
By gto400no1 in forum New To JavaReplies: 1Last Post: 04-14-2010, 01:20 AM -
HELP: Still having trouble getting arrays :(
By Psyclone in forum New To JavaReplies: 4Last Post: 02-06-2010, 01:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks