Java Error on token "}" expected "{" - java

So I'm in the midst of creating a gambling application and just as I'm about to close up for the night. I see there is a new error all the way at the bottom with my closing brackets.
I'm getting the following error Syntax on error "{", "}" expected.
I think there might be an issue with one of my methods, but all of the brackets seem to be placed correctly. I apologize that it's 200+ lines of code, but if someone could possibly point out where I went wrong to get this error, I'd really appreciate it. I've tried messing with my brackets, but that only seems to lead to further errors.
import java.util.Scanner;
import java.util.Random;
public class Project2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int min = 1;
int max = 10;
int colmax = 2;
double balance = 2500.0;
double bet1 = 0;
double bet2 = 0;
double profit1 = (bet1 * 2) - bet1;
double profit2 = (bet2 * 5) - bet2;
String kBet = null;
System.out.println(" -==-==-==-==-==-==-==-==-==-==-" );
System.out.println("-==-==-=={ Welcome to the Marist Casino! }==-==-==-");
System.out.println(" -==-==-==-==-==-==-==-==-==-==-" );
System.out.println("(1) Red Fox Roullete");
System.out.println("(2) Crash");
System.out.println("(3) Blackjack");
System.out.print("Enter the number for the game you'd like to play!: ");
int game = input.nextInt();
if(game == 1) {
do {
System.out.println("---------------------------------------------");
System.out.println("Welcome to Red Fox Roullete!");
System.out.print("Please choose Black or Red....and a number from 1-10: ");
String color = input.next();
int number = input.nextInt();
System.out.print("Your available balance is $"+balance+". How much would you like to bet on "+color+"?");
bet1 = input.nextInt();
balance -= bet1;
System.out.print("Your available balance is $"+balance+". How much would you like to bet on "+number+"?");
bet2 = input.nextInt();
balance -= bet2;
System.out.println("------------------------------BET INFO------------------------------------");
System.out.println("You just bet $"+bet1+" on "+color+" and $"+bet2+" on number "+number);
System.out.println("Spinning............");
System.out.println("------------------------------RESULTS-------------------------------------");
Random rouletteNum = new Random();
int rNum = min + rouletteNum.nextInt(max);
int rCol = min + rouletteNum.nextInt(colmax);
if (rCol == 1) {
System.out.println("The machine landed on Black "+rNum);
}
else if(rCol != 1) {
System.out.println("The machine landed on Red "+rNum);
}
if(rNum == number) {
System.out.println("Congrats, you guessed the right number! You've won $"+profit2);
balance += (bet2 * 5);
}
else if(rNum != number) {
System.out.println("Sorry!You didnt guess the right number! You've lost "+bet2);
}
if(rCol == 1 && color.equals("Black")) {
System.out.println("Congrats, you guessed the right color! You've won $"+profit1);
balance += bet1 * 2 - bet1;
}
else if(rCol == 2 && color.equals("Red")) {
System.out.println("Congrats, you guessed the right color! You've won $"+profit1);
balance += bet1 * 2 - bet1;
}
if(rCol == 2 && color.equals("Black")) {
System.out.println("Sorry, you didn't guess the right color. You've lost $"+bet1);
}
else if(rCol == 1 && color.equals("Red")) {
System.out.println("Sorry, you didn't guess the right color. You've lost $"+bet1);
}
System.out.println("After the bet, you're updated balance is $"+balance);
System.out.println("-----------------------------------------------------------------");
System.out.print("Yes or No? Would you like to place another bet in Roulette?");
kBet = input.next();
}
while(kBet.equals("Yes"));
if(kBet.equals("No")) {
System.out.println("(1) Red Fox Roullete");
System.out.println("(2) Blackjack");
System.out.println("(3) Crash");
System.out.print("Enter the number for the game you'd like to play!: ");
game = input.nextInt();
}
}
//CRASH
else if (game == 2) {
do {
int bet = 0;
double start = 1.00;
double crashValue = 1.00;
int stopGame = 1;
double cashout = 0;
System.out.println("-------------------CRASH GAME--------------------------");
System.out.println("Welcome to Crash!");
System.out.print("What number would you like to cashout at?(Ex. 1.15):");
cashout = input.nextDouble();
System.out.print("Your balance is $"+balance+". How much would you like to bet on this round?:");
bet = input.nextInt();
System.out.println("--------------------------------------------------------------------------");
System.out.println("Round is beginning.........");
for(int i =0; i < stopGame; i++) {
do {
double crash = Math.random() * 100;
if (crash < 95) {
start += .01;
System.out.printf("%.2f\n",start);
}
else if(crash > 95) {
i++;
crashValue = start;
System.out.println("----------------------------RESULTS--------------------------------");
System.out.print("CRASH! The game crashed at ");
System.out.printf("%.2f",start);
System.out.println("x");
}
}
while(i == 0);
}
if(cashout < crashValue) {
System.out.println("Congrats! You cashed out at "+cashout+" before the game crashed. You've won $"+bet*cashout);
balance += bet * cashout;
}
else {
System.out.println("Sorry! The game crashed before you could cash out. You've lost $"+bet);
balance -= bet;
}
System.out.println("After your bet, you're updated balance is $"+balance);
System.out.println("-------------------------------------------------------------------");
System.out.print("Yes or No? Would you like to play another round of Crash?: ");
kBet = input.next();
}
while(kBet.equals("Yes"));
if(kBet.equals("No")) {
System.out.println("(1) Red Fox Roullete");
System.out.println("(2) Blackjack");
System.out.println("(3) Crash");
System.out.print("Enter the number for the game you'd like to play!: ");
game = input.nextInt();
}
}
//BlackJack Game
else if(game == 3) {
Scanner input1 = new Scanner(System.in);
System.out.println("---------------------Black Jack--------------------------");
System.out.println("Welcome to BlackJack!");
System.out.println("Available balance is $"+balance);
System.out.print("How much would you like to bet on this hand?: ");
int bet = input.nextInt();
balance -= bet;
System.out.println("You just bet $"+bet+"......Dealing cards!");
System.out.println("----------------------------------------------------------");
String pCard1 = dealCard();
String pCard2 = dealCard();
int value1 = getCardValue(pCard1);
int value2 = getCardValue(pCard2);
System.out.println("Your hand is a "+pCard1+" and a "+pCard2);
System.out.print("Would you like to Hit or Stand?: ");
String HitOrStand = input.next();
}
}
public static String dealCard() {
int rCard = (int)Math.random() * 14;
switch(rCard) {
case 1 : return "2";
case 2 : return "3";
case 3 : return "4";
case 4 : return "5";
case 5 : return "6";
case 6 : return "7";
case 7 : return "8";
case 8 : return "9";
case 9 : return "10";
case 10 : return "Queen";
case 11 : return "Jack";
case 12 : return "King";
case 13 : return "Ace";
}
}
public static int getCardValue(String x) {
if(x.equals("2")) {
return 2;
}
if(x.equals("3")) {
return 3;
}
if(x.equals("4")) {
return 4;
}
if(x.equals("5")) {
return 5;
}
if(x.equals("6")) {
return 6;
}
if(x.equals("7")) {
return 7;
}
if(x.equals("8")) {
return 8;
}
if(x.equals("9")) {
return 9;
}
if(x.equals("10")) {
return 10;
}
if(x.equals("Queen")) {
return 10;
}
if(x.equals("Jack")) {
return 10;
}
if(x.equals("King")) {
return 10;
}
if(x.equals("Ace")) {
return 11;
}
}
}
}
}

