Dice Toss Game on java - java

I'm coding a dice roll game on java with some set rules.
The player seeks to obtain a 7 or 11 in the combination of dice to win. If on the other hand he gets a 2, 3 or 12 loses. If during the first throw you do not get a 7 or 11 (with which you win), or a 2, 3 or 12 (with which you lose), the game will enter a second stage, in which you will mark the "point" in the number that is obtained in said launch (4, 5, 6, 8, 9 or 10). In this stage, the shooter will seek to get that number again in the dice, with which he will win before obtaining a 7. If he manages to repeat the point number, the player will win. If on the other hand if a 7 appears, you will lose.
import myClass.dice1;
import myClass.dice2;
public class game{
public static void main(String [] args)
{
int t1, t2, total;
dice1 d1 = new dice1();
t1 = d1.tossdice();
dice2 d2 = new dice2();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1+t2;
if(total == 7 || total == 11)
{
System.out.println("Game won with "+ total);
}else if (total == 2 || total == 3 || total == 12)
{
System.out.println("Game lost with "+ total);
}else if (total == 4)
{
do
{
System.out.println("Total is "+total+" Throw again");
t1 = d1.tossdice();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1+t2;
break;
}while(total !=4 || total !=7);
if (total == 4)
{
System.out.println("Won game with "+ total);
}else
{
System.out.println("Lost game with "+ total);
}
}
}
}
The issue I'm having is once I begin the else if, so if I get a 4 I must get a 4 again to win, if I get a 7 I lose, an if it is another number just throw again until the 4 or 7, issue is that when I get a 4 it "throws" again but if is anything other than a 4 is a lost game, don't know if it is for the break line that I have but if I don't put break it goes on a loop forever and I have to close the cmd and start again.
Only the else if for the 4 is what I have done since once the code for the 4 is working I just have to do the same with the 5, 6, 8, 9, and 10.
This code that I have made separately
import myClass.dice1;
import myClass.dice2;
just print this:
case 1: System.out.println(" ");
System.out.println(" o ");
System.out.println(" ");
Depending of the value of t1 and t2 it prints between case 1 and 6.

