Java Objects类和方法编程有问题

时间:2013-10-02 21:01:56

标签: java class oop object methods

我的节目适合两个玩家。每个玩家每次掷骰子三次,在那里他们可以保持每次滚动的数字。如果他们从第一次滚动中保留一个号码,他们可以继续滚动两次,直到他们选择保留该号码。两个骰子加在一起,总和最高的玩家获胜。我有一个玩家类(负责保持骰子值),掷骰子和确定总和的pairOfdice类以及主游戏。当我运行程序时,我从diceGame类中收到这些错误。我想知道这些错误意味着什么。我的代码尚未完成。

    播放器中的
  • 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;
  }
}

2 个答案:

答案 0 :(得分:2)

您正在尝试使用getter(不带参数)来设置值。自然,Java不知道如何处理这个问题。

  player.getHoldA(dice.getDiceA());

应该是

  player.setHoldA(dice.getDiceA());

答案 1 :(得分:1)

您的方法getHoldAgetHoldB不接受任何参数,但您使用值player.getHoldA(dice.getDiceA());调用它们也许您打算调用setter?