Java: Need to add the rolls of user-defined number of dice

Benjamin Parrish

New Computer Science major here, trying to figure something out. So, I am supposed to take a user-defined number of D6 and add the rolls together, along with a user-defined bonus (I think my professor is secretly a DnD fan...) to come up with a total. The only difficulty I am having is that my program is only adding the last roll and the bonus. Here is my code so far:

import java.util.Random;
import java.util.Scanner;

public class Dice
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        Random roll = new Random();

        int rollValue = 0;
        int numDice = 0;
        int statBonus = 0;
        int totalRoll = 0;

        System.out.println("How many 6-sided die would you like to roll? ");
        numDice = scan.nextInt();
        System.out.println("What number would you like to add to the rolls? ");
        statBonus = scan.nextInt();

        scan.close();
        for (int i = 0; i < numDice; i++)
        {
            rollValue = roll.nextInt(6) + 1;
            System.out.println("Roll is: " + rollValue);

        }
        totalRoll = statBonus + rollValue;

        System.out.println("The result of rolling " + numDice + " D6, and adding " + statBonus + " is: " 
    + totalRoll);
    }
}

Would be a massive help if someone could point me in the right direction! Thanks in advance!

--Ben

chsbellboy

Rewrite this section:

for (int i = 0; i < numDice; i++)
{
    rollValue = roll.nextInt(6) + 1;
    System.out.println("Roll is: " + rollValue);

}
totalRoll = statBonus + rollValue;

to

for (int i = 0; i < numDice; i++)
{
    rollValue = roll.nextInt(6) + 1;
    System.out.println("Roll is: " + rollValue);
    totalRoll += rollValue;
}
totalRoll = statBonus + totalRoll;

In other words, keep a running total.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dependent probabilities find out the different combinations of rolls that add up to a certain number when rolling two dice

From Dev

Number of different rolls of K N-sided dice

From Dev

From 5 dice rolls, generate a random number in the range [1 - 100]

From Dev

Do/while loop not counting number of rolls or lining up with dice

From Dev

Modelling dice rolls in F#

From Dev

Random dice rolls aren't random

From Dev

More efficient simluation of 2 dice rolls - Python

From Dev

Nested computation of Cartesian-product of dice rolls

From Dev

Ruby on Rails Form for Calculating Dice Rolls

From Dev

Functionally pure dice rolls in C#

From Dev

Java dice roll with unexpected random number

From Dev

How to reverse a number in Java accepted by user and add both reversed number

From Dev

How to reverse a number in Java accepted by user and add both reversed number

From Dev

User defined packages in Java

From Dev

How do I calculate the total and average of all dice rolls?

From Dev

What is causing my dice game to show an unrealistic amount of perfect rolls

From Dev

Modern SSDs need user-defined overprovisioning?

From Dev

Dice repeat need assistance

From Dev

How can I add validation onto a Date that rolls into another year using Java Date class

From Dev

Python Roll dice with 2 parameters: Number of sides of the dice and the number of dice

From Dev

Start ID value from a user defined number in Ebean, Play-framework 2.5.4 java

From Dev

Start ID value from a user defined number in Ebean, Play-framework 2.5.4 java

From Dev

java User Defined Classes and Objects

From Dev

Java Array of user defined type

From Dev

File Name defined by user in Java

From Dev

Change href to tel: + number (user defined)

From Dev

How can i add set of information (in user defined data type) to an Array in java

From Dev

How to add user defined constraints on a node/relationship in Neo4j using Java API

From Dev

Run user defined in csv; for the number of loop defined In Thread Group -Jmeter

Related Related

  1. 1

    Dependent probabilities find out the different combinations of rolls that add up to a certain number when rolling two dice

  2. 2

    Number of different rolls of K N-sided dice

  3. 3

    From 5 dice rolls, generate a random number in the range [1 - 100]

  4. 4

    Do/while loop not counting number of rolls or lining up with dice

  5. 5

    Modelling dice rolls in F#

  6. 6

    Random dice rolls aren't random

  7. 7

    More efficient simluation of 2 dice rolls - Python

  8. 8

    Nested computation of Cartesian-product of dice rolls

  9. 9

    Ruby on Rails Form for Calculating Dice Rolls

  10. 10

    Functionally pure dice rolls in C#

  11. 11

    Java dice roll with unexpected random number

  12. 12

    How to reverse a number in Java accepted by user and add both reversed number

  13. 13

    How to reverse a number in Java accepted by user and add both reversed number

  14. 14

    User defined packages in Java

  15. 15

    How do I calculate the total and average of all dice rolls?

  16. 16

    What is causing my dice game to show an unrealistic amount of perfect rolls

  17. 17

    Modern SSDs need user-defined overprovisioning?

  18. 18

    Dice repeat need assistance

  19. 19

    How can I add validation onto a Date that rolls into another year using Java Date class

  20. 20

    Python Roll dice with 2 parameters: Number of sides of the dice and the number of dice

  21. 21

    Start ID value from a user defined number in Ebean, Play-framework 2.5.4 java

  22. 22

    Start ID value from a user defined number in Ebean, Play-framework 2.5.4 java

  23. 23

    java User Defined Classes and Objects

  24. 24

    Java Array of user defined type

  25. 25

    File Name defined by user in Java

  26. 26

    Change href to tel: + number (user defined)

  27. 27

    How can i add set of information (in user defined data type) to an Array in java

  28. 28

    How to add user defined constraints on a node/relationship in Neo4j using Java API

  29. 29

    Run user defined in csv; for the number of loop defined In Thread Group -Jmeter

HotTag

Archive