我的程序是为两个玩家设计的。每个玩家每次掷骰子 3 次,他们可以保留每次掷骰的数字。如果他们保留第一次滚动的一个数字,他们可以继续再滚动两次,直到他们选择保留该数字。两个骰子相加,总和最高的玩家获胜。我有一个玩家类(负责保持骰子值),骰子被滚动并确定总和的 pairOfdice 类,以及主游戏。当我运行程序时,我从 diceGame 类收到这些错误。我想知道这些错误是什么意思。我的代码还没有完成。
- playerGame 中的 getHoldA() 不能应用于 (int)
- playerGame 中的 getHoldB() 不能应用于 (int)
主游戏
import java.io.*;
import java.lang.*;
import java.util.*;
public class diceGame
{
public static void main(String[] args) {
pairOfDice dice;
dice = new pairOfDice();
playerGame player;
player = new playerGame();
int rollCount = 0;
int holdA = 0, holdB = 0;
do {
dice.roll(); // Roll the first pair of dice.
System.out.println("Dice 1: " + dice.getDiceA() + "\n" + "Dice 2: " + dice.getDiceB() + "\n" + "The total is: " + dice.getTotal());
System.out.println("Do you want to hold the fist dice with a value of " + dice.getDiceA());
if (holdA == 1){
player.getHoldA();
player.getHoldA(dice.getDiceA());
System.out.println("Value of dice A is held");
}
System.out.println("Do you want to hold the second dice with a value of " + dice.getDiceB());
if (holdB == 1){
player.getHoldB(dice.getDiceB());
System.out.println("Value of dice B is held");
}
rollCount++;
}
while (dice.getTurns() <= 3);
}
}
玩家等级
import java.io.*;
public class playerGame
{
private int holdA = 0, holdB = 0;
//constructor
public playerGame(){
}
public void setHoldA (int valA){
holdA = valA;
}
public void setHoldB (int valB){
holdB = valB;
}
public void setPlayer(int player){
player = player + 1;
}
public int getHoldA () {
return holdA;
}
public int getHoldB () {
return holdB;
}
}
pairOfdice 类
import java.io.*;
public class pairOfDice
{
private int diceA = 0, diceB = 0, turns = 0;
public pairOfDice() {//constructor
roll();
}
public void roll(){
diceA = (int)(Math.random()*6) + 1;
diceB = (int)(Math.random()*6) + 1;
turns = turns +1;
}
public void setDice(int newDiceA, int diceA) {
diceA = newDiceA;
}
public void setDiceB(int newDiceB, int diceB) {
diceB = newDiceB;
}
public void setTurns(int turns) {
turns = 0;
}
public int getDiceA() {
return diceA;
}
public int getDiceB() {
return diceB;
}
public int getTotal() {
return (diceA + diceB);
}
public int getTurns() {
return turns;
}
}