Results 1 to 4 of 4
  1. #1
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default Retreiving value from a for-loop

    I'm actually not entirely sure if this is what I even need to do. Essentially what I want is to get the "finalMidterms" and "finalfE" variables that were calculated within a for-loop and use them later on in a different class that will add up the two scores. The variables I'm talking about are in bold and underlined. Here is what I have:

    Java Code:
    import java.io.*;
    import java.util.Random;
    
    public class ComputeTestScore
    	{
    		public void finalExam()
          	{
    				Random gradeGenerator = new Random();
    				int finalfE = 0;
    
    				for (int i=1; i<=1; i++)
    					{
    						int grade = gradeGenerator.nextInt(120) + 1;
    						System.out.println("The grade for the final exam is " + grade);
    						[B][U]finalfE[/U][/B] += grade;
    					}		
    			}
    		public void Midterms()
          	{
    				Random gradeGenerator = new Random();
    				int finalMidterms = 0;
    
    				for (int i=1; i<=2; i++)
    					{
    						int grade = gradeGenerator.nextInt(30) + 1;
    						System.out.println("The grade for midterm "+ i + " is " + grade);
    						[B][U]finalMidterms[/U][/B] += grade;
    					}		
    			}
    		public void FinalTestScore()
    			{
    				int OverallTests = 0;
    				OverallTests = finalMidterms + finalfE;
    				System.out.println(OverallTests);
    			}
    	}
    Thanks for any help!

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You're close, but you've having an issue with scope. The variables finalfE and finalMidterms are local to their functions. What you probably want to do is, after each for loop, return the variable that you're modifying from the function (so they should be public int). Then, in your final test score function, you can add Midterms() + finalExam().

  3. #3
    BariMutation is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    (EDIT)

    Okay, so I think I'm a lot closer now, but it's still not working. Sorry for all the bother :( I thought I followed exactly what you said, so now I don't even know where I'm going wrong lol

    Java Code:
    import java.io.*;
    import java.util.Random;
    
    public class ComputeTestScore
    	{
    		public int finalExam()
          	{
    				Random gradeGenerator = new Random();
    				int finalfE = 0;
    
    				for (int i=1; i<=1; i++)
    					{
    						int grade = gradeGenerator.nextInt(120) + 1;
    						System.out.println("The grade for the final exam is " + grade);
    						finalfE += grade;
    					}		
    				return finalfE;
    			}
    		public int Midterms()
          	{
    				Random gradeGenerator = new Random();
    				int finalMidterms = 0;
    
    				for (int i=1; i<=2; i++)
    					{
    						int grade = gradeGenerator.nextInt(30) + 1;
    						System.out.println("The grade for midterm "+ i + " is " + grade);
    						finalMidterms += grade;
    					}	
    				return finalMidterms;	
    			}
    		public int FinalTestScore()
    			{
    				int OverallTests = 0;
    				OverallTests = finalMidterms() + finalfE();
    				System.out.println(OverallTests);
    			}
    		
    	}
    Last edited by BariMutation; 10-12-2010 at 11:23 PM. Reason: Fixed (?) issue

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default

    Any variables which are shared between multiple methods need to be declared outside of any method but still within the class.

    Regards.

Similar Threads

  1. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  2. Help in retreiving database data using a loop
    By danpj in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-17-2009, 09:49 AM
  3. Retreiving of mail body using mail number
    By chandu.v09 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-13-2008, 03:25 PM
  4. Retreiving list of keys from Map with certan value
    By desmond5 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 09:10 PM
  5. Replies: 1
    Last Post: 02-01-2008, 02:21 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
  •