“DnD Dice Roll Simulation” 可以翻译为 “龙与地下城骰子掷骰模拟”。

huangapple go评论72阅读模式
英文:

DnD Dice Roll Simulation

问题

I can help you with the translation of the provided code. Here's the translated code:

我正在尝试制作一个模拟掷骰子游戏给我的朋友们我想利用两个类Dice骰子和它的子类VarDice可变骰子),让用户选择骰子有多少面然而我在让我的派生类正常工作方面遇到了困难

这是我的父类

```java
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 {

   private 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 = scnr.nextInt();

         dice.setNumSides(numSides);
         
         dice.roll();
         System.out.println("骰子的点数是:" + dice.getNumRolled()); 
    
    }
}

我希望用户能够输入骰子有多少面以及投掷多少次,但是我的VarDice类在覆盖roll方法方面存在问题,我对错误感到有些迷茫...感谢您的帮助,希望这个格式合适。


<details>
<summary>英文:</summary>

I am Trying to make a simulated dice rolling game for my friends. I want to utilize two classes Dice and a child class VarDice which will let user choose how many sides. However, I am struggling to get my derived class to work properly. 

 This is my parent class :

    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;
       }
       
    }

This is the derived class: 

        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;
               }
        
        }
        }

This is finally my main code: 

    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(&quot;The dice come up &quot; + dice.getNumRolled()); 
            
        
            }
        
        }
I want the user to be able to input how many sides are on their dice and how many dice, but my VarDice class is not overriding the roll method very well and I am a little bit lost on the errors I am getting... I appreciate the help and hope the format is appropriate.  

</details>


# 答案1
**得分**: 1

以下是翻译好的内容:

1. `VarDice` 的代码位于一个不应该在这里的 `main` 方法中。
2. 正如 @Bohemian 在评论中指出的,你其实不需要两个带继承关系的类。我建议去掉父类,因为默认的6面情况可以通过默认构造函数来处理。
3. 是的,@Bohemian,"Dice" 是 "Die" 的复数形式。

你的 `Die` 类可以看起来像这样:

```java
import java.util.Random;

public class Die {
	private final Random rand;
	private int numSides;
	private int numRolled;

	public Die(int numSides) {
		this.rand = new Random();
		this.numRolled = -1;
		// 自定义面数,如果无效则默认为6
		this.numSides = numSides > 0 ? numSides : 6;
	}

	public Die() {
		this(6); // 默认为6
	}

	public void setNumSides(int sides) {
		numSides = sides;
		numRolled = -1;
	}

	public int getNumSides() {
		return numSides;
	}

	public int getNumRolled() {
		if (numRolled < 0) {
			roll();
		}
		return numRolled;
	}

    public int roll() {
    	numRolled = rand.nextInt(numSides) + 1;
    	return numRolled;
    }
}

然后在你的应用程序中可以像这样使用这个类:

import java.util.Scanner;

public class App {

	public static void main(String[] args) {
		// 使用默认骰子
		Die die = new Die(); // 创建带有默认面数的实例
		System.out.println(die.roll()); // 掷骰子并打印结果
		System.out.println(die.getNumRolled()); // 再次打印掷骰子的数字
		// 使用自定义骰子
		Scanner scanner = new Scanner(System.in);
		System.out.print("设置面数(大于0):");
		Die customDie = new Die(scanner.nextInt());
		System.out.println(customDie.roll()); // 掷自定义骰子
		scanner.close();
	}

}
英文:

There are some problems, here:

  1. The code of VarDice is in a main method that doesn't belong here.
  2. As @Bohemian pointed out in the comments, you don't really need two classes with inheritance, here. I'd drop the parent class, because the default case of 6 sides can be handled by a default constructor.
  3. And yes, @Bohemian, "Dice" is the plural of "Die".

Your Die class could look something like this:

import java.util.Random;

public class Die {
	private final Random rand;
	private int numSides;
	private int numRolled;

	public Die(int numSides) {
		this.rand = new Random();
		this.numRolled = -1;
		// custom no. of sides, 6 if invalid
		this.numSides = numSides &gt; 0 ? numSides : 6;
	}

	public Die() {
		this(6); // 6 is default
	}

	public void setNumSides(int sides) {
		numSides = sides;
		numRolled = -1;
	}

	public int getNumSides() {
		return numSides;
	}

	public int getNumRolled() {
		if (numRolled &lt; 0) {
			roll();
		}
		return numRolled;
	}

    public int roll() {
    	numRolled = rand.nextInt(numSides) + 1;
    	return numRolled;
    }
}

And you'd use this class something like this in your application:

import java.util.Scanner;

public class App {

	public static void main(String[] args) {
		// use default die
		Die die = new Die(); // create instance with default no of sides
		System.out.println(die.roll()); // roll die and print result
		System.out.println(die.getNumRolled()); // print rolled number again
		// use custom die
		Scanner scanner = new Scanner(System.in);
		System.out.print(&quot;Set number of sides (greater than 0): &quot;);
		Die customDie = new Die(scanner.nextInt());
		System.out.println(customDie.roll()); // roll custom die
		scanner.close();
	}

}

答案2

得分: 0

我注意到的几个错误:

这行代码 numSides = scar.nextInt(); 应该是 numSides = scnr.nextInt();

类 VarDice 把所有内容都包裹在 public static void main(String[] args) {...} 中。去掉这个包裹,确保代码是类的一部分,而不是方法的一部分。

做了这些修改后,代码在我的电脑上运行正常。

英文:

A few mistakes I noticed:

the line numSides = scar.nextInt(); should be numSides = scnr.nextInt();

the class VarDice has wrapped everything in public static void main(String[] args) {...}. Remove this wrapping to ensure that the code is a part of the class and not the method.

With those changes, the code worked for me.

huangapple
  • 本文由 发表于 2023年4月17日 12:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定