The following code fixes the problem. The main issues were:
In the second phase of the game, you need to store the dice roll which will win the game in a variable
The condition in the second phase is roll again if (roll != winTarget && roll != 7) .. you had an or here.
Here is the code:
public class game {
public static void main(String[] args) {
int t1, t2, total;
dice1 d1 = new dice1();
t1 = d1.tossdice();
dice2 d2 = new dice2();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1 + t2;
if (total == 7 || total == 11) {
System.out.println("Game won with " + total);
} else if (total == 2 || total == 3 || total == 12) {
System.out.println("Game lost with " + total);
} else {
int winTarget = total;
System.out.println("Rolling until dice roll="+winTarget+" or 7");
do {
t1 = d1.tossdice();
t2 = d2.tossdice();
d1.drawdice(t1);
d2.drawdice(t2);
total = t1 + t2;
System.out.println("Total is " + total + " Throw again");
} while (total != winTarget && total != 7);
if (total == winTarget) {
System.out.println("Won game with " + total);
} else {
System.out.println("Lost game with " + total);
}
}
}

Related

Dice continuous sum game Java

I'm new to programming and trying to make a dice game. The game consists of three dice and involves the sum of 12 (on any number of dice) in each round. Each dice can only be rolled once per round.
In each round, the player must be able to choose between: 1 to roll the dice 1, 2 to roll the dice 2, 3 to roll the dice 3, q to exit the game. The program must randomly find a value on the selected dice and then calculate the score. The program should also present the number of wins and the number of rounds lost. The program should continue until the user chooses to cancel the game. Regardless of the number of dice, and the definition of loss is a sum exceeding 12 after all three dice have been rolled. If the sum after three rolls is less than 12, there will be no profit or loss, but you go straight to the next round.
So currently my code works for 1 round, the problem begins when the second round begins. The new dice rolls are just the input I give it, and not a random value (1-6).
How can I make this work?
import java.util.Scanner;
import java.util.Random;
public class Main{
//Declaring int variables for all 3 dice
static int dice1;
static int dice2;
static int dice3;
//Declaring true/false for which dice that is thrown, can't be thrown more than 1 time in each round.
static boolean dice1Thrown = false;
static boolean dice2Thrown = false;
static boolean dice3Thrown = false;
//Method for which dice is thrown - the input the user choses (1, 2, 3 or q)
static char diceToThrow;
//Method that checks what number 1-6 the dice roll. sum by default is 0
static int sum = 0;
//Method for keep track of # wins
static int wins;
//Method keep tracks of # losses
static int losses;
//If firstGame = true it will print "Welcome to the game 12" if false, it doesn't print that every round
static boolean firstGame = true;
//Int for keeping track of when dice thrown is = 3. Go to next round.
static int rounds;
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
if(firstGame)
{
System.out.println("Welcome to the game 12. You must roll 1-3 dice and try to get the sum 12 ... ");
firstGame = false;
}
System.out.printf("Enter which dice you want to roll [1,2,3] (exit with q): ");
//Calling method diceToThrow, chosing input 1,2,3 or q
diceToThrow = scan.next().charAt(0);
tärningsKast(diceToThrow);
//sum = dice1 + dice2 + dice3
calcSum();
//If user gets 12 store int wins as +1, if lose store int loss as +1
winOrLose();
//Printing dices, if input from user is 1, it shows sum of first dice etc.
System.out.printf(dice1 + " " + dice2 + " " + dice3 + " sum: " + sum + " #win: " + wins + " #loss: " + losses+ " ");
//Calling method for round score.
checkRounds();
main(args);
//Method created for sum calculating, called in Main method
}static void calcSum(){
sum = (dice1 + dice2 + dice3);
}
//Method created for dice1, dice2, dice3 and char q (as exit). Dice generates random number between 0-5 and always adds + 1 (1-6)
static void tärningsKast(char diceToThrow){
Random rand = new Random();
//If user input = 1, generate dice1
if(diceToThrow == '1' && !dice1Thrown){
dice1 = rand.nextInt(6) + 1;
dice1Thrown = true;
}
//If user input = 2, generate dice2
else if(diceToThrow == '2' && !dice2Thrown){
dice2 = rand.nextInt(6) +1;
//If user input = 3, generate dice3
}else if(diceToThrow == '3' && !dice3Thrown){
dice3 = rand.nextInt(6) + 1;
dice3Thrown = true;
//If user input = char q, Print "Game Over!" and exit game.
}else if(diceToThrow == 'q'){
System.out.println("Game Over!");
System.exit(0);
}
}
static void winOrLose(){
if(sum == 12){
wins++;
}
else if(sum > 12){
losses++;
}
}
static void checkRounds(){
rounds++;
if(rounds == 3)
{
System.out.println("\n" + "Next round!");
dice1 = 0;
dice2 = 0;
dice3 = 0;
rounds = 0;
}
}
}
When you are going for the next round, the method tärningsKast() is only going to work again and again once each round passes if the dice is not thrown.
Meaning that once a round finishes and you are going to start another round, you have to set that the dices are not thrown, since the round is going to start right after.
The solution I found is to set the dice1Thrown, dice2Thrown and dice3Thrown to false on the if statement that prints that the next round is starting.
static void checkRounds(){
rounds++;
if(rounds == 3)
{
System.out.println("\n" + "Next round!");
dice1 = 0;
dice2 = 0;
dice3 = 0;
rounds = 0;
dice1Thrown = false;
dice2Thrown = false;
dice3Thrown = false;
}
After that the game seems to be working fine.
The steps I made to find that it, were:
Set a break point to debug the line that prints "Next round".
Once the debugger reachs that line, set a break point in the first line of the method tärningsKast(), then check that in each check of the if, else if and etc. the diceNThrown variables are all still setted to true, so none of the conditions will match.
Regarding the question:
"You mean, if you wanted to stop the game in the round 2 ?"
"Yeah exactly like that"
First I'd use you checkRounds() method to check if it were to stop the game, meaning if the user already played two rounds.
Second I would move the logic that move to the next round to a method that checks attemps, intead of rounds, since we will move to the next round every time the user already had three attempts.
And finally before moving to the next round I would be increasing the number of rounds, and right after checking if the number of rounds if the expected number of rounds to stop, that way you can play the game with the limit of rounds you desire to.
//Declaring int variables for all 3 dice
static int dice1;
static int dice2;
static int dice3;
//Declaring true/false for which dice that is thrown, can't be thrown more than 1 time in each round.
static boolean dice1Thrown = false;
static boolean dice2Thrown = false;
static boolean dice3Thrown = false;
//Method for which dice is thrown - the input the user choses (1, 2, 3 or q)
static char diceToThrow;
//Method that checks what number 1-6 the dice roll. sum by default is 0
static int sum = 0;
//Method for keep track of # wins
static int wins;
//Method keep tracks of # losses
static int losses;
//If firstGame = true it will print "Welcome to the game 12" if false, it doesn't print that every round
static boolean firstGame = true;
//Int for keeping track of when dice thrown is = 3. Go to next round.
static int attempts;
// keeping track of the number of rounds
static int rounds = 0;
// desired number of rounds to play
static final int LIMIT_OF_ROUNDS = 5;
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
if(firstGame)
{
System.out.println("Welcome to the game 12. You must roll 1-3 dice and try to get the sum 12 ... ");
firstGame = false;
}
System.out.printf("Enter which dice you want to roll [1,2,3] (exit with q): ");
//Calling method diceToThrow, chosing input 1,2,3 or q
diceToThrow = scan.next().charAt(0);
tärningsKast(diceToThrow);
//sum = dice1 + dice2 + dice3
calcSum();
//If user gets 12 store int wins as +1, if lose store int loss as +1
winOrLose();
//Printing dices, if input from user is 1, it shows sum of first dice etc.
System.out.printf(dice1 + " " + dice2 + " " + dice3 + " sum: " + sum + " #win: " + wins + " #loss: " + losses+ " ");
//Calling method for round score.
checkAttempts();
main(args);
//Method created for sum calculating, called in Main method
}static void calcSum(){
sum = (dice1 + dice2 + dice3);
}
//Method created for dice1, dice2, dice3 and char q (as exit). Dice generates random number between 0-5 and always adds + 1 (1-6)
static void tärningsKast(char diceToThrow){
Random rand = new Random();
//If user input = 1, generate dice1
if(diceToThrow == '1' && !dice1Thrown){
dice1 = rand.nextInt(6) + 1;
dice1Thrown = true;
}
//If user input = 2, generate dice2
else if(diceToThrow == '2' && !dice2Thrown){
dice2 = rand.nextInt(6) +1;
//If user input = 3, generate dice3
}else if(diceToThrow == '3' && !dice3Thrown){
dice3 = rand.nextInt(6) + 1;
dice3Thrown = true;
//If user input = char q, Print "Game Over!" and exit game.
}else if(diceToThrow == 'q'){
System.out.println("Game Over!");
System.exit(0);
}
}
static void winOrLose(){
if(sum == 12){
wins++;
}
else if(sum > 12){
losses++;
}
}
static void checkAttempts(){
attempts++;
if(attempts == 3)
{
rounds++;
checkRounds(rounds);
System.out.println("\n" + "Next round!");
dice1 = 0;
dice2 = 0;
dice3 = 0;
attempts = 0;
dice1Thrown = false;
dice2Thrown = false;
dice3Thrown = false;
}
}
static void checkRounds(int rounds) {
if (rounds == LIMIT_OF_ROUNDS) {
tärningsKast('q');
}
}

