Java:需要添加用户定义的骰子数量的卷

时间:2016-03-25 03:15:35

标签: java

这里的新计算机科学专业,试图解决问题。因此,我应该采用用户定义的D6数量并将这些卷添加在一起,以及用户定义的奖励(我认为我的教授秘密地是DnD粉丝......)来提出总数。我遇到的唯一困难是我的程序只添加最后一卷和奖金。到目前为止,这是我的代码:

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);
    }
}
如果有人能指出我正确的方向,那将是一个巨大的帮助!提前谢谢!

- 本

1 个答案:

答案 0 :(得分:2)

重写本节:

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

}
totalRoll = statBonus + rollValue;

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

换句话说,保持一个总计。