Java game - Balance() method not updating

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 988 Views - Last Post: 29 September 2018 - 02:45 AM Rate Topic: -----

#1 JohnH222   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-September 18

Java game - Balance() method not updating

Posted 20 September 2018 - 06:28 PM

The code is fully functional with no errors, however, my balance method is not updating with the inputs that the user had entered. Could someone please take a look and tell me what may have missed or done wrong? Thanks in advance.

// Craps.java
// Craps class simulates the dice game craps.
import java.util.Random;
import java.util.Scanner;

public class Craps {
  // Create random number
  private static final Random randomNumbers = new Random();

  // Emurate
  private enum Status { CONTINUE, WON, LOST };

  // Constants
  private static final int SNAKE_EYES = 2;
  private static final int TREY = 3;
  private static final int SEVEN = 7;
  private static final int YO_LEVEN = 11;
  private static final int BOX_CARS = 12;

  // One game of Craps
  public static void main( String[] args ) {
    int myPoint = 0;
    Status gameStatus;

    int balance = 1000; // Initialize Bank Balance
    int wager = 0;
    chatter(balance);
    wager(balance);

    int sumOfDice = rollDice();

    switch (sumOfDice) {
      case SEVEN:
      case YO_LEVEN:
        gameStatus = Status.WON;
        break;
      case SNAKE_EYES:
      case TREY:
      case BOX_CARS:
        gameStatus = Status.LOST;
        break;
      default:
        gameStatus = Status.CONTINUE;
        myPoint = sumOfDice;
        System.out.printf("Point is %d\n", myPoint);
        break;
    }

    while (gameStatus == Status.CONTINUE) {
      sumOfDice = rollDice();

      if (sumOfDice == myPoint)
        gameStatus = Status.WON;
      else if (sumOfDice == SEVEN)
        gameStatus = Status.LOST;
    }

    if (gameStatus == Status.WON)
      System.out.println("Player wins");
    else
      System.out.println("Player loses");

    balance(wager);
  }

  public static int rollDice() {
    int die1 = 1 + randomNumbers.nextInt(6);
    int die2 = 1 + randomNumbers.nextInt(6);

    int sum = die1 + die2;

    System.out.printf("Player rolled %d + %d = %d\n", die1, die2, sum);

    return sum;
  }

  // Chatter
  private static void chatter(int balance) {
    if(balance > 0) {
      String [] array = {"Oh, you're going for broke, huh?", "Aw c'mon, take a chance!", "You're up big. Now's the time to cash in your chips!"};
      Random r = new Random();
      int randomChatter = r.nextInt(array.length);
      System.out.println(array[randomChatter]);
    }

    if(balance <= 0) {
      System.out.println("Sorry. You busted!");
      System.out.println("The program will now terminate.");
      System.exit(0);
    }
  }

  // Wager
  private static void wager(int balance) {
    int wager = 0;
    while(wager == 0) {
      System.out.printf("How much do you want to wager: ");

      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }

    if(wager <= balance) {
      System.out.println("Thank you for your wager.");
    }

    while(wager > balance) {
      System.out.printf("Enter a valid wager: ");
      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }
  }

  // Balance
  private static void balance(int wager) {
    int balance = 1000;
    balance = balance - wager;

    System.out.println("Your balance is: " + balance);
  }
}





Is This A Good Question/Topic? 0
  • +

Replies To: Java game - Balance() method not updating

#2 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 06:40 PM

The reason is because wager is always assigned as 0 in line 26. So it will pass 0 to balance function.
The solution is return wager value in wager function
int getWager = wager(balance);

then pass the returned value to balance method.
balance(getWager);

This post has been edited by wseng92: 20 September 2018 - 07:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 JohnH222   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-September 18

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 06:57 PM

This is what I currently have:

// Craps.java
// Craps class simulates the dice game craps.
import java.util.Random;
import java.util.Scanner;

public class Craps {
  // Create random number
  private static final Random randomNumbers = new Random();

  // Emurate
  private enum Status { CONTINUE, WON, LOST };

  // Constants
  private static final int SNAKE_EYES = 2;
  private static final int TREY = 3;
  private static final int SEVEN = 7;
  private static final int YO_LEVEN = 11;
  private static final int BOX_CARS = 12;

