DnD 骰子滚动模拟

问题描述 投票:0回答:0

我正在尝试为我的朋友们制作一个模拟的掷骰子游戏。我想使用两个类 Dice 和一个子类 VarDice,这将让用户选择多少面。但是,我正在努力让我的派生类正常工作。

这是我的家长班:

import java.util.Random;

public class Dice {
   
   protected int numRolled;
   final Random rand = new Random();
    
    
   public void roll() {
        numRolled = rand.nextInt(5) + 1;
   }
   public int getNumRolled() {
      return numRolled;
   }
   
}

这是派生类:

    import java.util.Random;
    
    
    public class VarDice extends Dice {
    
       public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
            int numSides;
            
            public void setNumSides(int sides) {
                numSides = sides;
            }
            
           public int getNumSides() {
              return numSides;  
            }
            
            @Override
            public void roll() {
            numRolled = rand.nextInt(numSides) + 1;
           }
    
    }
    }

这终于是我的主要代码:

import java.util.Scanner; 

    public class Main {
    
       public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
             int numSides;

             VarDice dice = new VarDice();

             numSides = scar.nextInt();

             dice.setNumSides(numSides);
             
             dice.roll();
             System.out.println("The dice come up " + dice.getNumRolled()); 
        
    
        }
    
    }

我希望用户能够输入他们的骰子有多少面以及多少个骰子,但是我的 VarDice 类并没有很好地覆盖 roll 方法,我对我得到的错误有点迷茫......感谢您的帮助,希望格式合适。

java class random derived-class
© www.soinside.com 2019 - 2024. All rights reserved.