- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习 OOP,从 Java 开始,因为我读过并被告知这是最好的起点。话虽如此,我正在尝试创建一个有趣的游戏来帮助我的学习,但我也知道游戏编程和设计可能更具挑战性。
所以我的目标是从 RollDice D20 生成新值,而无需重置或重新启动程序。您会注意到,当我打印出值时,我打印了同一个实例两次以演示我要避免的内容,并打印一个新实例来表明新实例确实生成了新值。也许,我没有以正确的方式处理这个问题,但这是我希望在一些帮助下克服的障碍!
我最终想要的是弄清楚如何生成一个新实例或至少生成一个新的滚动值,次数不限。非常感谢任何和所有的帮助!我添加了下面的代码作为示例。也欢迎任何其他反馈。
import java.util.Random;
class RollDice
{// Begin RollDice Class
// Initiate method rollDice
public static int rollDice(int number, int nSides)
{
// System.out.println( "--- Welcome to the Dice Game v2! ---" ); //
// welcomes player
Random r = new Random();
// Declare class variables
int num = 0;
int roll = 0;
if (nSides >= 3)
{
for (int i = 0; i < number; i++)
{
roll = r.nextInt(nSides) + 1;
// System.out.println("Roll is: " + roll);
num = num + roll;
}
}
else
{
System.out.println("Error num needs to be from 3");
}
return num;
} // end method rollDice
int d4 = rollDice(1, 4);
int d6 = rollDice(1, 6);
int d8 = rollDice(1, 8);
int d10 = rollDice(1, 10);
int d12 = rollDice(1, 12);
int d20 = rollDice(1, 20);
public RollDice()
{
this.d4 = d4;
}
public void setD4(int D4)
{
this.d4 = D4;
}
public int getD4()
{
return d4;
}
// ////////////////
{
this.d6 = d6;
}
public void setD6(int D6)
{
this.d6 = D6;
}
public int getD6()
{
return d6;
}
// ////////////////
{
this.d8 = d8;
}
public void setD8(int D8)
{
this.d8 = D8;
}
public int getD8()
{
return d8;
}
// ////////////////
{
this.d10 = d10;
}
public void setD10(int D10)
{
this.d10 = D10;
}
public int getD10()
{
return d10;
}
// ////////////////
{
this.d12 = d12;
}
public void setD12(int D12)
{
this.d12 = D12;
}
public int getD12()
{
return d12;
}
// ////////////////
{
this.d20 = d20;
}
public void setD20(int D20)
{
this.d20 = D20;
}
public int getD20()
{
return d20;
}
// ////////////////
}// End RollDice Class
class Champion
{// Begin Champion Class
RollDice champDice = new RollDice();
int champroll = champDice.getD20();
public Champion()
{
this.champroll = champroll;
}
public void setChampRoll(int ChampRoll)
{
this.champroll = ChampRoll;
}
public int getChampRoll()
{
return champroll;
}
}// End Champion Class
public class DiceRollTest
{
public static void main(String ars[])
{
Champion tChampion = new Champion();
Champion pChampion = new Champion();
System.out.println("Your Champion defends with a " + tChampion.getChampRoll() + "\n");
System.out.println("Your Champion defends with a " + tChampion.getChampRoll() + "\n");
System.out.println("Your Champion defends with a " + pChampion.getChampRoll() + "\n");
}
}
最佳答案
您的 RollDice 类没有实现您想要的效果。它所做的只是为您拥有的每种类型的骰子存储一个骰子掷骰结果。因此,当您对 Champion 对象调用 getChampRoll() 时,它所做的只是返回在构造 RollDice 类时已经发生的掷骰。
相反,您应该使 rollDice() 成为 RollDice 类的成员函数。然后,RollDice 应该在其构造函数中接受一个参数,指示调用 rollDice() 时应掷哪个骰子。这样,当您调用 getD20() 时,它将滚动 D20 并给出结果。
我将以此作为起点:
import java.util.Random;
public class Die {
private int mSides;
private Random mRandom;
public Die(int sides) {
this.mSides = sides;
mRandom = new Random(System.currentTimeMillis());
}
public int roll() {
return mRandom.nextInt(mSides + 1);
}
public int roll(int times) {
int sum = 0;
for (int i = 0; i < times; i++) {
sum += roll();
}
return sum;
}
}
您想要创建的每个骰子都可以继承/子类化该类。然后你可以把其中一些扔进你冠军的口袋里:)
关于Java 骰子游戏生成新数字时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043611/
我刚刚向自己介绍了 WebGL 和 Threejs,作为开始,我正在尝试创建一个 3D 骰子。我已经到了创建立方体的地步,但我真的不知道如何在立方体的面上放置数字,我能找到的只是如何更改颜色。我已经查
这就是我现在得到的...... var max = 7; var min = 1; $('#dice').click(function() { random = Math.floor(Math.r
我是 Unity 的新手,一直在尝试掷骰子。我遇到了一组教程,它们允许我创建一个 3d 模具(模具使用 Rigidbody 和 Mesh Collider)并编写脚本使其在空格键上滚动,如下所示: 骰
我是 JavaScript 新手,我需要一些想法/帮助来了解如何使我的脚本正常工作。所以我们的想法是,你掷两个骰子,通过按下“掷骰子”按钮然后按下重置按钮来重置整个 HTML 中的所有内容。 所以事情
我正在尝试构建一个掷骰子游戏,如果计算机自动掷出一对骰子,并且如果 cpu 掷出 7 或 11,则用户获胜。然而,如果用户掷出 2、3 或 12,他们将自动失败。如果用户滚动任何其他数字(4、5、6、
背景 如此处所述http://www.ericharshbarger.org/dice/#gofirst_4d12 , “先走”骰子是一组四个骰子,每个都有唯一的编号,因此: 任何两个或更多骰子都不会
我是一名优秀的程序员,十分优秀!