Two more brackets and two missing return:
import java.util.Scanner;
import java.util.Random;
public class Project2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int min = 1;
int max = 10;
int colmax = 2;
double balance = 2500.0;
double bet1 = 0;
double bet2 = 0;
double profit1 = (bet1 * 2) - bet1;
double profit2 = (bet2 * 5) - bet2;
String kBet = null;
System.out.println(" -==-==-==-==-==-==-==-==-==-==-" );
System.out.println("-==-==-=={ Welcome to the Marist Casino! }==-==-==-");
System.out.println(" -==-==-==-==-==-==-==-==-==-==-" );
System.out.println("(1) Red Fox Roullete");
System.out.println("(2) Crash");
System.out.println("(3) Blackjack");
System.out.print("Enter the number for the game you'd like to play!: ");
int game = input.nextInt();
if(game == 1) {
do {
System.out.println("---------------------------------------------");
System.out.println("Welcome to Red Fox Roullete!");
System.out.print("Please choose Black or Red....and a number from 1-10: ");
String color = input.next();
int number = input.nextInt();
System.out.print("Your available balance is $"+balance+". How much would you like to bet on "+color+"?");
bet1 = input.nextInt();
balance -= bet1;
System.out.print("Your available balance is $"+balance+". How much would you like to bet on "+number+"?");
bet2 = input.nextInt();
balance -= bet2;
System.out.println("------------------------------BET INFO------------------------------------");
System.out.println("You just bet $"+bet1+" on "+color+" and $"+bet2+" on number "+number);
System.out.println("Spinning............");
System.out.println("------------------------------RESULTS-------------------------------------");
Random rouletteNum = new Random();
int rNum = min + rouletteNum.nextInt(max);
int rCol = min + rouletteNum.nextInt(colmax);
if (rCol == 1) {
System.out.println("The machine landed on Black "+rNum);
}
else if(rCol != 1) {
System.out.println("The machine landed on Red "+rNum);
}
if(rNum == number) {
System.out.println("Congrats, you guessed the right number! You've won $"+profit2);
balance += (bet2 * 5);
}
else if(rNum != number) {
System.out.println("Sorry!You didnt guess the right number! You've lost "+bet2);
}
if(rCol == 1 && color.equals("Black")) {
System.out.println("Congrats, you guessed the right color! You've won $"+profit1);
balance += bet1 * 2 - bet1;
}
else if(rCol == 2 && color.equals("Red")) {
System.out.println("Congrats, you guessed the right color! You've won $"+profit1);
balance += bet1 * 2 - bet1;
}
if(rCol == 2 && color.equals("Black")) {
System.out.println("Sorry, you didn't guess the right color. You've lost $"+bet1);
}
else if(rCol == 1 && color.equals("Red")) {
System.out.println("Sorry, you didn't guess the right color. You've lost $"+bet1);
}
System.out.println("After the bet, you're updated balance is $"+balance);
System.out.println("-----------------------------------------------------------------");
System.out.print("Yes or No? Would you like to place another bet in Roulette?");
kBet = input.next();
}
while(kBet.equals("Yes"));
if(kBet.equals("No")) {
System.out.println("(1) Red Fox Roullete");
System.out.println("(2) Blackjack");
System.out.println("(3) Crash");
System.out.print("Enter the number for the game you'd like to play!: ");
game = input.nextInt();
}
}
//CRASH
else if (game == 2) {
do {
int bet = 0;
double start = 1.00;
double crashValue = 1.00;
int stopGame = 1;
double cashout = 0;
System.out.println("-------------------CRASH GAME--------------------------");
System.out.println("Welcome to Crash!");
System.out.print("What number would you like to cashout at?(Ex. 1.15):");
cashout = input.nextDouble();
System.out.print("Your balance is $"+balance+". How much would you like to bet on this round?:");
bet = input.nextInt();
System.out.println("--------------------------------------------------------------------------");
System.out.println("Round is beginning.........");
for(int i =0; i < stopGame; i++) {
do {
double crash = Math.random() * 100;
if (crash < 95) {
start += .01;
System.out.printf("%.2f\n",start);
}
else if(crash > 95) {
i++;
crashValue = start;
System.out.println("----------------------------RESULTS--------------------------------");
System.out.print("CRASH! The game crashed at ");
System.out.printf("%.2f",start);
System.out.println("x");
}
}
while(i == 0);
}
if(cashout < crashValue) {
System.out.println("Congrats! You cashed out at "+cashout+" before the game crashed. You've won $"+bet*cashout);
balance += bet * cashout;
}
else {
System.out.println("Sorry! The game crashed before you could cash out. You've lost $"+bet);
balance -= bet;
}
System.out.println("After your bet, you're updated balance is $"+balance);
System.out.println("-------------------------------------------------------------------");
System.out.print("Yes or No? Would you like to play another round of Crash?: ");
kBet = input.next();
}
while(kBet.equals("Yes"));
if(kBet.equals("No")) {
System.out.println("(1) Red Fox Roullete");
System.out.println("(2) Blackjack");
System.out.println("(3) Crash");
System.out.print("Enter the number for the game you'd like to play!: ");
game = input.nextInt();
}
}
//BlackJack Game
else if(game == 3) {
Scanner input1 = new Scanner(System.in);
System.out.println("---------------------Black Jack--------------------------");
System.out.println("Welcome to BlackJack!");
System.out.println("Available balance is $"+balance);
System.out.print("How much would you like to bet on this hand?: ");
int bet = input.nextInt();
balance -= bet;
System.out.println("You just bet $"+bet+"......Dealing cards!");
System.out.println("----------------------------------------------------------");
String pCard1 = dealCard();
String pCard2 = dealCard();
int value1 = getCardValue(pCard1);
int value2 = getCardValue(pCard2);
System.out.println("Your hand is a "+pCard1+" and a "+pCard2);
System.out.print("Would you like to Hit or Stand?: ");
String HitOrStand = input.next();
}
}
public static String dealCard() {
int rCard = (int)Math.random() * 14;
switch(rCard) {
case 1 : return "2";
case 2 : return "3";
case 3 : return "4";
case 4 : return "5";
case 5 : return "6";
case 6 : return "7";
case 7 : return "8";
case 8 : return "9";
case 9 : return "10";
case 10 : return "Queen";
case 11 : return "Jack";
case 12 : return "King";
case 13 : return "Ace";
}
return "Unknown";
}
public static int getCardValue(String x) {
if(x.equals("2")) {
return 2;
}
if(x.equals("3")) {
return 3;
}
if(x.equals("4")) {
return 4;
}
if(x.equals("5")) {
return 5;
}
if(x.equals("6")) {
return 6;
}
if(x.equals("7")) {
return 7;
}
if(x.equals("8")) {
return 8;
}
if(x.equals("9")) {
return 9;
}
if(x.equals("10")) {
return 10;
}
if(x.equals("Queen")) {
return 10;
}
if(x.equals("Jack")) {
return 10;
}
if(x.equals("King")) {
return 10;
}
if(x.equals("Ace")) {
return 11;
}
return -1;
}
}