  // One game of Craps
  public static void main( String[] args ) {
    int myPoint = 0;
    Status gameStatus;

    int balance = 1000; // Initialize Bank Balance
    int wager = 0;
    chatter(balance);
    wager(balance);

    int sumOfDice = rollDice();

    switch (sumOfDice) {
      case SEVEN:
      case YO_LEVEN:
        gameStatus = Status.WON;
        break;
      case SNAKE_EYES:
      case TREY:
      case BOX_CARS:
        gameStatus = Status.LOST;
        break;
      default:
        gameStatus = Status.CONTINUE;
        myPoint = sumOfDice;
        System.out.printf("Point is %d\n", myPoint);
        break;
    }

    while (gameStatus == Status.CONTINUE) {
      sumOfDice = rollDice();

      if (sumOfDice == myPoint)
        gameStatus = Status.WON;
      else if (sumOfDice == SEVEN)
        gameStatus = Status.LOST;
    }

    if (gameStatus == Status.WON)
      System.out.println("Player wins");
    else
      System.out.println("Player loses");

    balance(balance, wager);
  }

  public static int rollDice() {
    int die1 = 1 + randomNumbers.nextInt(6);
    int die2 = 1 + randomNumbers.nextInt(6);

    int sum = die1 + die2;

    System.out.printf("Player rolled %d + %d = %d\n", die1, die2, sum);

    return sum;
  }

  // Chatter
  private static void chatter(int balance) {
    if(balance > 0) {
      String [] array = {"Oh, you're going for broke, huh?", "Aw c'mon, take a chance!", "You're up big. Now's the time to cash in your chips!"};
      Random r = new Random();
      int randomChatter = r.nextInt(array.length);
      System.out.println(array[randomChatter]);
    }

    if(balance <= 0) {
      System.out.println("Sorry. You busted!");
      System.out.println("The program will now terminate.");
      System.exit(0);
    }
  }

  // Wager
  private static void wager(int balance) {
    int wager = 0;
    while(wager == 0) {
      System.out.printf("How much do you want to wager: ");

      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }

    if(wager <= balance) {
      System.out.println("Thank you for your wager.");
    }

    while(wager > balance) {
      System.out.printf("Enter a valid wager: ");
      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }
  }

  // Balance
  private static void balance(int balance, int wager) {
    balance = balance - wager;

    System.out.println("Your balance is: " + balance);
  }

  // public static boolean balanceBoolean(int balance, int wager) {
  //   if(balance == 0) {
  //     return true;
  //   } else if(balance - wager > 0) {
  //     return true;
  //   } else {
  //     return false;
  //   }
  // }

}



I'm a bit confused. I hope I have this right. keep in mind in initializing balance towards the top of the page to 1000.

So: wager(balance);
Should be: int wager = wager(balance);

And: balance(wager);
Should be: balance(wager);

So your saying I should replace them or are these completely different instances.
Was This Post Helpful? 0
  • +
  • -

#4 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 07:03 PM

View PostJohnH222, on 21 September 2018 - 01:57 AM, said:

So your saying I should replace them or are these completely different instances.

Replace them.

This post has been edited by wseng92: 20 September 2018 - 07:04 PM

Was This Post Helpful? 1
  • +
  • -

#5 JohnH222   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-September 18

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 07:14 PM

I'm getting an error. I'm new to this and following along with the textbook his hard to do with tiny font.

javac Craps.java
Craps.java:26: error: incompatible types: void cannot be converted to int
    int wager = wager(balance);
                     ^
1 error


// Craps.java
// Craps class simulates the dice game craps.
import java.util.Random;
import java.util.Scanner;

public class Craps {
  // Create random number
  private static final Random randomNumbers = new Random();

  // Emurate
  private enum Status { CONTINUE, WON, LOST };

  // Constants
  private static final int SNAKE_EYES = 2;
  private static final int TREY = 3;
  private static final int SEVEN = 7;
  private static final int YO_LEVEN = 11;
  private static final int BOX_CARS = 12;

