Results 1 to 5 of 5
- 10-10-2010, 03:09 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 22
- Rep Power
- 0
Adding up all values generated from a for-loop
Here's the code I have:
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.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); } } }
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!
- 10-10-2010, 03:13 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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,
- 10-10-2010, 07:31 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 22
- Rep Power
- 0
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
- 10-10-2010, 07:39 AM #4
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.)
- 10-10-2010, 05:31 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Adding Values of 2 Arrays
By core2duo5252 in forum Java 2DReplies: 7Last Post: 09-05-2009, 03:50 AM -
restrictions on randomly generated values?
By busdude in forum New To JavaReplies: 6Last Post: 10-23-2008, 11:48 AM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
passing dynamically generated values
By abhiN in forum Advanced JavaReplies: 1Last Post: 01-20-2008, 03:21 AM -
BlackJack help please,, adding hand values
By javakid9000 in forum New To JavaReplies: 1Last Post: 11-18-2007, 04:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks