Need Help with Dice Program
I have this program I'm writing, but can't figure out the bet part of the program. Instructions are as follows: The player starts with $500. He can bet between 0 and his current amount left on next turn. If he bets 0 the game is over and you should print his remaining amount.He rolls 1 12 sided die. The computer rolls 2 6 sided dice. Add the 2 computer dice together.Compare the numbers. If the player number is bigger he wins his bet. If the computer number is bigger the player loses his bet. If the numbers are the same, but over 7 the player wins his bet and if the numbers are the same, but the numbers are lower than 7 the computer wins the bet. If the number is 7, the bet is a tie and the total will not change.
Code:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;
import java.text.DecimalFormat;
import java.util.*;
public class DiceGame1 extends Frame
{
public static void main(String[] args)
{
//declare variables
int startingAmount;// starting amount
double playerBet;// what the player bets
int computerRoll;//the computer's roll
int computerDie; //computers overall total rolls
int playerRoll;//the player's roll
double aRandomNumber;
double amount;
//Starting Input
startingAmount = 500;
playerBet=0;
amount=0;
System.out.println("Starting Value of $" +startingAmount);
JOptionPane.showInputDialog(null,"Enter Your Bet:");
//Random number's generator for player.
playerRoll = 1+(int)(Math.random()*12);
System.out.println("Your Roll:" +playerRoll);
//Random number's generator for the computer.
computerRoll = 1+(int)(Math.random()*6);
computerDie = computerRoll + computerRoll;
System.out.println("Computer's Roll:" + computerDie );
//Calculation
if(playerRoll > computerRoll)
{
System.out.println("YOU WIN !");
}
else if (computerRoll > playerRoll)
{
System.out.println("YOU LOSE");
}
if((computerRoll == playerRoll) || playerRoll>7)
{
System.out.println("Winner");
}
else if((computerRoll== playerRoll) ||playerRoll <7)
{
System.out.println("Loser");
}
else if((computerRoll == playerRoll) && playerRoll <7)
{
System.out.println("Tie");
}
}}
Re: Need Help with Dice Program
Code Conventions for the Java Programming Language: Contents
Why does DiceGame1 extends Frame when it isn't used as a Frame? And why even consider using Frame and not JFrame?
What value does JOptionPane.showInputDialog(...) return? Where/how do you think you should make use of that returned value?
db
Re: Need Help with Dice Program
DB,
Thanks for your reply! I am extremely new to Java, and have just kinda jumped in. I want the JOPtionPane.showInputDialog to return what the user bets, but I can't figure out how to make it do that. I need it to be able to accept user input. The user should input their bet, and if the user enters 0 for the bet it just prints out remaining amount, and the game is over. If the user enters a bet other than 0 it needs to eventually say if the user won or not, and add or subtract it to their starting amount and print out their new amount they have after that roll
Thanks for any and all help!
Re: Need Help with Dice Program
The instructions ask for 2 different programs, so here is another one that is pretty simple. It compiles with no errors, but I need to figure out where to put the loop and to make it store the amount, after it decreases or increases. Any assistance is GREATLY appreciated!! Can someone please help me??
Code:
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
public class DieGame1
{
public static void main(String[] args) throws IOException
{
//Declare and Construct Variables
int playerRoll, computerRoll;
double playerBet, roll, currentAmount, answer, startingAmount, newAmount;
startingAmount = 500;
//Call Method
playerBet = getDie();
System.out.println("Your starting amount is:" + startingAmount);
//Random number's generator for player.
playerRoll = 1+(int)(Math.random()*12);
System.out.println("Your Roll:" +playerRoll);
//Random number's generator for the computer.
computerRoll = 2+(int)(Math.random()*6);
System.out.println("Computer's Roll:" + computerRoll );
//Calculation
if(playerRoll > computerRoll || playerRoll>7)
{
newAmount = startingAmount += playerBet;
System.out.println("Your new amount is:" + newAmount);
System.out.println("YOU WIN !");
}
else if (computerRoll > playerRoll || playerRoll<7)
{
newAmount = startingAmount -= playerBet;
System.out.println("Your new amount is:"+ newAmount);
System.out.println("YOU LOSE");
}
else if(computerRoll == playerRoll && playerRoll ==7)
{
System.out.println("Tie");
}
}
//The getDie() method asks users to input the bet amount
public static double getDie()
{
//Declare Method Variables
double bet = 0.0;
String answer = JOptionPane.showInputDialog(null, "Enter Bet Amount:");
if (answer == null) finish();
bet = Double.parseDouble(answer);
return bet;
}
//Finish method ends the program
public static void finish()
{
System.exit(0);
}
}