我必须做一个基本的掷骰子计划而且我迷路了,到目前为止我已经花了10个小时。我有麻烦连接方法,或“把各个部分放在一起”可以这么说。感谢您抽出时间查看我的代码。
该计划的目标:用户输入开始余额,然后下注。然后玩游戏。如果用户获胜,则将赌注添加到余额中,如果从平衡中减去赌注。程序会提示用户是否要再次播放。如果是'y',则玩游戏。如果'n',程序退出。总游戏,胜利,亏损和必须在会话结束时打印当前帐户余额。
我已经完成了Play()方法和NewGame()方法,所以我可以一遍又一遍地玩。但是如何让我的printreport(),accountinfo(),update()方法相互协作?
这是我到目前为止,任何建议表示赞赏。
public class Craps {
double balance, startBalance, bet;
int wins, loss, games;
boolean win;
boolean eligibletoPlay;
public enum Status { CONTINUE, WON, LOST };
final static int SNAKE_EYES = 2;
final static int TREY = 3;
final static int SEVEN = 7;
final static int ELEVEN = 11;
final static int BOX_CARS = 12;
final static double MIN_BALANCE = 50.00;
final static double MIN_BET = 1.00;
Scanner input;
public static void main ()
{
Craps game = new Craps();
game.NewGame();
}
public void NewGame()
{
String answer;
input = new Scanner(System.in);
while (true)
{
System.out.print("Do you wish to play a new game? ");
answer = input.next();
if (answer.toLowerCase().charAt(0) == 'y')
{
play();
}
else if (answer.toLowerCase().charAt(0) == 'n')
{
printreport();
System.exit(5);
}
else
System.out.println("Please type in either 'y' to play again or 'n' to quit");
}
}
public boolean play ()
{
int point = 0;
int sumOfDice = roll();
Status gameStatus;
switch (sumOfDice)
{
case SEVEN:
case ELEVEN:
gameStatus = Status.WON;
System.out.printf("Player rolls: %d\n", sumOfDice);
break;
case SNAKE_EYES:
case TREY:
case BOX_CARS:
gameStatus = Status.LOST;
System.out.printf("Player rolls: %d\n", sumOfDice);
break;
default:
gameStatus = Status.CONTINUE;
point = sumOfDice;
System.out.printf("Player rolls: %d\n", point);
}
while ( gameStatus == Status.CONTINUE )
{
sumOfDice = roll();
System.out.printf("Player rolls: %d\n", sumOfDice);
if (sumOfDice == point)
{
gameStatus = Status.WON;
}
else if (sumOfDice == SEVEN)
{
gameStatus = Status.LOST;
}
}
if ( gameStatus == Status.WON )
{
System.out.printf("@@@@@@@@@Congratulations, You win@@@@@@@@@\n\n", sumOfDice);
}
else
System.out.printf("###########Sorry, you've lost###########\n\n", sumOfDice, point);
NewGame();
return something... no idea what..
}
public void accountinfo()
{
String response;
JOptionPane.showMessageDialog(null, "Hello, and Welcome to the Craps Game;");
while (true)
{
response = JOptionPane.showInputDialog(null, "Enter your starting balance");
try
{
startBalance = Double.parseDouble(response);
}
catch (NullPointerException e)
{
JOptionPane.showMessageDialog(null, "Fine, don't play.");
System.exit(0);
}
catch (NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "You must enter in a numeric value");
continue;
}
if (startBalance >= MIN_BALANCE)
break;
JOptionPane.showMessageDialog(null,"Please enter in a balance of 50.00 or greater");
}
balance = startBalance;
wins = 0;
loss = 0;
games = 0;
play();
}
public boolean update ()
{
games++;
if (win)
{
wins++;
balance = balance - bet;
}
else
{
loss++;
balance = balance - bet;
}
System.out.printf("Your total account balance is %4.2f", balance);
if (balance < MIN_BET)
{
eligibletoPlay = false;
System.out.println("You don't have enough money and can no longer play");
}
}
public void printreport()
{
if (games > 0)
{
double changed = balance - startBalance;
double percentchanged = (changed/startBalance) * 100;
double percentwins = (wins/(double)games) * 100;
double percentlosses = (loss/(double)games) * 100;
System.out.printf("Of the %d games you have played you have won %d (%4.2f) , lost %d (%4.2f)", games, wins, percentwins, loss, percentlosses);
if (changed < 0 )
{
System.out.printf("Your balanced has decreased by %4.2f (%-4.2f)\n", changed, percentchanged);
System.out.printf("You have no more money left, bye\n");
}
else if (changed > 0)
{
System.out.printf("Your balanced has increased by %4.2f (%4.2f)\n", changed, percentchanged);
System.out.printf("Bye, enjoy your winnings");
}
}
}
public static int roll ()
{
int dice1;
int dice2;
dice1 = 1 + (int)(6.0*Math.random());
dice2 = 1 + (int)(6.0*Math.random());
return (dice1 + dice2);
}