【问题标题】:Sum of two dice rolls in javajava中两个骰子的总和
【发布时间】:2018-04-29 02:08:40
【问题描述】:

所以我有一个到目前为止似乎工作的掷骰子程序,但我不知道的唯一问题是如何对掷骰子的输出求和。它再次掷骰子,同时对前一次掷骰求和。

这是我开始使用的 Die 类

public class Die{   //only thing changed was removing implementation of the interface

private int noFaces;

public Die() {
    noFaces = 6;
}

public int getNoFaces() {       //getter
    return noFaces;
}

public Die(int noFaces) {       //setter, sets new value according to passed parameter
    this.noFaces = noFaces;
}

public int roll() {             //generates random number+1 within specified "noFaces" range
    return (int) (Math.random() * noFaces + 1);
}

public void printRoll() {       //goes to above method roll() and prints the returned value
    System.out.println(roll());
}

}

将一个 Die 变成 PairOfDice,这就是我遇到求和问题的地方

public class PairOfDice {

private Die d1, d2;

public PairOfDice() {       //initializes 2 die objects
    d1 = new Die();
    d2 = new Die();
}

public PairOfDice(int noFaces) {    //sets the dice equally
    d1 = new Die(noFaces);
    d2 = new Die(noFaces);
}   

public PairOfDice(int noFaces1, int noFaces2) { //sets dice separately
    d1 = new Die(noFaces1);
    d2 = new Die(noFaces2);
}

public void printRoll() {   //prints rolls
    System.out.println("Die 1 returned: " + d1.roll());
    System.out.println("Die 2 returned: " + d2.roll());

}

public void printRollSum(){     //print sum of both rolls
    System.out.println("Sum of both dice rolls are: " + (d1.roll() + d2.roll()));
}

}

我在哪里得到输出,RollingDice

public class RollingDice {

public static void main(String[] args) {

    PairOfDice pairOne = new PairOfDice();
    pairOne.printRollSum();
    System.out.println();

    PairOfDice pairTwo = new PairOfDice(10);
    pairTwo.printRoll();
    pairTwo.printRollSum();
    System.out.println();

    PairOfDice pairThree = new PairOfDice(100, 3);
    pairThree.printRoll();
    pairThree.printRollSum();

}

}

感谢对我的程序或 cmets 的所有帮助,我仍在学习。谢谢!

【问题讨论】:

  • 所以您希望printRoll()printRollSum() 使用相同的数字,这是问题所在吗?如果是这样,您需要存储卷 b/c 的结果,每卷都会生成一个新的随机数。
  • 每次调用roll()都会生成一个新号码。

标签: java dice


【解决方案1】:

你得到两个骰子的总和,但你每次都掷骰子。 printRoll() 滚动并显示值,然后printRollSum() 再次滚动它们,为您提供不同的值。

【讨论】:

    【解决方案2】:

    作为一种解决方案,您可以将最后滚动的值存储在您的 Die 类中(类似于 noFaces,但例如将其称为 lastValue),然后将 printRollSum() 更改为使用 getLastValue()roll()

    【讨论】:

    • 谢谢,我加了一个'public int getPrintRoll(){ int i = roll(); System.out.println(i);返回我; }' 到我的 Die.java 类
    【解决方案3】:

    roll() 方法在每次调用时都会生成一个新的随机数,因此当您调用 printRoll()printRollSum() 时,您每次都会收到新的随机数。

    您需要将roll 的结果存储在您的一个类中,然后提供访问这些结果的方法。

    【讨论】:

      【解决方案4】:

      每次调用printRollprintRollSum 时都会生成新的数字。您应该将这两种方法结合起来,或者至少将掷骰的当前值存储在骰子中,并通过printRollSum访问它们

      【讨论】:

        【解决方案5】:

        您的每个 printRoll() 和 printRollSum() 调用都是重新掷骰子。如果您要掷骰子,打印每个骰子的面值,然后打印这两个面值的总和,您需要存储掷骰结果,然后对这些结果执行总和计算。例如:

        public class PairOfDice {
        
        private Die d1, d2;
        int lastRollD1;
        int lastRollD2;
        
        public PairOfDice() {       //initializes 2 die objects
            d1 = new Die();
            d2 = new Die();
        }
        
        public PairOfDice(int noFaces) {    //sets the dice equally
            d1 = new Die(noFaces);
            d2 = new Die(noFaces);
        }   
        
        public PairOfDice(int noFaces1, int noFaces2) { //sets dice separately
            d1 = new Die(noFaces1);
            d2 = new Die(noFaces2);
        }
        
        public void roll()
        {
            lastRollD1 = d1.roll();
            lastRollD2 = d2.roll();
        }
        
        public void printRoll() {   //prints rolls
            System.out.println("Die 1 returned: " + lastRollD1);
            System.out.println("Die 2 returned: " + lastRollD1);
        
        }
        
        public void printRollSum(){     //print sum of both rolls
            System.out.println("Sum of both dice rolls are: " + (lastRollD1 + lastRollD1));
        }
        
        }
        

        在这个版本中,你的 RollingDice 会调用:

        pairOne.roll();
        pairOne.printRoll();
        pairOne.printRollSum();
        

        【讨论】:

          猜你喜欢
          • 2018-01-16
          • 2021-07-14
          • 1970-01-01
          • 2020-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-03
          • 1970-01-01
          相关资源
          最近更新 更多