Results 1 to 4 of 4
Thread: Nested loops
- 02-10-2011, 10:24 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Nested loops
Im attempting to print---
Enter score 1 for student 1: 75
Enter score 2 for student 1: 82
Enter score 3 for student 1: 67
Average for student 1 is 74.67
Enter score 1 for student 2: 100
Enter score 2 for student 2: 66
Enter score 3 for student 2: 85
Average for student 2 is 83.67
Overall class average is 79.1
The lowest score was a 66 from student 2 on exam 2
The highest score was a 100 from student 2 on exam 1
(random scores obviously)
----
Im having trouble calculating the overall class average, and not sure where
to start for the lowest/highest score. Any input would be appreciated, this is my current code:
------Java Code:System.out.print("Enter number of students:"); int stud = Integer.parseInt(s.nextLine()); System.out.print("Enter number of exams:"); int exam = Integer.parseInt(s.nextLine()); double max = 0; double classum = 0; for (int i = 1; i <= stud; i++){ double sum = 0; for (int j = 1; j <= exam; j++){ System.out.print("Enter Score " + j + " for student " +i+":"); double score = Double.parseDouble(s.nextLine()); sum = sum+score; classum += sum; } System.out.println("Average for student "+ (sum/exam) ); } System.out.println("Overall class average is: " +classum/(exam*stud) ); System.out.println("The lowest score was a"); System.out.println("The highest score was a"); } }
Thanks for any help!Last edited by Aestuv; 02-10-2011 at 10:32 PM.
- 02-10-2011, 11:09 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
calculate classsum outside the inner loop, directly after, this way it will find one students test scores, then add it to the class sum and do this for every student, then when you do classsum/(student * exam) it will work correctly.
it will be easier to find min/max grades if you store the grades in an arraylist. This way, you can set the first item in the list to max, and loop through each item, if some item is greater then max, set it to the new max, at the end you will be left with the correct max value. Its similar for min. Here is an examples with an array and some numbers
This may be giving you a bit too much to be honest.Java Code:int[] intArr = {5, 2, 45, 98, 24 }; int max = intArr[0]; for(int i = 1; i < intArr.length; i++){ if(intArr[i] > max){ max = intArr[i]; } } System.out.println(max);
- 02-10-2011, 11:10 PM #3
So you don't have a nested loop problem at all.
For class sum take a close look at what your code is doing. Pretend you are the computer executing the code.
Enter score one > user enters 1 > sum = 0 + 1 > sum = 1 > classum = 0 + 1 > classum = 1
Enter score two > user enters 4 > sum = 1 + 4 > sum = 5 > classum = 1 + 5 > classum = 6
Hang on! The two scores are 1 and 4 so classum should be 5 but it is 6. <scratches head>
Enter score three > user enters 5 > sum = 5 + 5 > sum = 10 > classum = 6 + 10 > classum = 16
Wow, now classum is 16 but it should be 10. Hmmmm!Last edited by Junky; 02-10-2011 at 11:46 PM.
- 02-10-2011, 11:40 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Nested Loops
By joemama in forum New To JavaReplies: 1Last Post: 01-01-2011, 09:17 PM -
Nested Loops
By candygirl198827 in forum New To JavaReplies: 38Last Post: 12-01-2010, 06:03 AM -
Nested for loops
By luke in forum New To JavaReplies: 23Last Post: 10-21-2010, 02:49 AM -
nested for loops
By Implode in forum New To JavaReplies: 4Last Post: 09-01-2009, 08:47 AM -
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08-06-2007, 04:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks