我正在尝试在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 + ".");
}
println
不在方法之内rand
只是roll()
方法中的一个变量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]
}