How would I get the dice to keep rerolling and printing until it hits "7" or the "point"

I am trying to build a craps game in which the computer automatically rolls a pair of dice and if the roll is a 7 or 11 the user win. However if the user rolls a 2, 3, or 12 they automatically lose.
Furthermore, if the user rolls any other number (4, 5, 6, 8, 9, 10) that is their "point" and they are to try to roll that point again. (unless they roll a 7 then they lose.) I am trying to get my while loop to continue rolling if the computer rolls a number other than 7 or the "point"
Unfortunately, my loop is not working and it does not continue to re-roll.
Thanks in advance, and this is my code:
this is what i have now:
public class CrapsPractice{
public static void main(String[]args){
int d1 = (int) (6 * Math.random() + 1);
int d2 = (int) (6 * Math.random() + 1);
int roll = (d1 + d2);
int point = roll;
if (roll == 7 || roll == 11)
{
System.out.println("You rolled a" + roll);
System.out.println("Congrats! You've immediately won!");
return; //terminate the main function
}
else if (roll == 2 || roll == 3 || roll == 12)
{
System.out.println("you rolled a " + roll);
System.out.println("You lose!");
return; //terminate the main function
}
//do-while loop: execute and then check for condition
//and then if condition holds, execute a second time and so on
do {
int d3 = (int) (6 * Math.random() + 1);
int d4 = (int) (6 * Math.random() + 1);
roll = d3 + d4;
System.out.println("your point is" + point);
} while(roll != 7 && roll != point );
}
}
Your code works for me but try these printouts instead to get a better idea of what is happening:
//do-while loop: execute and then check for condition
//and then if condition holds, execute a second time and so on
do {
int d3 = (int) (6 * Math.random() + 1);
int d4 = (int) (6 * Math.random() + 1);
roll = d3 + d4;
System.out.println("your roll is " + roll + " and point is " + point);
} while(roll != 7 && roll != point );
if (roll == 7)
System.out.println("you lose");
else
System.out.println("you win");
Output:
your roll is 6 and point is 9
your roll is 4 and point is 9
your roll is 3 and point is 9
your roll is 8 and point is 9
your roll is 8 and point is 9
your roll is 6 and point is 9
your roll is 9 and point is 9
you win
BUILD SUCCESSFUL (total time: 0 seconds)

How do I use an if statement to know whether the player or the computer won?

I just started learning java so I may not even be on the right track but I have an assignment that asks me to create a game of 21. The way the game works is that a player and computer take turns entering either a 1, 2, or 3. The player that enters a number that meets or exceeds 21 loses. The trouble I seem to be having is that I cannot seem to get the program to exit the loop when the final number is entered and it will display that the player loses every time, win or lose.
I have tried using another if statement after the do-while loop to display either "You Win!" or "You Lost." However, I can't figure out what parameters I should use in the if statement to decide who won. I also tried to set the game up with the player as even numbers and the computer as odd numbers but I couldn't get the numbers to add to a running total to end the loop.
int numLoops = 0;
int firstCheck;
int points;
Scanner input = new Scanner(System.in);
do
{
System.out.print("\nEnter a 1, 2, or 3 >> ");
points = input.nextInt();
int random = (int )(Math.random() * 3 + 1);
numLoops = points + numLoops + random;
if(numLoops < 21)
{
System.out.println("The computer entered a " + random);
System.out.println("The new total is " + numLoops);
}
else
{
//This is what always prints.
System.out.println("You lost! The computer is the victor.");
}
}while(numLoops < 21);
//this is what I am having most of my trouble with.
System.out.println("You Win!");
I expect that the loop will close after the total reaches 21 and will output a statement That varies based on who won. However, the program always outputs that the player lost.
If have made a few changes to simplify things. Have a look at the source code, it should be well documented and explains things best.
/*
* Which players turn is it?
* (true for human player, false for computer)
*/
boolean isHumanPlayersTurn = true;
int totalPoints = 0;
Scanner input = new Scanner(System.in);
// the game ending condition
while (totalPoints < 21) {
// take an action, depending on the players turn
if (isHumanPlayersTurn) {
System.out.print("\nEnter a 1, 2, or 3 >> ");
totalPoints += input.nextInt();
} else {
int random = (int) (Math.random() * 3 + 1);
System.out.println("The computer takes " + random);
totalPoints += random;
}
System.out.println("Total amount is " + totalPoints);
/*
* Important part: After each players move, change the players turn, but do NOT
* do this if the game already ended, since then the other player would have won.
*/
if (totalPoints < 21) {
isHumanPlayersTurn = !isHumanPlayersTurn;
}
}
// now, the player to move is the one who loses
if (isHumanPlayersTurn) {
System.out.println("You lost! The computer is the victor.");
} else {
System.out.println("You Win!");
}
After the user enters a number, you should check immediately to see if they hit 21 (and also output the total at that point). If 21 was not hit, then the computer should guess, and then you check again to see if 21 was hit. So you'll have two if statements (one after each guess; user/computer). In the else block after the user guess is where the computer would guess. Your if statement will check for >= 21, indicating a loss for the user or computer as appropriate. You won't need to declare a victor outside of the loop as this can be done inside the loop...
For example:
int total = 0;
do
{
System.out.print("\nEnter a 1, 2, or 3 >> ");
points = input.nextInt();
total = total + points;
System.out.println("The new total is " + total);
if (total >=21)
{
System.out.println("You lost! The computer is the victor.");
}
else
{
int random = (int )(Math.random() * 3 + 1);
total = total + random;
System.out.println("The computer entered a " + random);
System.out.println("The new total is " + total);
if(total >= 21)
{
System.out.println("You Win!");
}
}
}while(total < 21);

Creating a Java Game Pig

I am in the process of creating the java game PIG, each player gets to roll a dice and each turn is added to a score and the first player to get 100 is the winner, the players can choose to roll the dice or hold, if the player rolls and land a 1 their turn is over and all points go to 0, if a player chooses to hold all points gained during the turn are added to her school.
I do however have some issues that i cannot work out, when a user rolls a 1 and their turn would be over and no points given, instead the program gives no output and does not carry on playing (going into an infinite loop??).
edit:
i have since been able to solve the previous issues however i now only get an infinite loop when the program is ran and i do not understand why, Also does anyone know how to make the game replayable after a game has ended?
below is my new code:
//Callum Fawcett
//Game of Pig
//Create: 20/02/2019
//Version 0.1
import java.util.Scanner;
import java.util.Random;
public class game2
{
public static void main(String[] args)
{
int playerScores = 0;
int playerTotal = 0;
int computerScores = 0;
int computerTotal = 0;
int dice;
boolean gameOver = false;
boolean turnOver = false;
char repeat;
String input;
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
System.out.println("Welcome to the game of Pig!\n");
while (gameOver == false)
{
do
{
dice = rand.nextInt(6) + 1;
System.out.println("You rolled: " + dice);
if (dice == 1)
{
playerScores = 0;
System.out.print("You lose your turn! ");
System.out.println("Your total is " + playerTotal);
turnOver = true;
}
else
{
playerScores += dice;
System.out.print("Your turn score is " +
playerScores);
System.out.println(" If you hold you will have " +
playerScores + " points.");
System.out.println("Enter 'r' to roll " +
"again, 'h' to hold.");
input = keyboard.nextLine();
repeat = input.charAt(0);
if (repeat == 'h')
break;
}
} while(turnOver == false || dice != 1);
playerTotal += playerScores;
System.out.println("Your score is " +
playerTotal);
playerScores = 0;
if (playerTotal >= 100)
{
System.out.println("YOU WIN!");
gameOver = true;
}
System.out.println();
System.out.println("It is the computer's turn.");
do
{
dice = rand.nextInt(6) + 1;
System.out.println("The computer rolled: " +
dice);
if(dice == 1)
{
computerScores = 0;
System.out.print("The computer lost its turn!");
System.out.print(" Computer total is " +
computerTotal);
turnOver = false;
}
else
{
computerScores += dice;
if(computerScores >= 20 || (computerTotal +
computerScores) >= 100)
System.out.println("The computer holds");
turnOver = false;
}
} while (dice != 1 || computerScores < 20);
computerTotal += computerScores;
System.out.println("The computer's score is " +
computerTotal + "\n");
computerScores = 0;
if (computerTotal >= 100)
{
System.out.println("THE COMPUTER WINS!");
gameOver = true;
}
if(keyboard!=null)
keyboard.close();
}
}
}
this is an output i get :
TheRealFawcett:lab5 therealfawcett$ java game2
Welcome to the game of Pig!
You rolled: 6
Your turn score is 6 If you hold you will have 6 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 4
Your turn score is 10 If you hold you will have 10 points.
Enter 'r' to roll again, 'h' to hold.
r
You rolled: 1
You lose your turn! Your total is 0
Your score is 0
It is the computer's turn.
The computer rolled: 2
The computer rolled: 2
The computer rolled: 1
The computer lost its turn! Computer total is 0The computer's score is
0
You rolled: 2
Your turn score is 2 If you hold you will have 2 points.
Enter 'r' to roll again, 'h' to hold.
Exception in thread "main" java.lang.IllegalStateException: Scanner
closed
at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150)
at java.base/java.util.Scanner.findWithinHorizon(Scanner.java:1781)
at java.base/java.util.Scanner.nextLine(Scanner.java:1649)
at game2.main(game2.java:50)
TheRealFawcett:lab5 therealfawcett$

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
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.
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.
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

Categories

Resources