Reputation: 3
I'm writing a program that's supposed to simulate craps and after, as far as I know, making no major changes, the program appears to have stopped using the user input that I give it. Any idea why?
UPDATE: the error only happens when the dice roll does not equal 7 or 11.
import java.io.*;
import java.util.*;
public class Craps {
public static void main(String[] args) {
int bet=0;
int availableMoney=1000;
Scanner userInput=new Scanner(System.in);
int diceValue=0;
int die1=0;
int die2=0;
int numberTryingFor=0;
Random diceNumber=new Random();
boolean isItTheBeginningOfTheRound=true;
System.out.println("Welcome to craps!");
System.out.println("You have "+availableMoney+" dollars available.");
while(availableMoney>0){
//sets up round
System.out.println("Please input a bet.");
bet=userInput.nextInt();
die1=1+diceNumber.nextInt(6);
die2=1+diceNumber.nextInt(6);
diceValue=die1+die2;
/*System.out.println(die1);
System.out.println(die2);
System.out.println(diceValue);
System.out.println(availableMoney);*/
if(diceValue==7||diceValue==11&&isItTheBeginningOfTheRound){
availableMoney=availableMoney+bet;
System.out.println("Your roll is "+diceValue);
System.out.println("You win! You now have "+availableMoney+" dollars available.");
}else if(diceValue==2||diceValue==3||diceValue==12&&isItTheBeginningOfTheRound){
availableMoney=availableMoney-bet;
System.out.println("Your roll is "+diceValue);
System.out.println("You lost. You now have "+availableMoney+" dollars available.");
}else{
diceValue = tryForPoint(diceValue, numberTryingFor, diceNumber);
if(diceValue==7&&!isItTheBeginningOfTheRound){
availableMoney=availableMoney-bet;
System.out.println("Your roll is "+diceValue);
System.out.println("Uh Oh! You lost. You now have "+availableMoney+"left.");
}else{
availableMoney=availableMoney+bet;
System.out.println("Your roll is "+diceValue);
System.out.println("You won!You now have "+availableMoney);
}
}
}
System.out.println("You are out of money. You lose.");
}
public static int tryForPoint(int diceValue, int numberTryingFor,
Random diceNumber) {
int die1;
int die2;
while(diceValue!=7||numberTryingFor!=diceValue){
die1=1+diceNumber.nextInt(6);
die2=1+diceNumber.nextInt(6);
diceValue=die1+die2;}
return diceValue;
}
}
Upvotes: 0
Views: 57
Reputation: 608
you never change the value of isItTheBeginningOfTheRound, so the secondary block never executes because
! isItTheBeginningOfTheRound is always false.
Upvotes: 2