0

I've written a code for a game that simulates the user and the computer rolling a die and the winner receives 1$ from the loser, with each starting with 2$. The code runs fine, but it doesn't end when either the user or computer reaches 0$ like i had anticipated. Any suggestions?

import java.util.Scanner;

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

    Scanner input = new Scanner(System.in);
    int userMoney = 2;
    int compMoney = 2;
    int userRoll = (int) (1 + Math.random() * 6);
    int compRoll = (int) (1 + Math.random() * 6);

    System.out.print("Press 'r' if you would like to roll ");
    do {String roll = input.nextLine();
    if (roll.equals("r")); {
        if (userRoll > compRoll){
            userMoney++;
            compMoney--;
     System.out.print("The computer rolled " + compRoll + " and you rolled "       + userRoll + ". you won."
        + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
            }

            else if (userRoll < compRoll) {
                compMoney++;
                userMoney--;
                System.out.print("The computer rolled " + compRoll + " and you rolled " + userRoll + 
                        ". you lost" + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
            }

            else {System.out.print("The computer rolled " + compRoll + "and you rolled " + userRoll + 
                    ". it's a tie" + "\n" + "You have $" + userMoney + " & the computer has $" + compMoney);}


            }
            }while (userMoney >= 0 || compMoney >=0);
    }}

5 Answers 5

1

Your while statement is testing for <= 0, but initially both variables are > 0. The while loop will never fire.

Sign up to request clarification or add additional context in comments.

Comments

1

First off you have a problem with your while condition money values = 2 for player and comp, so while will never fire..fix that and you could use a do while

do{
    statement(s) //block of statements
}while (Boolean expression);

So inside your do{} you could have your statements and conditions..so it will do whatever is inside those braces until the condition inside the while() is met

for example

class DoWhileLoopExample {
    public static void main(String args[]){
         int i=10;
         do{
              System.out.println(i);
              i--;
         }while(i>1);
    }
}

1 Comment

if this has solved your issue you should accept the answer
0

You are using the incorrect condition in the while statement.You are testing if any player has >=0 this will always test true and cause an infinite loop. Instead test if BOTH players have >0 and end the game if not.

Also you have a ';' after you if statement. That will cause the code after it to execute all the time.

Here is complete working code:

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int userMoney = 2;
int compMoney = 2;
int userRoll = (int) (1 + Math.random() * 6);
int compRoll = (int) (1 + Math.random() * 6);

System.out.print("Press 'r' if you would like to roll ");
do {String roll = input.nextLine();
if (roll.equals("r")) {
    if (userRoll > compRoll){
        userMoney++;
        compMoney--;
 System.out.print("The computer rolled " + compRoll + " and you rolled "       + userRoll + ". you won."
    + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
        }

        else if (userRoll < compRoll) {
            compMoney++;
            userMoney--;
            System.out.print("The computer rolled " + compRoll + " and you rolled " + userRoll + 
                    ". you lost" + "\n" + "You have $" + userMoney + " & The computer has $" + compMoney);
        }

        else {System.out.print("The computer rolled " + compRoll + "and you rolled " + userRoll + 
                ". it's a tie" + "\n" + "You have $" + userMoney + " & the computer has $" + compMoney);}


        }
         //prompt user to type 'r' to roll again
        System.out.println("\n\nPress 'r' if you would like to roll ");
        }while (userMoney > 0 && compMoney >0);

        System.out.println("\n\nGAME OVER!!!");
    }
   }
//end main

Comments

0

You want to stop when less than or equal to but you have greater than or equal to.

    while (userMoney >= 0 || compMoney >=0) {

Comments

0

Cause of problem:

Your while loop does not run at all, because the condition inside is initially evaluated as false.

  while (userMoney <= 0 || compMoney <=0)

This reads: whilst the user has a money less than or equal to 0 OR whilst the computer has money less than or equal to 0 then run the while loop, this will never initially be true as they both the computer and user both start with $2, hence userMoney == 2 and compMoney == 2.


Solution:

Change the condition of the while loop to the following:

 while(userMoney>0 || compMoney>0)

then add an if statement which says if the compMoney == 0 or if the userMoney == 0, then break out of the while loop.

 if(userMoney == 0 || compMoney == 0){
     break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.