  // One game of Craps
  public static void main( String[] args ) {
    int myPoint = 0;
    Status gameStatus;

    int balance = 1000; // Initialize Bank Balance
    int wager = wager(balance);
    chatter(balance);
    wager(balance);

    int sumOfDice = rollDice();

    switch (sumOfDice) {
      case SEVEN:
      case YO_LEVEN:
        gameStatus = Status.WON;
        break;
      case SNAKE_EYES:
      case TREY:
      case BOX_CARS:
        gameStatus = Status.LOST;
        break;
      default:
        gameStatus = Status.CONTINUE;
        myPoint = sumOfDice;
        System.out.printf("Point is %d\n", myPoint);
        break;
    }

    while (gameStatus == Status.CONTINUE) {
      sumOfDice = rollDice();

      if (sumOfDice == myPoint)
        gameStatus = Status.WON;
      else if (sumOfDice == SEVEN)
        gameStatus = Status.LOST;
    }

    if (gameStatus == Status.WON)
      System.out.println("Player wins");
    else
      System.out.println("Player loses");

    balance(balance, wager);
  }

  public static int rollDice() {
    int die1 = 1 + randomNumbers.nextInt(6);
    int die2 = 1 + randomNumbers.nextInt(6);

    int sum = die1 + die2;

    System.out.printf("Player rolled %d + %d = %d\n", die1, die2, sum);

    return sum;
  }

  // Chatter
  private static void chatter(int balance) {
    if(balance > 0) {
      String [] array = {"Oh, you're going for broke, huh?", "Aw c'mon, take a chance!", "You're up big. Now's the time to cash in your chips!"};
      Random r = new Random();
      int randomChatter = r.nextInt(array.length);
      System.out.println(array[randomChatter]);
    }

    if(balance <= 0) {
      System.out.println("Sorry. You busted!");
      System.out.println("The program will now terminate.");
      System.exit(0);
    }
  }

  // Wager
  private static void wager(int balance) {
    int wager = 0;
    while(wager == 0) {
      System.out.printf("How much do you want to wager: ");

      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }

    if(wager <= balance) {
      System.out.println("Thank you for your wager.");
    }

    while(wager > balance) {
      System.out.printf("Enter a valid wager: ");
      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }
  }

  // Balance
  private static void balance(int balance, int wager) {
    balance = balance - wager;

    System.out.println("Your balance is: " + balance);
  }

  // public static boolean balanceBoolean(int balance, int wager) {
  //   if(balance == 0) {
  //     return true;
  //   } else if(balance - wager > 0) {
  //     return true;
  //   } else {
  //     return false;
  //   }
  // }

}


Was This Post Helpful? 0
  • +
  • -

#6 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 07:18 PM

Return type for wager function should be int, not void.
private static int wager(int balance) {
           ......       
        return wager;
  }


More info about return method can be found here

This post has been edited by wseng92: 20 September 2018 - 07:23 PM

Was This Post Helpful? 1
  • +
  • -

#7 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 07:29 PM

wager(balance);
at line 28 in not needed since you already have
int wager = wager(balance);
at line 26.
Was This Post Helpful? 1
  • +
  • -

#8 JohnH222   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-September 18

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 07:32 PM

View Postwseng92, on 20 September 2018 - 07:29 PM, said:

wager(balance);
at line 28 in not needed since you already have
int wager = wager(balance);
at line 26.


Removing this fixed so many errors I was getting. Thank you for spotting this! It works as intended now.
Was This Post Helpful? 1
  • +
  • -

#9 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 07:34 PM

That's a pleasure :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#10 JohnH222   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-September 18

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 07:58 PM

I'm trying to figure out how to display a boolean under all my output at the end.

public static boolean balanceOverloaded(int balance, int wager) {
    if(balance == 0) {
      return true;
    } else if(balance - wager > 0) {
      return true;
    } else {
      return false;
    }
  }



How would I go about this?

I've already got

balanceOverloaded(balance, wager);


The code compiles but it doesn't display anything.
Was This Post Helpful? 0
  • +
  • -

#11 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 08:03 PM

What you want it to displayed ? Did you add System.out.print in balanceOverloaded method ?

This post has been edited by wseng92: 20 September 2018 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#12 JohnH222   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-September 18

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 08:10 PM

View Postwseng92, on 20 September 2018 - 08:03 PM, said:

What you want it to displayed ? Did you add System.out.print in balanceOverloaded method ?


I've tried but i'm currently trying to figure out how to make the boolean display.
Was This Post Helpful? 0
  • +
  • -

#13 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 08:11 PM

