Results 1 to 6 of 6
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Random number with a total.

    hey guys.

    im making this dicing simulation program.
    what it does: it generates a random number when you click the button "hit".
    it's in a GUI.

    whenever you click the button it creates a random number and displays it, and next to it it displays the total(ill explain later).

    the total is: every number generated, added up.

    like if you click the button 2 times:
    it generates these numbers(example): 14, 86
    then i want the total to be: 100.

    if you click the button 3 times:
    it generates these numbers(example):30, 70, 80
    then i want the total to be: 180.

    with the current code i have, the total is always equal to the random number generated. (which i dont want..).

    i don't really know how to do it.
    i tried thinking about a for-loop but i dont know how to put it in that for loop..

    so if anyone could help me with this, would be cool.

    code:
    Java Code:
    final JButton btnRoll = new JButton("Hit");
    		btnRoll.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				Random rand = new Random();
    				int rand1 = rand.nextInt(100)+1;
    				
    				lblNewLabel.setText(""+rand1);
    				
    				int totrand = +rand1;
    				
    				lblNewLabel_1.setText("Total: "+totrand);
    				
    				
    				
    				
    				
    			}
    		});

  2. #2
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: Random number with a total.

    It can be more complicated but to keep it simple, try

    changing

    Java Code:
     int totrand = +rand1;
    to
    Java Code:
     int totrand = totrand + rand1;
    I suppose you could also use a loop but I am not sure if that's necessary since you have an ActionListener object...

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Random number with a total.

    Why would you need a for loop? just as bigsonny said, totrand += rand1;
    so each time the button is pressed, the random numbers are added to the sum, which you store in totr

  4. #4
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Re: Random number with a total.

    Quote Originally Posted by superhaNds View Post
    Why would you need a for loop? just as bigsonny said, totrand += rand1;
    so each time the button is pressed, the random numbers are added to the sum, which you store in totr
    well both of your suggestions didnt work.

    everytime i click the button, it generates a random number and displays that random number in the total.
    but when i click the button again, it does exactly the same.
    it doesnt add those 2 numbers.

    i cant make it work

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default Re: Random number with a total.

    The 'totrand' variable is local to the method in the ActionListener; you get a new one each time that method is called.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Random number with a total.

    yeah sorry, totrand should be declared in class scope.

Similar Threads

  1. Counting total number of images in a website
    By sharifiit in forum Networking
    Replies: 0
    Last Post: 04-30-2011, 08:20 AM
  2. total number of words starting with a vowel
    By somnath6088 in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 09:52 AM
  3. Total number of reserved words in java
    By vivvy in forum New To Java
    Replies: 1
    Last Post: 02-06-2010, 03:18 PM
  4. Replies: 2
    Last Post: 12-10-2009, 12:06 PM
  5. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 09:13 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
  •