简体   繁体   English

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

[英]Returning a value to another class and storing

Suppose given a class Die and it contains a random value for a six sided die.假设给定一个类 Die,它包含一个六面骰子的随机值。

Another class PairOfDice needs to access getvalue in Die and store two die values.另一个类PairOfDice需要访问 Die 中的getvalue并存储两个 die 值。

An error: cannot find symbol occurs when PairOfDice is executed.执行 PairOfDice 时出现错误:找不到符号。

How can this problem be fixed?如何解决这个问题? And are there any other suggestions for the java code?对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;
}

The second class given is:给出的第二类是:

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

This quest should be generalized: I wrote the Die class with two public constructors.这个任务应该被概括:我用两个公共构造函数编写了 Die 类。 If the constructor is without the parameter, default size of die is six, else you can have any number of sides.如果构造函数不带参数,则骰子的默认大小为 6,否则您可以有任意数量的边。 Then, I wrote the Dices class with two constructors.然后,我用两个构造函数编写了 Dice 类。 First one have the number of dices (with 6 sides), and second one have the List of dices with preferred sides.第一个有骰子的数量(有 6 个面),第二个有带有首选面的骰子列表。 If you want to learn how to generalize the problem (any problem) you can check my code.如果您想学习如何概括问题(任何问题),您可以查看我的代码。 (Of course, it can be done more efficiently and with more elegance, but here is the simple code): (当然,它可以更高效、更优雅地完成,但这里是简单的代码):

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

You can see, if the die is never rolled, getValue first roll the die, then return the value.可以看到,如果骰子从未掷出,则getValue首先掷出骰子,然后返回值。 Otherwise you can roll the die and the value will be stored into the private field value...否则,您可以掷骰子,该值将存储到私有字段值中...

We will presume that the Die class works as expected.我们将假定Die类按预期工作。

So, one could think of these changes to PairOfDice that likely resolve the immediate issue.因此,人们可以考虑对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();
  }
}

The PairOfDice class should hold references to two dice (in the private Die instance variables). PairOfDice类应该保存对两个骰子的引用(在private Die实例变量中)。

The getDieXValue() methods work with the instance variables, and return a value, after generating a roll() . getDieXValue()方法使用实例变量,并在生成roll()后返回一个值。

Now, the question is whether the requirement is to store the values of two dice, or just access to the ability to get the values.现在,问题是要求是存储两个骰子的values ,还是只是访问获取值的能力。 If truly the need is to store the values, then one could do:如果确实需要存储值,那么可以这样做:

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

  ...

Personally, if one is going to create objects, then store and use the objects.就个人而言,如果要创建对象,则存储和使用这些对象。

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

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