0

I'm just messing around in Java a little bit and I created a little console program. Everything is working fine until the last step, the integer bankValue always stays at 1000 when it should be changed depending on whether you lose or win. Would appriciate if someone could explain what may be causing this mess and maybe give me some feedback on my code.

Here's the source code: http://ideone.com/Kl1FsB

//Dice game created 2015-08-29
import java.util.Scanner;
import java.util.Random;
public class cantSleep {
    //----------How the program should work-------------------//
    //Create a dice program which lets the user choose between h/l (High/Low)
    //The dice is then rolled and depending on which number it lands on there are different
    //actions.
    // l = 1-3, h = 4-6
    //The user should start with a bank value of a 1000 marks.
    //For each win the amount of the bet should be doubled.
    //After a win/loss the user should be prompted again, asking if they want to play again.

    //----------How to create the program --------------------//
    //Create a boolean variable for the loop so the user can be prompted after one run.
    //Create one integer for the bank value and one for the dice.
    //Create a char and set make it so the Scanner takes in values for either 'h' or 'l'
    //If the user gives any value other than the ones above prompt them to do it over again.
    //Create a random method which random's out the dice number.
    //Create an switch/case for the different outcomes on the dice number.

    //------Problems encountered----//
    //Everything works until the last step, whenever you win/lose
    //the bankValue doesn't change.


    //Random number generator method.
    public static int randomGenerator(int startValue, int lastValue)
    {
        Random rand = new Random();

        int randomNum = rand.nextInt((lastValue-startValue) + 1) + startValue;
        return randomNum;
    }

    public static void diceWin(int bankValue, int bet, int dice, char choice, boolean runAgain)
    {
        Scanner userInput = new Scanner(System.in);

        bankValue += bet;
        System.out.println("Congratulations, the dice shows " + dice + " and you just won yourself " + bet + " $.");
        System.out.print("Would you like to bet again? (y/n): ");
        choice = userInput.nextLine().charAt(0);
        while(choice != 'y' && choice != 'n')
        {
            System.out.println("Please use the characters 'y' or 'n' next time.");
            System.out.print("Would you like to bet " + bet + "? (y/n): ");
            choice = userInput.next().charAt(0);
            if(choice == 'n')
            {
                runAgain = false;
            }
        }
    }

    public static void diceLoss(int bankValue, int bet, int dice, char choice, boolean runAgain)
    {
        Scanner userInput = new Scanner(System.in);

        bankValue -= bet;
        System.out.println("Sorry, the dice shows " + dice + " and you just lost " + bet + " $.");
        System.out.print("Would you like to bet again? (y/n): ");
        choice = userInput.nextLine().charAt(0);
        while(choice != 'y' && choice != 'n')
        {
            System.out.println("Please use the characters 'y' or 'n' next time.");
            System.out.print("Would you like to bet " + bet + "? (y/n): ");
            choice = userInput.next().charAt(0);
            if(choice == 'n')
            {
                runAgain = false;
            }
        }
    }

    public static void main(String[] args)
    {
        Scanner userInput = new Scanner(System.in);

        //Variable declaration
        boolean runAgain = true, low = true;
        int bankValue = 1000, dice = 0, bet = 0;
        char choice;

        //Start of the program
        System.out.println("Hello and welcome to the dice machine.");
        while(runAgain == true)
        {
            System.out.println("Your current balance is: " + bankValue + " $.");
            //User input, if the bet amount is bigger than the balance it gives an error message out.
            System.out.print("How much would you like to bet?: ");
            bet = userInput.nextInt();
            while(bet > bankValue)
            {
                System.out.print("You do not have that much money on your bank account, please try again: ");
                bet = userInput.nextInt();
            }
            //Prompts the user for a verification of the bet.
            //If anything but 'y' and 'n' is typed in it gives an error message out.
            //If the user types a 'y' in the program continues to run, if they type an 'n' in the program closes.
            System.out.print("Do you really want to bet " + bet + "? (y/n): ");
            choice = userInput.next().charAt(0);
            while(choice != 'y' && choice != 'n')
            {
                System.out.println("Please use the characters 'y' or 'n' next time.");
                System.out.print("Would you like to bet " + bet + "? (y/n): ");
                choice = userInput.next().charAt(0);
                if(choice == 'n')
                {
                    System.exit(0);
                }
            }
            //Asks the user what he/she would like to put the bet on. (h/l)
            System.out.print("What would you like to put " + bet + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
            choice = userInput.next().charAt(0);
            while(choice != 'h' && choice != 'l')
            {
                System.out.println("Please use the characters 'h' or 'l' next time.");
                System.out.print("What would you like to put " + bet + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
                choice = userInput.next().charAt(0);
            }

            //Executing the randomGenerator method.
            dice = randomGenerator(1, 6);

            //Giving the dice numbers boolean values.
            switch(dice)
            {
            case 1:
                low = true;
                break;
            case 2:
                low = true;
                break;
            case 3:
                low = true;
                break;
            case 4:
                low = false;
                break;
            case 5:
                low = false;
                break;
            case 6:
                low = false;
                break;
            default:
                System.out.println("Error, dice gave the number " + dice);
            }

            if((low == true && choice == 'l') || (low == false && choice == 'h'))
            {
                diceWin(bankValue, bet, dice, choice, runAgain);
            } else if((low == true && choice == 'h') || (low == false && choice == 'l')) {
                diceLoss(bankValue, bet, dice, choice, runAgain);
            } else {
                System.out.println("Something went horribly wrong, please check the source code.");
            }

        }
        userInput.close();

    }




}
kemkoi
  • 53
  • 1
  • 2
  • 9

3 Answers3

1

First note that Java is pass by value.

So in your diceLoss or diceWin method the value of bankValue will not be reflected in your actual variable. You need to return the value from the method and store it or you can declare bankValue as a class level variable and don't pass it in method.

For Example :

public class Test {

    public static void printNumber(int number) {
        number = 100;//Copy of actual variable number of main method
    }

    public static void main(String[] args) {
        int number = 25;
        printNumber(number);
        System.out.println(number);//Still print 25
    }

}
Community
  • 1
  • 1
akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    Ah, thanks. I knew it had to be something with the dice method's. It's working perfectly fine now :) – kemkoi Aug 29 '15 at 01:59
0

Your bankValue is local to main only. You might though of you are passing reference. In the current programm java not modifying that value in your diceWin and diceLoss, the variable you are modiying in win or loss is completely different than bankValue which is declared in main.

What you can do is, move your variable out of your main. Which is Class level and then modify it.

//Dice game created by Kemal 'kemkoi' Kojic 2015-08-29
import java.util.Scanner;
import java.util.Random;

public class cantSleep {
    // ----------How the program should work-------------------//
    // Create a dice program which lets the user choose between h/l (High/Low)
    // The dice is then rolled and depending on which number it lands on there
    // are different
    // actions.
    // l = 1-3, h = 4-6
    // The user should start with a bank value of a 1000 marks.
    // For each win the amount of the bet should be doubled.
    // After a win/loss the user should be prompted again, asking if they want
    // to play again.

    // ----------How to create the program --------------------//
    // Create a boolean variable for the loop so the user can be prompted after
    // one run.
    // Create one integer for the bank value and one for the dice.
    // Create a char and set make it so the Scanner takes in values for either
    // 'h' or 'l'
    // If the user gives any value other than the ones above prompt them to do
    // it over again.
    // Create a random method which random's out the dice number.
    // Create an switch/case for the different outcomes on the dice number.

    // ------Problems encountered----//
    // Everything works until the last step, whenever you win/lose
    // the bankValue doesn't change.

    private static int bankValue = 1000;

    // Random number generator method.
    public static int randomGenerator(int startValue, int lastValue) {
        Random rand = new Random();

        int randomNum = rand.nextInt((lastValue - startValue) + 1) + startValue;
        return randomNum;
    }

    public static void diceWin(int bet, int dice, char choice, boolean runAgain) {
        Scanner userInput = new Scanner(System.in);

        bankValue += bet;
        System.out.println("Congratulations, the dice shows " + dice
                + " and you just won yourself " + bet + " $.");
        System.out.print("Would you like to bet again? (y/n): ");
        choice = userInput.nextLine().charAt(0);
        while (choice != 'y' && choice != 'n') {
            System.out
                    .println("Please use the characters 'y' or 'n' next time.");
            System.out.print("Would you like to bet " + bet + "? (y/n): ");
            choice = userInput.next().charAt(0);
            if (choice == 'n') {
                runAgain = false;
            }
        }
    }

    public static void diceLoss(int bet, int dice, char choice, boolean runAgain) {
        Scanner userInput = new Scanner(System.in);

        bankValue -= bet;
        System.out.println("Sorry, the dice shows " + dice
                + " and you just lost " + bet + " $.");
        System.out.print("Would you like to bet again? (y/n): ");
        choice = userInput.nextLine().charAt(0);
        while (choice != 'y' && choice != 'n') {
            System.out
                    .println("Please use the characters 'y' or 'n' next time.");
            System.out.print("Would you like to bet " + bet + "? (y/n): ");
            choice = userInput.next().charAt(0);
            if (choice == 'n') {
                runAgain = false;
            }
        }
    }

    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);

        // Variable declaration
        boolean runAgain = true, low = true;
        int dice = 0, bet = 0;
        char choice;

        // Start of the program
        System.out.println("Hello and welcome to the dice machine.");
        while (runAgain == true) {
            System.out.println("Your current balance is: " + bankValue + " $.");
            // User input, if the bet amount is bigger than the balance it gives
            // an error message out.
            System.out.print("How much would you like to bet?: ");
            bet = userInput.nextInt();
            while (bet > bankValue) {
                System.out
                        .print("You do not have that much money on your bank account, please try again: ");
                bet = userInput.nextInt();
            }
            // Prompts the user for a verification of the bet.
            // If anything but 'y' and 'n' is typed in it gives an error message
            // out.
            // If the user types a 'y' in the program continues to run, if they
            // type an 'n' in the program closes.
            System.out.print("Do you really want to bet " + bet + "? (y/n): ");
            choice = userInput.next().charAt(0);
            while (choice != 'y' && choice != 'n') {
                System.out
                        .println("Please use the characters 'y' or 'n' next time.");
                System.out.print("Would you like to bet " + bet + "? (y/n): ");
                choice = userInput.next().charAt(0);
                if (choice == 'n') {
                    System.exit(0);
                }
            }
            // Asks the user what he/she would like to put the bet on. (h/l)
            System.out.print("What would you like to put " + bet
                    + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
            choice = userInput.next().charAt(0);
            while (choice != 'h' && choice != 'l') {
                System.out
                        .println("Please use the characters 'h' or 'l' next time.");
                System.out.print("What would you like to put " + bet
                        + " on, 'h' or 'l'? (l = 1-3, h = 4-6): ");
                choice = userInput.next().charAt(0);
            }

            // Executing the randomGenerator method.
            dice = randomGenerator(1, 6);

            // Giving the dice numbers boolean values.
            switch (dice) {
            case 1:
                low = true;
                break;
            case 2:
                low = true;
                break;
            case 3:
                low = true;
                break;
            case 4:
                low = false;
                break;
            case 5:
                low = false;
                break;
            case 6:
                low = false;
                break;
            default:
                System.out.println("Error, dice gave the number " + dice);
            }

            if ((low == true && choice == 'l')
                    || (low == false && choice == 'h')) {
                diceWin(bet, dice, choice, runAgain);
            } else if ((low == true && choice == 'h')
                    || (low == false && choice == 'l')) {
                diceLoss(bet, dice, choice, runAgain);
            } else {
                System.out
                        .println("Something went horribly wrong, please check the source code.");
            }

        }
        userInput.close();

    }
}

However you are correct in case of mutable objects. The rule of new copy being generated and modified is only for primitives and immutable classes.

For ex :

Look the below programm carefully

public class Main {
    public static void main(String[] args) {

        Person pe = new Person();
        pe.setName("so");
        int i=5;
        changeSomething(pe,i);
        System.out.println(pe.getName());// changeSo
        System.out.println(i);// 5


    }

    private static void changeSomething(Person pe, int i) {
        pe.setName("changeSo");i=10;

    }
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

When you pass an int variable to a function, the function works on a copy of that variable; you could instead have the function return the new value for it and assign the value returned by the function to the variable.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101