Billy Yin
Billy Yin

Reputation: 11

Different methods of printing returning different values for the same variables?

I wrote a simple program to simulate a game of craps, everything seems to be working well except for the fact that I noticed that my "scoreboard" would be returning different values depending on the methods I'm using to print it.

Printing the "wins" variable with print statement returns the correct results But printing the "wins" variable through another formatted string "status" returns lower values. I know I'm missing something here as I haven't been programming for long but I'm quite stumped as to how this could happen. Any feedback is greatly appreciated.

public class RandomSumGame {

    public static void main(String[] args) {
        // TODO Auto-generated method stub\
        RandomSumGame test = new RandomSumGame();
        test.play();
       }


    boolean start;
    int d1;
    int d2;
    int sum;
    int valuePoint;
    int wins;
    int loss;
    String status;

    public void play(int d1, int d2) 
    {
        status= String.format("printing through variable - wins : %d | loss : %d \n", wins, loss );

        if (sum == 11 || sum == 7) 
        {
            System.out.println("Natural - You Win!");
            wins = wins + 1;
        }

    else if (sum == 2 || sum == 3 || sum == 12) {
        System.out.println("Craps! - You lose!");
        loss = loss + 1;

    }

    else {
        valuePoint = sum;

        System.out.printf("You set the value point of %d = %d + %d \n", valuePoint, d1, d2);
        while (true) {
            rollDice();
            if (sum == valuePoint) {
                System.out.println("YOU WIN!");
                wins = wins + 1;

                break;
            }

            else if (sum == 7) {
                System.out.println("YOU LOSE!");
                loss = loss + 1;

                break;

            }

            else {
                System.out.println("ROLLING DICE AGAIN!");
                continue;
            }

        }
    }

    System.out.printf("Straight up printing - wins : %d | loss : %d \n", wins, loss );
    System.out.println(status);

}

public void play() {
    int round = 1;
    start = true;

    while (start == true){
        System.out.println("Round : " + round);
        round +=1;

        rollDice();

        play(d1, d2);
        //System.out.println(status);
        if (round >3) {
            start = false;
        }
    }


}

public void rollDice() {
    d1 = (int) (Math.random() * 6 + 1);
    d2 = (int) (Math.random() * 6 + 1);
    sum = d1 + d2;
    System.out.printf("YOU ROLL THE DICE! - sum is: %d , Dice 1: %d , Dice 2: %d\n", sum, d1, d2);

}

}

Here is the sample output in the console, as you can see they return different results.

Round : 1

YOU ROLL THE DICE! - sum is: 7 , Dice 1: 3 , Dice 2: 4

Natural - You Win!

Straight up printing - wins : 1 | loss : 0

printing through variable - wins : 0 | loss : 0

Round : 2

YOU ROLL THE DICE! - sum is: 11 , Dice 1: 5 , Dice 2: 6

Natural - You Win!

Straight up printing - wins : 2 | loss : 0

printing through variable - wins : 1 | loss : 0

Round : 3

YOU ROLL THE DICE! - sum is: 10 , Dice 1: 4 , Dice 2: 6

You set the value point of 10 = 4 + 6

YOU ROLL THE DICE! - sum is: 6 , Dice 1: 1 , Dice 2: 5

ROLLING DICE AGAIN!

YOU ROLL THE DICE! - sum is: 8 , Dice 1: 6 , Dice 2: 2

ROLLING DICE AGAIN!

YOU ROLL THE DICE! - sum is: 4 , Dice 1: 2 , Dice 2: 2

ROLLING DICE AGAIN!

YOU ROLL THE DICE! - sum is: 10 , Dice 1: 6 , Dice 2: 4

YOU WIN!

Straight up printing - wins : 3 | loss : 0

printing through variable - wins : 2 | loss : 0

Upvotes: 0

Views: 273

Answers (3)

Nipuna Priyamal
Nipuna Priyamal

Reputation: 368

You assign string value to the status variable before you roll the dice. Then the no of win and loss times will not be updated in status string. In order to correct move status= String.format("printing through variable - wins : %d | loss : %d \n", wins, loss ); just before System.out.println(status); line as following

enter image description here

Upvotes: 0

Kdaydin
Kdaydin

Reputation: 455

Just move this line to the end of your function before print. status= String.format("printing through variable - wins : %d | loss : %d \n", wins, loss ); You were creating string with values of pre-processed values of win,loss.

Upvotes: 0

Thiyagu
Thiyagu

Reputation: 17900

Strings are immutable in Java. Once you create a string, the value of that can never be changed.

So, here you are creating status String that has the values of wins and loss.

status = String.format("printing through variable - wins : %d | loss : %d \n", 
        wins, loss );

Then you change the wins value. Now, you cannot expect the status String value to reflect the current value of wins or loss.

You have to create a new string with the latest values.

Upvotes: 3

Related Questions