繁体   English   中英

将值返回给另一个类并存储

[英]Returning a value to another class and storing

假设给定一个类 Die,它包含一个六面骰子的随机值。

另一个类PairOfDice需要访问 Die 中的getvalue并存储两个 die 值。

执行 PairOfDice 时出现错误:找不到符号。

如何解决这个问题? 对java代码还有其他建议吗?

public class Die {
public static Random rand = new Random();
private int sides; // Number of sides
private int value; // Die's value
public Die() {
    sides = 6;
    roll();
}
public void roll() {
    value = rand.nextInt(sides) + 1;
}
public int getSides() {
    return sides;
}
public int getValue() {
    return value;
}

给出的第二类是:

public class PairOfDice {
private int dieOne;
private int dieTwo;
public void main(String[] args){
    Die die;
    die = new Die();
}
private void dieOne(int value){
    dieOne = die.getValue();
}
private void dieTwo(int value){
    dieTwo = die.getValue();
}
public int getDieOneValue(){
    return dieOne;
}
public int getDieTwoValue(){
    return dieTwo;
}
}

这个任务应该被概括:我用两个公共构造函数编写了 Die 类。 如果构造函数不带参数,则骰子的默认大小为 6,否则您可以有任意数量的边。 然后,我用两个构造函数编写了 Dice 类。 第一个有骰子的数量(有 6 个面),第二个有带有首选面的骰子列表。 如果您想学习如何概括问题(任何问题),您可以查看我的代码。 (当然,它可以更高效、更优雅地完成,但这里是简单的代码):

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Die {
    private Random RAND = new Random();
    private int noOfSides;
    private int value;

    // Default constructor without the parameter of sides gives the six sized die
    public Die() {
        this.noOfSides = 6;
    }

    // The constructor WITH number of sides
    public Die(int noOfSides) {
        this.noOfSides = noOfSides;
    }

    // rolling the die
    public void roll() {
        this.value = RAND.nextInt(noOfSides) + 1;
    }

    public int getValue() {
        if (value == 0) roll(); // if the die is never rolled -> roll it!
        // else return the rolled value
        return value;
    }

    // just for curiosities
    public int getNoOfSides() {
        return noOfSides;
    }

    public String toString() {
        return "Die has the " + noOfSides + " sides, and the last roll value was " + getValue();
    }
}

class Dices {

    private int noOfDices;
    private List<Die> myDices = new ArrayList<Die>();

    // NO constructor without the number of dices
    private Dices() {
    }

    public Dices(int noOfDices) {
        this.noOfDices = noOfDices;

        // example is for 6 sided dices
        for (int i = 0; i < noOfDices; i++) {
            getMyDices().add(new Die());
        }
    }

    // example with the list of dices with predefined sizes
    public Dices(List<Die> myDices){
        this.myDices = myDices;
    }

    public List<Die> getMyDices() {
        return myDices;
    }

    public String toString() {
        String s = "";
        for (Die die : getMyDices()) {
            s = s + die + "\n";
        }
        return s;
    }
}

public class Answer {

    public static void main(String[] args) {

        //test with two dices (6 size):
        Dices twoDices = new Dices(2);
        System.out.println(twoDices);

        //test with 4 dices size 3, 7, 9, 22
        Dices fourDices = new Dices
                (List.of(new Die(3),
                        new Die(7),
                        new Die(9),
                        new Die(22)));
        System.out.println(fourDices);
    }
}

可以看到,如果骰子从未掷出,则getValue首先掷出骰子,然后返回值。 否则,您可以掷骰子,该值将存储到私有字段值中...

我们将假定Die类按预期工作。

因此,人们可以考虑对PairOfDice进行的这些更改可能会解决当前的问题。

public class PairOfDice {
  private Die dieOne = new Die();
  private Die dieTwo = new Die();

  public static void main(String[] args) {
    PairOfDice pair = new PairOfDice();
    System.out.printf("Pair: %d - %d%n", pair.getDieOneValue(), pair.getDieTwoValue());
  }

  public int getDieOneValue() {
     dieOne.roll();
    return dieOne.getValue();
  }

  public int getDieTwoValue() {
    dieTwo.roll();
    return dieTwo.getValue();
  }
}

PairOfDice类应该保存对两个骰子的引用(在private Die实例变量中)。

getDieXValue()方法使用实例变量,并在生成roll()后返回一个值。

现在,问题是要求是存储两个骰子的values ,还是只是访问获取值的能力。 如果确实需要存储值,那么可以这样做:

public class PairOfDice {
  private int dieOneValue;
  private int dieTwoValue;

  public PairOfDice {
    Die die = new Die();

    // get a value for the first die
    die.roll();
    dieOneValue = die.getValue();

    // get a value for the 2nd die
    die.roll();
    dieTwoValue = die.getValue();
  }

  public int getDieOneValue() {
    return dieOneValue;
  }

  ...

就个人而言,如果要创建对象,则存储和使用这些对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM