![]() |
[java] program help, possible instantiation problem.
Hey everyone, I am in need of some help with this program. It seemed like a simple program but I cannot get it to start. The problem comes up when I try to instatiate dice1 and dice2. It says it cannot start new GameDice() without an int. Even if I give it an int in the GameDice class, the dice1.getFace or the dice2.getFace does not do the job. Im sorry if this is not the proper way to ask for help but anything would be great. Thanks in advance. Here is the program./**
* This is a simple class the models dice * * @author (Danny Leal) * @version (2-18-04) */ public class GameDice { private int dieNumber = 1; private int diceRoll; public GameDice(int dice) { dieNumber = dice; } public void roll() { diceRoll = (int) (Math.random() * 6) + 1; //this will generate a random number between 1 and 6. } public int getFace() { return diceRoll; } } ///////////////THIS IS THE GAME PART////////////////////////// public class Game { public static void main(String args[]) { String playAgain = "y"; JOptionPane.showMessageDialog(null,"You will enter an amount of money to start with." + "You will the roll the dice, if the dice roll as a pair" + "you will win the amount of the dice. If the dice are not a pair" + "then your bank will be subracted the amount of the smaller dice"); GameDice dice1 = new GameDice(); GameDice dice2 = new GameDice(); String input = JOptionPane.showInputDialog(null,"Please enter the amount you want to play with in whole amounts only"); int userPurse = Integer.parseInt(input); do { userPurse = userPurse - 1; if (dice1.getFace() == dice2.getFace()) { JOptionPane.showMessageDialog(null,"Congradulations, the dice both show " + dice1.getFace() + " and you will be awarded $" + dice1.getFace() + " to your purse"); userPurse = dice1.getFace() + userPurse; } else { if (dice1.getFace() > dice2.getFace()) { JOptionPane.showMessageDialog(null,"Im sorry, you rolled a" + dice1.getFace() + " and a" + dice2.getFace() + ", your purse will have" + dice2.getFace() + "subtracted from your purse"); userPurse = userPurse - dice2.getFace(); } else { JOptionPane.showMessageDialog(null,"Im sorry, you rolled a" + dice1.getFace() + " and a" + dice2.getFace() + ", your purse will have" + dice1.getFace() + "subtracted from your purse"); userPurse = userPurse - dice1.getFace(); } } if (userPurse < 0) { playAgain = "n"; JOptionPane.showMessageDialog(null,"Im sorry, you have run out of money and are unable to play again."); } else { do { playAgain = JOptionPane.showInputDialog(null,"Would you like to play again? (Y)es or (N)o"); } while(!(playAgain.equalsIgnoreCase("y")||playAgain.equalsIgnoreCase("n"))); } } while(!(playAgain.equalsIgnoreCase("n"))); JOptionPane.showMessageDialog(null,"Thank you for playing"); } } |
There is only one constructor defined for class GameDice, specifically one that takes an integer parameter. You can either make a new constructor that takes no parameters or change the statements
GameDice dice1 = new GameDice(); GameDice dice2 = new GameDice(); to pass an int. |
ok, Im gonna give changing the parameters a try. I appreciate it
|
ok, now I have a new problem, I went ahead and took out the parameters. It compiled great, but now dice1.getFace(); and dice2.getFace(); both seem to return 0. I have also tried passing an int through GameDice dice1 = new GameDice(1); and it still comes up with 0 when I try to use the getFace method.
|
You have to call GameDice.roll first or GameDice.getFace will always return the default value for an integer (0).
|
wow, thank you so much. I dont know how I could repay you, I guess if you ever need help writing a really bad program just look me up! hahaha! thanks again!
|
All times are GMT -8. The time now is 03:52 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project