【问题标题】:Transferring variables from class to class in Java在Java中将变量从类传递到类
【发布时间】:2018-09-09 11:41:12
【问题描述】:

在完成大富翁类型的基于文本的游戏时,我在 Java 中将变量数据从类传输到类时遇到了一些问题。

我一直在尝试将 roll1 和 roll2 变量从 Dice 类传输到 Board 类,但由于某种原因,数据没有正确传输,当我从 2 Rollin Board 类返回组合数据时,它只是返回为 0。

这是我的课程:

public class Dice {

    static int dots, roll1, roll2, flag;
    Random number = new Random();

    public Dice(){
         dots = number.nextInt(6)+1 ;
    }

    public void roll1(){
        roll1 = number.nextInt(dots)+1;
    }

    public void roll2(){
        roll2 = number.nextInt(dots)+1;
    }

    public int getDots1(){
        return roll1;
    }

    public int getDots2(){
        return roll2;
    }

    public String getSame(){
        if(roll1 == roll2){
            flag++;
            return("Your rolls were the same");
        }
        else{
            return(" ");
        }

    }
}

public class Board {
    static int roll1 = Dice.roll1;
    static int roll2 = Dice.roll2;
    public static int i;
    public int Turn = 0;
    public int totalP = 0;
    public static int Pos = 0;
    public static int[] Players= {1, 2, 3, 4};
    public static int[] Square = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
    public static int Money = 40000;
    static ArrayList<Integer> pPos = new ArrayList<Integer>();
    void pPosList(){}

    public Board(int totalP){
        this.totalP = totalP;
    }

    public static int getPos(){
           while(Money != 0){
           for(i=0;i<Players.length;i++){ 
               System.out.println("Player Turn: Player "+(i));
               Pos = Square[roll1 + roll2];
               pPos.set(0, Pos);
               return roll1;
           }
        }
    }
}

public class Blobfish {

    public void main(String[] args) {
        System.out.println(" " +Board.getPos());
        Dice dice = new Dice();
        dice.roll1();
        dice.roll2();
        System.out.println("First Roll: " +dice.getDots1());
        System.out.println("Second Roll: " +dice.getDots2());
        System.out.println("" + dice.getSame());
        System.out.println("Your Current Position = " +Board.getPos());
    } 
}

【问题讨论】:

  • 首先这些不应该是静态的。您有一个 Board 实例,并且有一对 Dice。你为什么要让这些静态的?将它们与实例相关联。您不希望世界上所有的棋盘或世界上的所有骰子都具有相同的值吗?
  • 如何将值从一个类传递到另一个类?
  • 您在代码中的哪个位置传递值?无处 ;) 板中的 roll1 与骰子中的 roll1 不同(即使它们是静态的并且它们被称为相同 - 它们与您声明它们的类相关)。您应该执行类似 board.roll1=dice.roll1 的操作。卷后。这个静态 int roll1 = Dice.roll1;不会终生连接它们
  • 变量是静态的,因为每当我将它们设为非静态时,主函数将无法正常运行。
  • 那么我到底需要做什么才能让Dice中的roll1和roll2转移到Board中的roll1和roll2,这样我就可以计算出用户的当前位置了?

标签: java class variables inheritance


【解决方案1】:

您的代码的问题是您掷骰子,但随后没有更新 Board 类中的骰子值

这可能会解决您的问题,但不能解决您的布局问题。

public class Blobfish {

    public void main(String[] args) {
        System.out.println(" " +Board.getPos());
        Dice dice = new Dice();
        dice.roll1();
        dice.roll2();
        Board.roll1 = dice.getDots1();
        Board.roll2 = dice.getDots2();
        System.out.println("First Roll: " + dice.getDots1());
        System.out.println("Second Roll: " + dice.getDots2());
        System.out.println(dice.getSame());
        System.out.println("Your Current Position = " + Board.getPos());
    } 
}

您的代码布局不佳。我的意思是

  • 您应该声明一个 Board 实例,而不是使用静态变量。即Board b = new Board(0);
  • 并非所有内容都应该是静态的,因为变量通常应该通过类的实例来访问,除非您使用的是单例
  • 并非所有内容都应该公开。相反,应该使用私有变量,然后通过Getters and Setters 进行操作(换句话说,您想查看encapsulation
  • 不需要System.out.println(...) 中的" " + someInt

实现我刚才谈到的大部分内容的示例类可能如下所示。

public class Test {
    //Private int that can be manipulated via the use of getters and setters.
    private int someInt;

    private Test() {
        someInt = 0;
    }

    //Setter
    public void setSomeInt(int valToSet) {
        someInt = valToSet;
    }

    //Getter
    public int getSomeInt() {
        return someInt;
    }

    public static void main(String[] args) {
        //Instance of Test
        Test t = new Test();

        //Updates the value of the instance
        t.setSomeInt(10);

        //Prints the value from that same instance
        System.out.println(t.getSomeInt());
    }
}

【讨论】:

  • 所以如果我这样做并创建一个 Board 类的新实例以使所有内容都静态化,我需要在构造函数中定义什么?
  • 如果你想要的话,你可以有一个默认的构造函数。这取决于您在实例化 Board 类时要实现的目标
【解决方案2】:

我认为您的课程需要稍微重新设计。 Board 班级做得太多了。以下是您可以考虑的粗略设计:

// Represents a single die
Dice
    int roll()  // Returns 1-6

// A single player. Maintains its own position on board
Player
    int getPosition()
    void move(int numSpaces)
    int bankAccount = 200

// This will contain all the spaces and whatever actions
// happen on those spaces
Board
    ?? getAction(int spaceNumber)

Bank
    payAmount(int amount)
    receiveAmout(int amount)

// The manager class. Maintains the board and players and bank
Game
    Dice dice1, dice2
    Player players[4]
    Board board
    Bank bank
    void nextTurn()
        int roll1, roll2;
        do {
            roll1 = dice1.roll()
            roll2 = dice2.roll()
            players[currentPlayer].move(roll1 + roll2)
            board.getAction(players[currentPlayer].getPosition()]
        } while roll1 == roll2
        currentPlayer = (currentPlayer + 1) % 4

没有static 变量。 Game 类管理所有其他类之间的交互。

【讨论】:

    【解决方案3】:

    缩小错误范围的一种方法是查看类的结构并问问自己是如何定义它们的(因为它们可能比实际需要的更复杂),并确保你已经简化了缩小的每个要求:

    • “骰子”的掷骰次数是否有限?

    通常一个游戏会决定你掷多少次骰子,以及一次需要多少个骰子,所以一个 roll() 函数就足够了,骰子的数量由使用的类决定它

    • Dice 是否记得它在过去进行的一次掷骰,以便您以后可以问它曾经掷过什么? (换句话说,你会掷骰子,用骰子执行一些其他动作,甚至使用你正在玩的游戏的其他部分,然后回来看看掷出的骰子是什么?可能不会)。

    骰子提供的值立即被游戏使用,并被游戏中的某些动作存储或使用,而不是骰子本身

    • 如果您创建 100 个 Dice 实例,每个实例是否都有自己的 Roll 值,或者您可以掷 dice24 并期望在 dice81 上显示相同的值?

    关于在全局/静态变量中存储成员值的提示

    或者简而言之,使用以下方法不会实现你的 Dice 类旨在提供的东西吗?

    public getRoll() {
        return number.nextInt(6) + 1;
    }
    public boolean compareValues(int roll1, int roll2) {
        return roll1 == roll2;
    }
    

    额外问题:什么是骰子旗?

    接下来让我们看一下 Board 类:您要做的第一件事是将值或 Dice.roll1 和 Dice.roll2 存储在属于 Board 的变量中,因为还没有发生任何其他事情,所以它们为 0。你现在可以对 Dice 类做任何你想做的事情,这两个属于 Board 的值不会受到影响。您可以以与上述类似的方式剖析所有预期功能:

    • 一个棋盘实例是否只能掷 2 次骰子?

    可能不会,所以不要使用静态变量

    • 是不是只有一个万能板,不退出游戏重新启动就不能使用另一个? (否则所有这些骰子掷骰值仍然存在)

    可能不会,你应该创建一个 Board 的实例

    • 更多的东西,你可以进一步剖析

    最后是 Blobfish... 看起来您指的是通用棋盘,但创建了一个与棋盘无关的 Dice 实例,滚动它们并询问棋盘玩家现在在哪里,这仍然是他们开始的地方因为你使用的是不同的骰子。

    把现实生活中会发生的一切写成短句,然后慢慢把它变成Java,即游戏开始:每个玩家掷两个骰子,如果掷出的骰子相同,说“耶”,然后移动玩家滚动的组合距离

    变成:

    // create the game, and a Dice for it
    Board myGame = new Board();
    Dice dice = new Dice();
    int player = 0;
    
    // roll the dice twice
    int roll1 = dice.roll();
    int roll2 = dice.roll();
    
    // celebrate if the rolls double up
    if (roll1 == roll2) {
        System.out.println("yay");
    }
    int distanceToMove = roll1 + roll2;
    
    // move the player
    myGame.movePlayer(player, distanceToMove);
    

    当你遇到困难时,简化

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多