Java D20骰子滚轮

问题描述 投票:-1回答:1

我正在尝试在java中为基于文本的RPG创建一个骰子滚动类。我是编程和寻求帮助的新手。提前致谢!!

    import java.util.Random;

    public class Dice {
        public Random random;

        public Dice() {
            this.random = new Random();
        }

        public int roll() {
            int rand = random.nextInt(20);
        }

        System.out.println("\nThe dice rolled a " + rand + ".");
    }
java random dice
1个回答
1
投票
  1. 你的陈述println不在方法之内
  2. 你使用的rand只是roll()方法中的一个变量
  3. 你没有在roll()方法中返回值

你需要类似的东西

public static void main (String[] args){
    Dice d = new Dice();
    System.out.println("The dice rolled a " + d.roll() + ".");
}

public int roll () {
    return random.nextInt(20); // value in [0;20[, so [0;19]
}
© www.soinside.com 2019 - 2024. All rights reserved.