Post the latest code please
Was This Post Helpful? 0
  • +
  • -

#14 JohnH222   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-September 18

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 08:15 PM

I need to name the boolean return statements, don't I? then call to print the statement by name.

View Postwseng92, on 20 September 2018 - 08:11 PM, said:

Post the latest code please

// Craps.java
// Craps class simulates the dice game craps.
import java.util.Random;
import java.util.Scanner;

public class Craps {
  // Create random number
  private static final Random randomNumbers = new Random();

  // Emurate
  private enum Status { CONTINUE, WON, LOST };

  // Constants
  private static final int SNAKE_EYES = 2;
  private static final int TREY = 3;
  private static final int SEVEN = 7;
  private static final int YO_LEVEN = 11;
  private static final int BOX_CARS = 12;

  // One game of Craps
  public static void main( String[] args ) {
    int myPoint = 0;
    Status gameStatus;

    int balance = 1000; // Initialize Bank Balance
    int wager = wager(balance);
    chatter(balance);

    int sumOfDice = rollDice();

    switch (sumOfDice) {
      case SEVEN:
      case YO_LEVEN:
        gameStatus = Status.WON;
        break;
      case SNAKE_EYES:
      case TREY:
      case BOX_CARS:
        gameStatus = Status.LOST;
        break;
      default:
        gameStatus = Status.CONTINUE;
        myPoint = sumOfDice;
        System.out.printf("Point is %d\n", myPoint);
        break;
    }

    while (gameStatus == Status.CONTINUE) {
      sumOfDice = rollDice();

      if (sumOfDice == myPoint)
        gameStatus = Status.WON;
      else if (sumOfDice == SEVEN)
        gameStatus = Status.LOST;
    }

    if (gameStatus == Status.WON)
      System.out.println("Player wins");
    else
      System.out.println("Player loses");

    balance(balance, wager);
    balanceOverloaded(balance, wager);
  }

  public static int rollDice() {
    int die1 = 1 + randomNumbers.nextInt(6);
    int die2 = 1 + randomNumbers.nextInt(6);

    int sum = die1 + die2;

    System.out.printf("Player rolled %d + %d = %d\n", die1, die2, sum);

    return sum;
  }

  // Chatter
  private static void chatter(int balance) {
    if(balance > 0) {
      String [] array = {"Oh, you're going for broke, huh?", "Aw c'mon, take a chance!", "You're up big. Now's the time to cash in your chips!"};
      Random r = new Random();
      int randomChatter = r.nextInt(array.length);
      System.out.println(array[randomChatter]);
    }

    if(balance <= 0) {
      System.out.println("Sorry. You busted!");
      System.out.println("The program will now terminate.");
      System.exit(0);
    }
  }

  // Wager
  private static int wager(int balance) {
    int wager = 0;
    while(wager == 0) {
      System.out.printf("How much do you want to wager: ");

      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }

    if(wager <= balance) {
      System.out.println("Thank you for your wager.");
    }

    while(wager > balance) {
      System.out.printf("Enter a valid wager: ");
      Scanner input = new Scanner(System.in);
      wager = input.nextInt();
      System.out.println("Your current wager: " + wager);
    }
    return wager;
  }

  // Balance
  private static void balance(int balance, int wager) {
    balance = balance - wager;

    System.out.println("Your balance is: " + balance);
  }

  public static boolean balanceOverloaded(int balance, int wager) {
    if(balance == 0) {
      overloaded = true;
    } else if(balance - wager > 0) {
      overloaded = true;
    } else {
      overloaded = false;
    }
    System.out.println(overloaded);
  }

}


Was This Post Helpful? 0
  • +
  • -

#15 wseng92   User is offline

  • D.I.C Addict

Reputation: 33
  • View blog
  • Posts: 535
  • Joined: 23-September 15

Re: Java game - Balance() method not updating

Posted 20 September 2018 - 08:16 PM

You can simply achieve by this
boolean theBalance = balanceOverloaded(balance, wager);
System.out.print(theBalance);
The output will be either true or false.
Modify your boolean method to this
public static boolean balanceOverloaded(int balance, int wager) {
    if(balance == 0) {
       return true;
    } else if(balance - wager > 0) {
      return true;
    } else {
      return false;
    }
  }

This post has been edited by wseng92: 20 September 2018 - 08:21 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2