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

    Default Adding up all values generated from a for-loop

    Here's the code I have:

    Java Code:
    import java.io.*;
    import java.util.Random;
    
    public class Assignments
    	{
    		public static void main (String[] args)
          	{
    
    				Random gradeGenerator = new Random();
    				int finalAssignments = 0;
    				int grade = 0;
    
    				for (int i=1; i<=6; i++)
    					{
    						grade = gradeGenerator.nextInt(100) + 1;
    						System.out.println("The grade for assignment " + i + " is " + grade);
    					}		
    			}
    	}
    It's part of a much bigger program, but the details don't matter. Essentially what I need is to add up all of the values that will be generated by this code and store them in a variable.

    For instance, if all my values happen to be 1, then the final variable would be 15 because it added up all of the 1's.

    Thanks for any help! I've only been in a java class for a few weeks now, and I have a strong feeling that this is a really stupid question... thanks for dealing with the beginner!

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    you need to add up all the grades right? make a variable of type int called totalGrades and initialise it equal to zero. add grade to it for every iteration of your for loop,

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

    Default

    Yeah, you get what I'm trying to do. But I'm not entirely sure on how to go about doing what you recommended... I'm not looking for you to answer the problem for me, but give me a hint? lol

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

    Default

    You're obviously familiar with variable declaration, so you can do that part (hint: do that before the for loop). Are you familiar with the increment/decrement operators? (++, --, +=, -= ?) Hint: One of those goes in the for loop block. (Here is some info on those operators if you need it.)

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

    Default

    Yep, got it! Thanks for the help, I appreciate it. When I was working on it last night, it never dawned on me that I could have something like "finalAssignments += grade". I always that that I needed a number on the right hand side of the increment operator.

Similar Threads

  1. Adding Values of 2 Arrays
    By core2duo5252 in forum Java 2D
    Replies: 7
    Last Post: 09-05-2009, 04:50 AM
  2. restrictions on randomly generated values?
    By busdude in forum New To Java
    Replies: 6
    Last Post: 10-23-2008, 12:48 PM
  3. Replies: 0
    Last Post: 05-22-2008, 11:53 AM
  4. passing dynamically generated values
    By abhiN in forum Advanced Java
    Replies: 1
    Last Post: 01-20-2008, 04:21 AM
  5. BlackJack help please,, adding hand values
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 05:26 AM

Posting Permissions

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