This is the program, I designed it to be a phone store, and the problem I am facing is that after you choose to go back, the program will go back normally, but after you choose to buy the next time it restarts the program. What I want is to make it stop at buy directly.
import java.util.Scanner;
public class MyStore {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Phones phOne = new Phones("iPhone 13.");
phOne.phs(256);
phOne.php(904);
Phones phTwo = new Phones("Galaxy s22.");
phTwo.phs(512);
phTwo.php(1199);
Phones phThree = new Phones("Huawei p50 pocket.");
phThree.phs(512);
phThree.php(1699);
System.out.println("Welcom To My Store!\n-------*****-------");
int i = 1;
do {
System.out.println("Choose a phone please:");
System.out.println("1-iPhone 13.\n2-Galaxy s22.\n3-Huawei p50 pocket.");
int a = scan.nextInt();
System.out.println("-------*****-------");
switch(a) {
case 1:
phOne.print();
System.out.println("1-buy.\n2-go back.");
int bg1 = scan.nextInt();
if(bg1 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
--i;
}
else if(bg1 == 2){
++i;
}
else {
System.out.println("Please choose 1 or 2.");
}
break;
case 2:
phTwo.print();
System.out.println("1-buy.\n2-go back.");
int bg2 = scan.nextInt();
if(bg2 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
--i;
}
else if(bg2 == 2){
++i;
}
else {
System.out.println("Please choose 1 or 2.");
}
break;
case 3:
phThree.print();
System.out.println("1-buy.\n2-go back.");
int bg3 = scan.nextInt();
if(bg3 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
i-=2;
}
else if(bg3 == 2){
++i;
}
else {
System.out.println("Please choose 1 or 2.");
}
break;
default:
System.out.println("Please choose 1, 2, or 3.");
}
}while(i >= 1);
}
}
And this is the rest of the program if you need it.
import java.text.NumberFormat;
public class Phones {
NumberFormat nf = NumberFormat.getCurrencyInstance();
String name;
int storage;
double price;
public Phones(String name) {
this.name = name;
}
public void phs(int phs) {
storage = phs;
}
public void php(double php) {
price = php;
}
public void print() {
System.out.println("Model: "+name);
System.out.println("Storage: "+storage+"G");
System.out.println("Price: "+nf.format(price));
}
}
If you want the program to stop after buying a phone you'd want to implement the do-while loop with a boolean value, then after the user buys a phone set the boolean value to false.
boolean loop = true;
do {
System.out.println("Choose a phone please:");
System.out.println("1-iPhone 13.\n2-Galaxy s22.\n3-Huawei p50 pocket.");
int a = scan.nextInt();
System.out.println("-------*****-------");
switch(a) {
case 1:
phOne.print();
System.out.println("1-buy.\n2-go back.");
int bg1 = scan.nextInt();
if(bg1 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
loop = false;
}
......
while(loop);
I'd recommend taking better care of your variable names. Make them obvious and meaningful, makes it easier to debug in the end!
Related
I am creating a basic banking app that tracks a user's bank account activities, and I cannot seem to figure out why when I run my code that it is simply running what I have set for the "default" case; so even when I press 1,2,3, or 4, the console states, "Error -- Please choose a valid option."
Thanks in advance!
package Account;
import java.util.Scanner;
public class Account extends Bank {
int Balance;
int Previoustransaction;
int amount;
int amount2;
String Name;
String ID;
Account(String Name,String ID){
}
void deposit(int amount) {
if (amount != 0) {
Balance+=amount;
Previoustransaction=amount;
}
}
void withdraw(int amount) {
if(amount!=0) {
Balance-=amount;
Previoustransaction = -amount;
}
}
void getPrevioustransaction() {
if(Previoustransaction > 0) {
System.out.println("Deposited:" + Previoustransaction);
}
else if(Previoustransaction<0) {
System.out.println("Withdrawn:" + Math.abs(Previoustransaction));
} else {
System.out.println("No transaction occurred.");
}
}
void Menu() {
int choice = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Welcome," + Name + ".");
System.out.println("Your account number is" + ID);
System.out.println("What would you like to do?");
System.out.println("1.Check balance.");
System.out.println("2. Make a deposit.");
System.out.println("3. Make a withrawl.");
System.out.println("4. Show last transaction.");
System.out.println("0. Exit.");
do {
System.out.println("Choose an option.");
choice = scan.nextInt();
System.out.println();
switch(choice) {
case'1':
System.out.println("Balance = $" + Balance);
System.out.println();
break;
case'2':
System.out.println("Enter an amount to deposit.");
int amount = scan.nextInt();
deposit (amount);
System.out.println();
break;
case'3':
System.out.println("Enter an amount to withdrawl.");
int amount2 = scan.nextInt();
withdraw(amount2);
break;
case '4':
getPrevioustransaction();
break;
case '0':
break;
default:
System.out.println("Error -- Please choose a valid option.");
}
} while (choice != 0);
System.out.println("Thank you for using the Bank Account Tracker!");
scan.close();
}
{
}
{
}
}
The reason your program isn't working as you expect is that:
you are prompting for user input
capturing that input as a numeric value; specifically, primitive data type int
comparing that int input against various character values – that is, values of primitive data type ch (such as '1')
Here's a paired down version of what you're doing:
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case '1':
System.out.println("match");
break;
default:
System.out.println("some other input found: " + choice);
}
Here's that same block, but instead of case '1' (which matches on a single character value), I changed it to case 1 (which matches on an integer value):
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1: // <-- this is the only edit, use 1 instead of '1'
System.out.println("match");
break;
default:
System.out.println("some other input found: " + choice);
}
So, to fix your program, change your various case statements to use integer values, not characters.
I am trying to make a small game:
There are 2 heroes you can choose: 1 Warrior and 2 Mage.
Next, you should choose how to travel: 1 by Horse 2 Teleportation (available to Mage only).
Finally, choose a weapon: 1 Sword 2 Staff (*Mage can only use Staff; Warrior can use both).
I created a loop for my first question (choosing a hero) so that if the user enters something else aside from 1 or 2, the program will repeat the question ("Choose your hero: ...). I need the same done for my second and third question (especially since there are some restrictions, e. g. if the user chose Warrior, he can't choose Teleportation as his travel option).
public static void main(String[] args) {
int hero, travel, weapon;
Scanner scan = new Scanner(System.in);
loop:
while (true) {
System.out.println("Choose your hero: 1 for Warrior, 2 for Mage");
hero = scan.nextInt();
switch (hero) {
case 1:
System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
travel = scan.nextInt();
break loop;
case 2:
System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
travel = scan.nextInt();
break loop;
default:
break;
}
}
}
I don't know how to use a loop inside another loop properly. I've tried several options but it always returns an error.
It is always a good idea to split things up, instead of making nested loops. Here is a simple way to split the program in 3 methods, each one dealing with a choice.
Hero choice: Offer both choices and loop until given a valid answer. Then return the answer
private static int queryHero() {
Scanner scan = new Scanner(System.in);
int hero;
while (true) {
System.out.println("Choose your hero: 1 for Warrior, 2 for Mage");
hero = scan.nextInt();
if(hero == 1 || hero == 2) {
break;
} else {
System.out.println(hero + " is not a valid choice");
}
}
return hero;
}
Travel option choice: Offer choices depending on the chosen hero and loop until given a valid answer. Then return the answer
private static int queryTravelOptionForHero(int hero) {
Scanner scan = new Scanner(System.in);
int travelOption;
while (true) {
if (hero == 1) {
System.out.println("Choose your travel option: 1 for Horse");
travelOption = scan.nextInt();
if (travelOption == 1) {
break;
} else {
System.out.println(travelOption + " is not a valid choice");
}
} else if (hero == 2) {
System.out.println("Choose your travel option: 1 for Horse; 2 for Teleportation");
travelOption = scan.nextInt();
if (travelOption == 1 || travelOption == 2) {
break;
} else {
System.out.println(travelOption + " is not a valid choice");
}
}
}
return travelOption;
}
Weapon choice: Offer choices depending on the chosen hero and loop until given a valid answer. Then return the answer
private static int queryWeaponForHero(int hero) {
Scanner scan = new Scanner(System.in);
int weapon;
while (true) {
if(hero == 1) {
System.out.println("Choose your weapon: 1 for Sword; 2 for Staff");
weapon = scan.nextInt();
if (weapon == 1 || weapon == 2) {
break;
} else {
System.out.println(weapon + " is not a valid choice");
}
} else if(hero == 2) {
System.out.println("Choose your weapon: 2 for Staff");
weapon = scan.nextInt();
if(weapon == 2) {
break;
}else {
System.out.println(weapon + " is not a valid choice");
}
}
}
return weapon;
}
Then in your main:
int hero = queryHero();
int travelOption = queryTravelOptionForHero(hero);
int weapon = queryWeaponForHero(hero);
System.out.println("hero: " + hero);
System.out.println("travelOption: " + travelOption);
System.out.println("weapon: " + weapon);
Note: I am not sure if you know about them, but there are ways to make this code nicer using enums and Lists
Your flow can be written as a simple procedural code. As so, I would write it in the most simple form I can - at least as a start.
There is no real justification for using switch labels and loops inside loops
Just write 3 simple loops, one after another - It will be simple to read, understand and debug.
I dont want to write the code for you, it is not the purpose of this site, but here's a Pseudo code:
Loop 1 (selecting heroes)
If(heroes != Warrior)
Loop 2 (selecting travel)
else travel=Horse
Loop 3 (selecing weapon)
Like the comments suggest i would not go with loops inside loops. Instead you should assign the variables one at the time. I have written a helper method selectVariable(String description, String varOne, String varTwo) that you can use to assign variables and give you a start for your story game. You could expand it if you want to give the user more choices. Also don't give the use the illusion a choice can be made, if there is no choice in that situation.
Here is the code that should do what you want:
import java.util.Scanner;
public class Story {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int hero = chooseHero();
int travel = chooseTravel(hero);
int weapon = chooseWeapon(hero);
//For printing purposes give your choice their respective string name again.
String heroDesc = hero == 1 ? "Warrior" : "Mage";
String travelDesc = travel == 1 ? "Horse" : "Teleportation";
String weaponDesc = weapon == 1 ? "Sword" : "Staff";
System.out.printf("you are a %s, traveling by %s, wielding a %s" + System.lineSeparator(), heroDesc, travelDesc, weaponDesc);
}
private static int chooseHero() {
return selectVariable("choose your hero class", "warrior", "mage");
}
private static int chooseTravel(int hero) {
if (hero == 1) { // if the user has no choice don't give the illusion a choice can be made
System.out.println("you are a Warrior you will travel by horse");
return 1;
} else {
return selectVariable("choose your way of travel", "by horse", "teleportation");
}
}
private static int chooseWeapon(int hero) {
if (hero == 2) {
System.out.println("you are a mage you will wield a staff");
return 2;
} else {
return selectVariable("choose your weapon", "sword", "staff");
}
}
//you can reuse this method to also assign other traits to your story
private static int selectVariable(String description, String varOne, String varTwo) {
int var;
do {
System.out.printf("%s: 1 %s, 2 for %s" + System.lineSeparator(), description, varOne, varTwo);
var = scan.nextInt();
switch (var) {
case 1:
System.out.printf("you have chosen %s" + System.lineSeparator(), varOne);
return var;
case 2:
System.out.printf("you have chosen %s" + System.lineSeparator(), varTwo);
return var;
default:
System.out.println(var + " is an invalid choice");
}
}
while (true);
}
}
I need to make atm machine for school project.
I finished all and all is working fine, and i make the validation for the pin because it is string. So my problem is how to make validation for all other methods to check if anything else but numbers is entered to say the user that is wrong and to return him on the start of that method. All variables are stored into array as integers.
So here is my code please help i tried so many things and i cant make it work..
public class Banka {
static Scanner skener = new Scanner(System.in);
static String pin[] = {"1234","2345","3456","4567","5678"};
static String username[] = {"Mate","Nathan","John","Michelle","Angelina"};
static int balance[] = {200,100,250,150,300};
static boolean overdraft[] = {true,true,false,true,false};
static int index;
public static void main(String[] args) {
login();
}
public static void login() {
Date datum = new Date();
System.out.println("" +datum);
System.out.println("------------------------------");
System.out.println("Welcome to the Illuminati Bank \n Please log in with your PIN");
String login = skener.next();
checkpin(login);
for (int i = 0; i< pin.length; i++) {
if (login.matches(pin[i])) {
index = i;
System.out.println("\nWelcome " + username[index]);
Menu();
}
}
System.out.println("Wrong PIN entered, please login again \n");
login();
}
public static void Menu() {
System.out.println("Please select an option");
System.out.println("\n 1.View Bank Statement \n 2.Deposit \n 3.Withdraw \n 4.Change Pin \n 5.Exit \n");
int choice = skener.nextInt();
switch (choice) {
case 1: statement();
break;
case 2: deposit();
break;
case 3: withdraw();
break;
case 4: change();
break;
case 5: exit();
break;
default: System.out.println("Incorrect Choice ");
Menu();
}
}
public static void statement() {
switch(index) {
case 0: case 1: case 2: case 3: case 4:
System.out.println("" +username[index]+ ", your balance is: " +balance[index]+ "€");
if (overdraft[index] == true) {
System.out.println("You are entitled to overdraft");
}
else {
System.out.println("You are NOT entitled to overdraft");
}
Menu();
}
}
public static void deposit() {
System.out.println("" +username[index]+ ", how much you wish to deposit?");
int deposit = skener.nextInt();
balance[index] = balance[index] + deposit;
System.out.println("Thank you, you deposited " +deposit+ "€, now you have " +balance[index]+ "€ total");
depositm();
}
public static void depositm(){
System.out.println("\n 1.Deposit more \n 2.Exit to menu");
int more = skener.nextInt();
switch (more) {
case 1: deposit();
break;
case 2: Menu();
default: System.out.println("Wrong choice, please choose again");
depositm();
}
}
public static void withdraw() {
System.out.println("" +username[index]+ ", how much you wish to withdraw?");
int withdraw = skener.nextInt();
if (overdraft[index] == true) {
balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
if(overdraft[index] == false && balance[index] >= withdraw)
{balance[index] = balance[index] - withdraw;
System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
Menu();
}
else {
System.out.println("You have insufficient funds \nPlease try again");
withdraw();
}
}
public static void change() {
System.out.println("" +username[index]+ ", do you want to change your pin?");
System.out.println("Press 1 to change or 2 to exit to menu");
int change = skener.nextInt();
switch (change) {
case 1: System.out.println("Please enter new PIN");
pin[index] = skener.next();
System.out.println("You successfully changed your PIN");
Menu();
case 2: System.out.println("Your PIN remains unchanged");
Menu();
default: System.out.println("Wrong choice, please choose again");
change();
}
}
public static void exit(){
System.out.println("Goodbye " +username[index]+ ", Illuminati Bank wish you all the best \n");
login();
}
public static int checkpin(String x){
while(!x.matches("\\d{4}")){
System.err.println("\n Error.\n Please enter 4 digit pin.");
login();
}
return 0;
}
}
so if any one can help me how to validate all other methods with user inputs where is INTs that would be great.
Try this
String input = "xxxx";
int pin;
try{
pin = Integer.parseInt(input);
}catch(NumberFormatException e){
// input contains letters or symbols.
}
Or here's another one using the Character class.
String input = "xxxx";
boolean allDigits = true;
for(char ch: input.toCharArray()){
if(!Character.isDigit(ch)) {
allDigits = false;
break;
}
}
if(allDigits){
// input contains only digits.
}
Edit: Answering this comment.
You can modify your method like this,
public static void checkpin(String x) {
if (x.length() == 4) {
try {
Integer.parseInt(x);
login();
} catch (NumberFormatException e) {
System.err.println("\n Error.\n Invalid pin.");
}
} else {
System.err.println("\n Error.\n Please enter 4 digit pin.");
}
}
this way the method login() is called only if the pin has 4 digits and all the digits are numbers.
I have a game that's running perfectly. I want to put a line of code that asks the player if they want to play again at the end of the game. I would also like to keep a score system for every player and computer win.
I'm having trouble with the input = Integer.parseInt(sc.nextInt()); line
import java.util.Scanner;
public class Sticks {
public static boolean whoStart(String choice) {
int ran = (int) (Math.random() * 2 + 1);
String ht = "";
switch (ran) {
case 1:
ht = "head";
break;
case 2:
ht = "tails";
}
if (ht.equals(choice.toLowerCase())) {
System.out.println("you start first");
return true;
} else {
System.out.println("computer starts first");
return false;
}
}
public static int playerTurn(int numberOfSticks) {
System.out.println(" \nthere are " + numberOfSticks + " sticks ");
System.out.println("\nhow many sticks do you wanna take? 1 or 2?");
Scanner in = new Scanner(System.in);
int sticksToTake = in.nextInt();
while ((sticksToTake != 1) && (sticksToTake != 2)) {
System.out.println("\nyou can only take 1 or 2 sticks");
System.out.println("\nhow many sticks do you wanna take?");
sticksToTake = in.nextInt();
}
numberOfSticks -= sticksToTake;
return numberOfSticks;
}
public static int computerTurn(int numberOfSticks) {
int sticksToTake;
System.out.println("\nthere are " + numberOfSticks + " sticks ");
if ((numberOfSticks - 2) % 3 == 0 || (numberOfSticks - 2 == 0)) {
sticksToTake = 1;
numberOfSticks -= sticksToTake;
} else {
sticksToTake = 2;
numberOfSticks -= sticksToTake;
}
System.out.println("\ncomputer took " + sticksToTake + " stick ");
return numberOfSticks;
}
public static boolean checkWinner(int turn, int numberOfSticks) {
int score = 0;
int input;
int B = 1;
int Y=5, N=10;
if ((turn == 1) && (numberOfSticks <= 0)) {
System.out.println("player lost");
return true;
}
if ((turn == 2) && (numberOfSticks <= 0)) {
System.out.println("player won");
score++;
return true;
}
System.out.println("Your score is "+ score);
System.out.println("Do you want to play again? Press (5) for Yes / (10) for No");
// ----- This line -----
input = Integer.parseInt(sc.nextInt());
if (input == Y) {
B = 1;
System.out.println("Rock, Paper, Scissors");
} else if (input == N) {
System.exit(0);
System.out.println("Have A Good Day!");
}
}
public static void main(String args[]) {
int turn;
int numberOfSticks = 21;
Scanner in = new Scanner(System.in);
System.out.println("choose head or tails to see who starts first");
String choice = in.next();
if (whoStart(choice) == true) {
do {
turn = 1;
numberOfSticks = playerTurn(numberOfSticks);
if (checkWinner(turn, numberOfSticks) == true) {
break;
};
turn = 2;
numberOfSticks = computerTurn(numberOfSticks);
checkWinner(turn, numberOfSticks);
} while (numberOfSticks > 0);
} else {
do {
turn = 2;
numberOfSticks = computerTurn(numberOfSticks);
if (checkWinner(turn, numberOfSticks) == true) {
break;
};
turn = 1;
numberOfSticks = playerTurn(numberOfSticks);
checkWinner(turn, numberOfSticks);
} while (numberOfSticks > 0);
}
}
}
The title of your question almost answered you what you need to add: a loop!
I suggest you to refactor your function main and extract all your game logic from it to be stored within a dedicated function for the sake of the readability. Let's call it startGame().
Your main is going to become shorter and can represent a good location to introduce this loop, such as:
public static void main(String[] a) {
boolean isPlaying = true;
Scanner in = new Scanner(System.in);
while(isPlaying) {
startGame();
// Your message to continue or stop the game
if(in.next().equalsIgnoreCase("No")) {
isPlaying = false;
}
}
}
I recommend you to use a boolean that is checked in your while loop rather than using a break statement, as it brings a better control flow in your application.
Just put everything in a while(true) loop and use a break; if they choose no. Something like:
static int playerPoints = 0;
public static void main(String args[]) {
int turn;
int numberOfSticks = 21;
Scanner in = new Scanner(System.in);
while(true){
...
System.out.println("You have " + playerPoints + " points!")
System.out.println("Do you want to play again?");
if (!in.nextLine().toLowerCase().equals("yes")){
break;
}
}
}
Edit: ZenLulz's answer is better than this one, mainly because it encourages better programming practice. Mine works but isn't the best way to solve the issue.
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.