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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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: %dn", 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










share|improve this question































    0















    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: %dn", 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










    share|improve this question



























      0












      0








      0








      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: %dn", 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










      share|improve this question
















      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: %dn", 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







      java






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 4 at 5:40









      Nipuna Priyamal

      312114




      312114










      asked Jan 4 at 4:42









      Billy YinBilly Yin

      111




      111
























          3 Answers
          3






          active

          oldest

          votes


















          3














          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.






          share|improve this answer































            0














            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.






            share|improve this answer
























            • I feel like an idiot. Thanks!

              – Billy Yin
              Jan 4 at 5:03



















            0














            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






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033219%2fdifferent-methods-of-printing-returning-different-values-for-the-same-variables%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              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.






              share|improve this answer




























                3














                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.






                share|improve this answer


























                  3












                  3








                  3







                  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.






                  share|improve this answer













                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 at 4:48









                  user7user7

                  9,73932546




                  9,73932546

























                      0














                      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.






                      share|improve this answer
























                      • I feel like an idiot. Thanks!

                        – Billy Yin
                        Jan 4 at 5:03
















                      0














                      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.






                      share|improve this answer
























                      • I feel like an idiot. Thanks!

                        – Billy Yin
                        Jan 4 at 5:03














                      0












                      0








                      0







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 4 at 4:52









                      KdaydinKdaydin

                      65111




                      65111













                      • I feel like an idiot. Thanks!

                        – Billy Yin
                        Jan 4 at 5:03



















                      • I feel like an idiot. Thanks!

                        – Billy Yin
                        Jan 4 at 5:03

















                      I feel like an idiot. Thanks!

                      – Billy Yin
                      Jan 4 at 5:03





                      I feel like an idiot. Thanks!

                      – Billy Yin
                      Jan 4 at 5:03











                      0














                      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






                      share|improve this answer




























                        0














                        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






                        share|improve this answer


























                          0












                          0








                          0







                          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






                          share|improve this answer













                          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







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 4 at 5:08









                          Nipuna PriyamalNipuna Priyamal

                          312114




                          312114






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54033219%2fdifferent-methods-of-printing-returning-different-values-for-the-same-variables%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Mossoró

                              Error while reading .h5 file using the rhdf5 package in R

                              Pushsharp Apns notification error: 'InvalidToken'