Related

I'm trying to add components relating to bets to my game in Java, but the program stops working after it asks for a bet amount

So I'm trying to make this car racing game, modeled after horse races. I've tested the game without the betting component and it works the way I want it to, so thats no problem. But once I started to implement the betting, the program kinda gave up on me. After the user is prompted to enter the bet, the program is just blank.
I'm not getting any error messages and I've tried debugging, but I just really can't figure it out.
I've attached the whole code minus the header; all it is is the names of the cars. (1,2,3)
public static void main(String[]args)throws IOException, InterruptedException{ //start main
Scanner in;
in = new Scanner(System.in);
boolean doAnother = true;
printHeader();
printGame();
while (doAnother) { //start while
int response;
System.out.print("\nWanna play again? 1 = yes, 2 = no.");
response = in.nextInt();
if (response == 1) { // start if loop
doAnother = true;
printHeader();
printGame();
} // end if loop
else {
doAnother = false;
System.out.println("Come back again soon!");
}
} //end while look
} //end main
public static boolean printGame()throws IOException, InterruptedException{
Scanner in = new Scanner(System.in);
Random rd = new Random();
int car = in.nextInt();
if (car >= 4) {
System.out.println("THATS NOT AN OPTION! You automatically lose.");
} else
bet();
System.out.println("Commence race!");
Thread.sleep(250);
int lambo = 0,
nissan = 0,
egg = 0;
int track = 100;
while (true) {
for (int i = 0; i < 50; i++)
System.out.println();
for (int i = 0; i < track; i++) {
System.out.print("-");
}
System.out.println();
lambo = lambo + rd.nextInt(4) + rd.nextInt(2) - rd.nextInt(2);
nissan = nissan + rd.nextInt(4) + rd.nextInt(2) - rd.nextInt(2);
egg = egg + rd.nextInt(4) + rd.nextInt(2) - rd.nextInt(2);
for (int i = 0; i < lambo; i++) //L
{
System.out.print("."); //distance travelled
}
System.out.println("ℾ");
//
for (int i = 0; i < nissan; i++) //M
{
System.out.print("."); //distance travelled
}
System.out.println("ℿ");
//
for (int i = 0; i < egg; i++) //T
{
System.out.print("."); //distance travelled
}
System.out.println("⅀");
//
for (int i = 0; i < track; i++) {
System.out.print("-");
}
System.out.println();
//
Thread.sleep(250);
//
if (nissan > track || lambo > track || egg > track) {
break;
}
}
if (car == track) {
System.out.println("\nYour car won! Rad!");
return true;
} else {
System.out.println("\nYour car lost... bummer.");
return false;
}
} //printGame
public static void bet()throws IOException, InterruptedException{
Scanner in;
in = new Scanner(System.in);
int money;
int bet;
boolean userWins = true;
money = 100;
while (true) {
System.out.println("You have " + money + " dollars.");
do {
System.out.println("How much do you wanna bet? Or, enter 0 to walk away.)");
System.out.print("$");
bet = in.nextInt();
if (bet < 0 || bet > money) {
System.out.println("Your bet must be between 0 and " + money + '.');
}
} while (bet < 0 || bet > money);{
if (bet == 0) {
System.out.println("Bye.");
break; //walk away
} else {
userWins = printGame();
}
if (userWins == true) {
money = money + bet;
}
if (userWins == false) {
money = money - bet;
System.out.println();
}
if (money == 0) {
System.out.println("Aw shoot, looks like you've are out of money!");
break;
}
}
}
System.out.println();
System.out.println("You walk away with $" + money + '.');
} //end method
You can't have two scanners nested like that. You must use the same scanner from the main method in you other methods or close that scanner

When the loop comes to a continue statement at the end it doesn't restart from the top

Ok, so I have created the following program. It is not yet complete but when the code comes to the end (y or n part) and the user decides to try again they don't get the option to enter a new bet it just uses the one entered from the first time.
(Please comment if you need help about understanding the code or thinking it might be hard for other people to understand)
import java.util.*;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; //variable to cancel whole loop
char yesNo; //if user wants to continue playing or not
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
do { //loop to test if the bet is legit
if(money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while(bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n)");
yesNo = kin.next().charAt(0);
int loopBreak = 0; //to break do while loop bellow
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while(loopBreak == 0);
if(play == 0) {
break;
}
}
}
}
}
Its all about nested while loop don't stop when it needed. Its like this
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
...
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while(loopBreak == 0);
if(play == 0) {
break;
}
}
}
After you get answer 'y' you don't break the nested while and program never get the
while(money > 0 && play == 1) {
--> System.out.print("Please Enter The Amount You Want To Bet: ");
--> double bet = kin.nextDouble();
//because below while loop continues to loop
while((bet <= money || bet > 0)) {
...
}
}
part. Anyways corrected code is here(i tried every possibility and it worked)
import java.util.*;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; //variable to cancel whole loop
char yesNo; //if user wants to continue playing or not
while(money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while((bet <= money || bet > 0)) {
do { //loop to test if the bet is legit
if(money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while(bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n): ");
yesNo = kin.next().charAt(0);
int loopBreak = 0; //to break do while loop bellow
do {
if(yesNo == 'y') { //take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak++;
continue;
}
else if(yesNo == 'n') {
System.out.println("Thank You For Playing");
play--; //to cancel whole program
break;
}
else {
System.out.println("Please Enter 'y' Or 'n': ");
}
} while(loopBreak == 0);
if(play == 0 || loopBreak == 1) {
break;
}
}
}
}
}
Have a good day!
Another solution is to use labeled breaks/continues https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
However it should be used sparingly (or not at all). You probably can simplify it in some other way.
package test.test;
import java.util.Scanner;
public class Lab15 {
static Scanner kin = new Scanner(System.in);
public static void main(String[] args) {
double money = 1000;
System.out.println("Welcom To Craps You Start With 1000$!!!");
int play = 1; // variable to cancel whole loop
char yesNo; // if user wants to continue playing or not
//Labeled <-----------------
start: while (money > 0 && play == 1) {
System.out.print("Please Enter The Amount You Want To Bet: ");
double bet = kin.nextDouble();
while ((bet <= money || bet > 0)) {
do { // loop to test if the bet is legit
if (money < bet || bet < 0) {
System.out.print("Sorry Invalid Bet; Please Enter A Legitimant Bet: ");
bet = kin.nextDouble();
}
} while (bet > money || bet < 0);
money -= bet;
System.out.println("You Have Bet $" + bet + " And Have " + money + " Left");
int die1 = (1 + (int) (6 * Math.random()));
int die2 = (1 + (int) (3 * Math.random()));
int sum = die1 + die2;
System.out.println("You Rolled " + die1 + " and " + die2 + " totaling " + sum);
System.out.print("Do You Want To Play Again(y) Or Not(n)");
yesNo = kin.next().charAt(0);
int loopBreak = 0; // to break do while loop bellow
do {
if (yesNo == 'y') { // take above yes no to restart loop or not
System.out.println("Restarting...");
loopBreak += 1;
//Labeled continue <-----------------
continue start;
} else if (yesNo == 'n') {
System.out.println("Thank You For Playing");
play -= 1; // to cancel whole program
break;
} else {
System.out.println("Please Enter 'y' Or 'n'");
}
} while (loopBreak == 0);
if (play == 0) {
break;
}
}
}
}
}

update a variable in a different class

How do I make amount() in class Casino.java store the total value and allow it to be returned to the class Roulette.java.
When I use:
int amount = Casino.amount();
It gives me several hundred lines of errors.
What I want done is to run the game number() and store the value into Casino.amount()
Please note in class Roulette.java there is a function called amountUpdate() which should update Casino.amount().
This is class Casino.java
package Casino;
import java.util.*;
public class Casino {
static String player = "";
static int playAmount = 0;
public static void main(String[] args) {
System.out.println("Welcome to Michael & Erics Casino!");
player();
ask();
start();
}
public static String player() {
if (player.equals("")) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your name : ");
player = sc.nextLine();
}
return player;
}
public static void ask() {
Scanner sc = new Scanner(System.in);
System.out.println("How much would you like to play with? ");
playAmount = sc.nextInt();
if (playAmount < 1) {
System.out.println("Please enter a value that is more than $1");
playAmount = 0;
}
System.out.println("You are playing with: $" + playAmount + "\n");
}
public static int amount() {
int amount = playAmount;
if (Roulette.amountUpdate() >= 1) {
amount += Roulette.amountUpdate();
}
return amount;
/*if (Blackjack.amountUpdate() >= 1)
amount += Blackjack.amountUpdate();
return amount;*/
}
public static void start() {
System.out.println("Which table would you like to play at?");
System.out.println("1: Blackjack");
System.out.println("2: Roulette");
System.out.println("3: Check Balance");
System.out.println("4: Exit");
Scanner sc = new Scanner(System.in);
int table = sc.nextInt();
switch (table) {
case 1:
//blackjack.main(new String[]{});
break;
case 2:
Roulette.main(new String[]{});
break;
case 3:
System.out.println("You have : $" + playAmount);
start();
break;
case 4:
System.exit(0);
break;
}
}
}
This is class Roulette.java
package Casino;
import java.util.*;
import java.lang.*;
public class Roulette {
public static void main(String[] args) {
System.out.println("Welcome to the roulette table " + Casino.player());
placeBet();
amountUpdate();
//number();
}
public static int amountUpdate() {
int amount = 0;
if (number() > 0) {
amount += number();
}
if (br() > 0) {
amount += br();
}
if (oe() > 0) {
amount += oe();
}
if (third() > 0) {
amount += third();
}
return amount;
}
public static void placeBet() {
Scanner sc_Choice = new Scanner(System.in);
System.out.println("What would you like to bet on?");
System.out.println("1: Numbers");
System.out.println("2: Black or Red");
System.out.println("3: Odd or Even");
System.out.println("4: One Third");
System.out.println("5: Count Chips");
System.out.println("6: Restart");
int choice = sc_Choice.nextInt();
if (choice > 0 && choice < 7) {
switch (choice) {
case 1:
number();
break;
case 2:
br();
break;
case 3:
oe();
break;
case 4:
third();
break;
case 5:
System.out.println(Casino.amount());
break;
case 6:
Casino.main(new String[]{});
break;
}
} else {
System.out.println("You must choose between 1 and 6");
}
}
public static int number() {
Boolean betting = true;
//int amount = Casino.amount();
int amount = 5000;
int number;
int winnings;
String reply;
int betX;
int betAgain = 1;
Scanner sc_Number = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> bet = new ArrayList<Integer>();
while (betAgain == 1) {
System.out.println("What number would you like to place a bet on?");
int listCheck = sc_Number.nextInt();
if (listCheck >= 0 && listCheck <= 36) {
list.add(listCheck);
} else {
System.out.println("You must choose a number between 0 and 36");
}
System.out.println("How much do you want to bet?");
betX = sc_Number.nextInt();
if (betX > amount) {
System.out.println("You have insufficient funds to make that bet");
number();
} else if (betX < 1) {
System.out.println("You must bet more than 1$");
} else {
bet.add(betX);
amount = amount - betX;
}
System.out.println("Do you want to bet on more numbers?");
reply = sc_Number.next();
if (reply.matches("no|No|false|nope")) {
betAgain = betAgain - 1;
//No - Don't bet again
} else {
betAgain = 1;
//Yes - Bet again
}
int result = wheel();
System.out.println("Spinning! .... The number is: " + result);
for (int i = 0; i < bet.size(); i++) {
if (list.get(i) == result) {
winnings = bet.get(i) * 35;
System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);
betAgain = betAgain - 1;
} else {
System.out.println("Sorry, better luck next time!");
System.out.println("Current Balance: " + amount);
betAgain = betAgain - 1;
}
}
betAgain = betAgain - 1;
}
return amount;
}
public static int wheel() {
Random rnd = new Random();
int number = rnd.nextInt(37);
return number;
}
//NOT WORKING - AFTER MAKING A BET IT RUNS Number()
public static int br() {
Scanner sc_br = new Scanner(System.in);
int amount = Casino.amount();
int winnings;
System.out.println("Would you like to bet on Black or Red?");
String replyBR = sc_br.nextLine();
boolean black;
//****************
if (replyBR.matches("black|Black")) {
black = true;
} else {
black = false;
}
System.out.println("How much would you like to bet?");
int betBR = sc_br.nextInt();
if (betBR > amount) {
System.out.println("You have insufficient funds to make that bet");
br();
} else if (betBR < 1) {
System.out.println("You must bet more than 1$");
br();
} else {
amount = amount - betBR;
}
//*****************
boolean resultColour = colour();
if (resultColour == black) {
winnings = betBR * 2;
//PRINT OUT WHAT COLOUR!!!
System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);
} else {
System.out.println("Sorry, better luck next time!");
}
System.out.println("Current Balance: " + amount);
return amount;
}
public static boolean colour() {
Random rnd = new Random();
boolean colour = rnd.nextBoolean();
return colour;
}
//NOT WORKING - AFTER MAKING A BET IT RUNS Number()
public static int oe() {
Scanner sc_oe = new Scanner(System.in);
int amount = Casino.amount();
int winnings;
System.out.println("Would you like to bet on Odd or Even?");
String replyOE = sc_oe.next();
System.out.println("How much would you like to bet?");
int betOE = sc_oe.nextInt();
if (betOE > amount) {
System.out.println("You have insufficient funds to make that bet");
oe();
}
amount = amount - betOE;
boolean resultOE = oddOrEven();
//PRINT OUT IF IT WAS ODD OR EVEN
if (resultOE == true) {
winnings = betOE * 2;
System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);
} else {
System.out.println("Sorry, better luck next time!");
System.out.println("Current Balance: " + amount);
}
return amount;
}
public static boolean oddOrEven() {
Random rnd = new Random();
boolean num = rnd.nextBoolean();
return num;
}
//NOT WORKING - AFTER MAKING A BET IT RUNS Number()
public static int third() {
Scanner sc_Third = new Scanner(System.in);
int amount = Casino.amount();
int winnings;
System.out.println("Would you like to bet on 1st, 2nd or 3rd third?");
String replyT = sc_Third.next();
System.out.println("How much would you like to bet?");
int betT = sc_Third.nextInt();
if (betT > amount) {
System.out.println("You have insufficient funds to make that bet");
third();
}
amount = amount - betT;
boolean resultT = thirdResult();
//PRINT OUT WHAT NUMBER IT WAS AND IF IT WAS IN WHICH THIRD
if (resultT == true) {
winnings = betT * 3;
System.out.println("Congratulations!! You won: $" + winnings);
amount = amount + winnings;
System.out.println("Current Balance: " + amount);
} else {
System.out.println("Sorry, better luck next time!");
System.out.println("Current Balance: " + amount);
}
return amount;
}
public static boolean thirdResult() {
Random rnd = new Random();
int num = rnd.nextInt(2);
if (num == 0) {
return true;
} else {
return false;
}
}
}
Looks like you're probably running into a StackOverflowException. When you call Casino.amount() inside of Roulette.number(), it then calls Roulette.amountUpdate(), which then calls Roulette.number(). Your methods are stuck in an infinite loop like this. You'll need to redesign your code such that these 3 functions are not all dependent on each other.
Your code is terse, so it's hard to help you fully solve the problem, but I believe you would benefit from splitting up your "amount" variable into separate entities. Keep things like bet amount, winnings, and such separate until you need to combine them.
Another issue you may run into is thatRoulette.amountUpdate() is called twice in Casino.amount(), but Roulette.amountUpdate() will not necessarily return the same thing both times. Consider storing the return value from the first call instead of calling it twice.

I have to create a High Low game. I don't understand why it won't work

public static void main(String[] args) {
int money = 100, roll1, roll2;
int userBet;
char c;
int lostwin;
Scanner in = new Scanner(System.in);
do {
if (money == 0 || money < 0)
break;
System.out.println(" You have " + money + " dollars. ");
userBet = getBet(in, money);
if (userBet == 0)
break;
c = getHighLow(in);
roll1 = getRoll();
System.out.println(" Die 1 rolls : " + roll1);
roll2 = getRoll();
System.out.println(" Die 2 rolls : " + roll2);
System.out.println("Total of two dice is: " + (roll1 + roll2));
lostwin = determineWinnings(c, userBet, roll1 + roll2);
if (lostwin < 0)
System.out.println("You lost!");
else
System.out.println("You won " + lostwin + " dollars! ");
money = money + lostwin;
} while (true);
}
private static int getBet(Scanner inScanner, int moneyPot) {
System.out.println("Enter an amount to bet (0 to quit): ");
int result = inScanner.nextInt();
if (result > moneyPot) {
do {
System.out.println("Enter an amount to bet (0 to quit): ");
result = inScanner.nextInt();
} while (result > moneyPot);
}
return result;
}
private static char getHighLow(Scanner inScanner) {
System.out.println("High, low or sevens (H/L/S): ");
String str = inScanner.next();
return str.charAt(0);
}
private static int getRoll() {
return (int) (Math.random() * 6) + 1;
}
private static int determineWinnings(char highLow, int bet, int roll) {
int result = 0;
if (highLow == 'H') {
if (roll < 7) {
result = -1 * bet;
} else {
result = bet;
}
}
if (highLow == 'L') {
if (roll > 7) {
result = -1 * bet;
} else {
result = bet;
}
}
if (highLow == 'S') {
if (roll == 7) {
result = 4 * bet;
} else {
result = -1 * bet;
}
}
return result;
}
I need the program to say "Goodbye!" when the user enters the number 0, but I can't figure out where to put it or how to get it to work. The other issue I need help with is if the user enters a number higher than 100 or less than 1, the program needs to say "Your bet MUST be between 0 and 100 dollars". I don't know where to put them or how to get them to work.
You can modify your getBet function like this to achieve what you want.
private static int getBet(Scanner inScanner, int moneyPot) {
int result;
System.out.println("Enter an amount to bet (1-100) (0 to quit): ");
do {
result = inScanner.nextInt();
if (result == 0) {
System.out.println("Good Bye");
return result;
} else if (result < 0 && result > 100) {
System.out.println("Please enter an amount between (1-100) (0 to quit)");
} else if (result > moneyPot) {
System.out.println("Please enter an amount less than your moneyPot between (1-100) (0 to quit)");
} else {
return result;
}
} while (true);
}
public static void main(String[] args) {
int money = 100;
int roll1;
int roll2;
int userBet;
int lostwin;
char c;
Scanner in = new Scanner(System.in);
do {
if (money < 1)
break;
System.out.println(" You have " + money + " dollars. ");
userBet = getBet(in, money);
if (userBet == 0)
break;
c = getHighLow(in);
roll1 = getRoll();
System.out.println(" Die 1 rolls : " + roll1);
roll2 = getRoll();
System.out.println(" Die 2 rolls : " + roll2);
System.out.println("Total of two dice is: " + (roll1 + roll2));
lostwin = determineWinnings(c, userBet, roll1 + roll2);
if (lostwin < 0)
System.out.println("You lost!");
else
System.out.println("You won " + lostwin + " dollars! ");
money = money + lostwin;
} while (money > 0);
System.out.println("Goodbye!");
}
private static int getBet(Scanner inScanner, int moneyPot) {
int bet;
do {
System.out.println("Enter an amount to bet (0 to quit): ");
bet = inScanner.nextInt();
} while (bet > moneyPot && bet != 0);
return bet;
}
private static char getHighLow(Scanner inScanner) {
System.out.println("High, low or sevens (H/L/S): ");
String str = inScanner.next();
return str.charAt(0);
}

int variable remains at 0, while loop not running properly as a result

What I really am stuck with is the second to last variable userGuessDifference. It remains at zero making my second while loop not run properly as it just keeps going back to the first else if statement.
public class GuessingGame {
/**
* #param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
Random generator = new Random();
int difficulty = 0;
int guesses = 0;
int userGuess = 0;
int correctAnswer = 0;
int counter = 0;
int userGuessDifference = (Math.abs(correctAnswer) - Math.abs(userGuess));
boolean flag = false;
System.out.println("We are going to play a number guessing game.");
System.out.println(" ");
System.out.println("Choose your difficulty:");
System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
difficulty = keyboard.nextInt();
if (difficulty == 10)
{
guesses = 3;
System.out.println("You have 3 guesses, make them count!");
}
else if (difficulty == 25)
{
guesses = 5;
System.out.println("You have 5 guesses, make them count!");
}
else if (difficulty == 50)
{
guesses = 6;
System.out.println("You have 6 guesses, make them count!");
}
else
{
System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
difficulty = (difficulty * 100);
guesses = 1;
}
System.out.println(" ");
System.out.println("Ok, I have my number. Time to play.");
correctAnswer = generator.nextInt(difficulty) + 1;
System.out.println("Pick a whole number between 1 and " + difficulty + ":");
userGuess = keyboard.nextInt();
while (!flag || (counter <= guesses))
{
if (userGuess == correctAnswer)
{
System.out.println("CONGRATS YOU WIN!");
flag = true;
}
else if ((userGuessDifference <= (difficulty * .10)))
{
System.out.println("HOT!");
userGuess = keyboard.nextInt();
counter++;
}
else if ((userGuessDifference < (difficulty * .25)) && (userGuessDifference > (difficulty * .10)))
{
System.out.println("Warm...");
userGuess = keyboard.nextInt();
counter++;
}
else
{
System.out.println("Ice cold.");
userGuess = keyboard.nextInt();
counter++;
}
}
}
}
As #SotiriosDelimanolis wrote, you never reassign userGuessDifference. This should be done inside the while loop.
Moreover, there is another problem with your code: if you guess the number, the program just prints "CONGRATS YOU WIN!" forever, but it seems to me that you wanted to quit from the while loop once the user guesses the number (I guess the flag variable was introduced for this reason).
I slightly changed your code in order to meet this requirement:
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random generator = new Random();
int difficulty = 0;
int guesses = 0;
int userGuess = 0;
int correctAnswer = 0;
int counter = 0;
System.out.println("We are going to play a number guessing game.");
System.out.println(" ");
System.out.println("Choose your difficulty:");
System.out.println("Pick a number - 10 is easy, 25 is medium, 50 is hard.");
difficulty = keyboard.nextInt();
if (difficulty == 10) {
guesses = 3;
System.out.println("You have 3 guesses, make them count!");
} else if (difficulty == 25) {
guesses = 5;
System.out.println("You have 5 guesses, make them count!");
} else if (difficulty == 50) {
guesses = 6;
System.out.println("You have 6 guesses, make them count!");
} else {
System.out.println("If you can't follow instructions, I'm going to make this very difficult for you!");
difficulty = (difficulty * 100);
guesses = 1;
}
System.out.println(" ");
System.out.println("Ok, I have my number. Time to play.");
correctAnswer = generator.nextInt(difficulty) + 1;
System.out.println("Pick a whole number between 1 and " + difficulty + ":");
userGuess = keyboard.nextInt();
while (counter <= guesses) {
// int userGuessDifference = (Math.abs(correctAnswer) - Math
// .abs(userGuess));
int userGuessDifference = Math.abs(correctAnswer - userGuess);
if (userGuess == correctAnswer) {
System.out.println("CONGRATS YOU WIN!");
break;
}
else if ((userGuessDifference <= (difficulty * .10))) {
System.out.println("HOT!");
}
else if ((userGuessDifference < (difficulty * .25))
&& (userGuessDifference > (difficulty * .10))) {
System.out.println("Warm...");
}
else {
System.out.println("Ice cold.");
}
userGuess = keyboard.nextInt();
counter++;
}
}
}

Categories