Results 1 to 4 of 4

Thread: Nested loops

  1. #1
    Aestuv is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Unhappy 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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

    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);
    This may be giving you a bit too much to be honest.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  4. #4
    Aestuv is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks guys! You helped a lot

    Im still working on the minimum/maximum grades. We aren't suppose to use arrays yet.
    Last edited by Aestuv; 02-10-2011 at 11:47 PM.

Similar Threads

  1. Nested Loops
    By joemama in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 09:17 PM
  2. Nested Loops
    By candygirl198827 in forum New To Java
    Replies: 38
    Last Post: 12-01-2010, 06:03 AM
  3. Nested for loops
    By luke in forum New To Java
    Replies: 23
    Last Post: 10-21-2010, 02:49 AM
  4. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •