gpt4 book ai didi

java - Java中类与类之间传递变量

转载 作者:行者123 更新时间:2023-11-30 06:14:16 27 4
gpt4 key购买 nike

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

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

这是我的类(class):

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

最佳答案

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

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

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

  • 骰子是否会记住它过去掷过的骰子,以便您稍后可以询问它曾经掷过什么? (换句话说,您会掷骰子,用骰子甚至您正在玩的游戏的其他部分执行其他操作,然后回来看看骰子掷出了什么?可能不会)。

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

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

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

或者简而言之,使用以下方法不会实现您的 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 的这两个值不会受到影响。您可以按照与上面类似的方式剖析所有预期的功能:

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

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

  • 是否只有一个通用板,并且游戏不能在不退出游戏并重新启动的情况下使用另一 block 板? (否则所有这些骰子掷值仍然存在)

可能不是,您应该创建一个 Board 实例

  • 更多内容,您可以进一步剖析

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

将现实生活中会发生的所有事情写成简短的句子,然后慢慢将其转换为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);

每当遇到困难时,简化

关于java - Java中类与类之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49574287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com