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
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.
Comments