【问题标题】:Returning a value to another class and storing将值返回到另一个类并存储
【发布时间】:2018-11-14 22:21:52
【问题描述】:

假设给定一个类 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;
}
}

【问题讨论】:

  • 好吧,main 方法中定义的Die 没有存储在哪里,所以方法dieOne(int) 将无法调用die.getValue()。为什么 dieOne 方法接受一个 int 参数而不用它做任何事情?

标签: java parameter-passing


【解决方案1】:

这个任务应该概括: 我用两个公共构造函数编写了 Die 类。如果构造函数没有参数,则骰子的默认大小为六,否则您可以有任意数量的面。 然后,我用两个构造函数编写了 Dices 类。第一个有骰子的数量(有 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 先掷骰子,然后返回值。否则,您可以掷骰子,该值将存储到私有字段值中...

【讨论】:

    【解决方案2】:

    我们将假定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;
      }
    
      ...
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-14
      • 2021-09-16
      • 2019-10-10
      • 1970-01-01
      • 2012-11-19
      • 2021-06-25
      相关资源
      最近更新 更多