-1

I want to create a game called Crap. The problem that I am facing is that the main while loop does not end despite the condition being set to false (by user inputing "no" when asked if they want to conitnue playing). What is the problem here? Comments are inserted where I think the problem is.

import java.util.Scanner;

public class crap {

    public static void main(String[] args) {
        System.out.println("You are about to play the game of Crap. Press Enter to continue.");
        
        Scanner enter = new Scanner (System.in);
        String hitEnter = enter.nextLine();
        Scanner input = new Scanner(System.in);
        String proceed = "yes";
            
        // This loop does not exit even if proceed == "no"
        while (proceed != "no"){
            
            int playerPoint;
            int firstDice = 1 + (int) (Math.random() * 10) % 6;
            int secondDice = 1 + (int) (Math.random() * 10) % 6;
            int throwSum = firstDice + secondDice;
            
            if (throwSum == 7 || throwSum == 11) {
                System.out.println("Congratulations! You win on your first roll! You rolled " 
            + firstDice + " and " + secondDice + " for a total of " + throwSum);
            }
            else if (throwSum == 2 || throwSum == 3 || throwSum == 12) {
                System.out.println("Sorry, you crapped out, you lose! You rolled " + firstDice +
                " and " + secondDice + " for a total of " + throwSum);
            } else {
                playerPoint = throwSum;
                System.out.println("You rolled " + firstDice + " + " + secondDice + " which is "
                + playerPoint);
                System.out.println("Your point is " + playerPoint + " now. Good luck.");
                
                while (throwSum != 7 ) {
                    firstDice = 1 + (int) (Math.random() * 10) % 6;
                    secondDice = 1 + (int) (Math.random() * 10) % 6;
                    throwSum = firstDice + secondDice;
                    
                    if (throwSum != 7) {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        
                        if (throwSum == playerPoint) {
                            System.out.println("Congratulations! You win. You reached your point.");
                        break;
                        }
                        System.out.println("Your point is " + playerPoint + ". Good luck.");
                    }
                     else {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        System.out.println("Sorry, you crapped out, you lose."); 
                        System.out.println("You rolled 7 before reaching your point.");
                        break;
                    }
                }       
            }
            System.out.println("Do you want to play again? yes/no: ");
            
                 // even if user enters "no" the loop does not exit     
            proceed = input.nextLine(); 
        }
        System.out.println("Thanks for playing.");
        enter.close();
        input.close();
    }
}
twicelost
  • 61
  • 8

2 Answers2

1

You are using the wrong operator. != checks if the two objects on left and right side are the the same (e.g. two variables reference the same object in memory).

You must write while (! "no".equals(proceed) ).

I did not write while (! proceed.equals("no") ) on purpose because if the string is null then you would get a NullPointerException.

I am not sure if Scanner.readLine() does ever return a null string, so it may make no difference here. But writing it in the "reverse" way as in my first example is more safely in general.

Stefan
  • 1,789
  • 1
  • 11
  • 16
  • Yes, I changed the operator as suggested and it fixed the problem. I also leaned something new today. Thanks for the quick reply. – twicelost Sep 27 '20 at 16:53
0

For a String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

You have to use .equals(String) to compare Strings, like so:

import java.util.Scanner;

public class crap {

    public static void main(String[] args) {
        System.out.println("You are about to play the game of Crap. Press Enter to continue.");
        
        Scanner enter = new Scanner (System.in);
        String hitEnter = enter.nextLine();
        Scanner input = new Scanner(System.in);
        String proceed = "yes";
            
        // This loop does not exit even if proceed == "no"
        while (!proceed.equals("no")){
            
            int playerPoint;
            int firstDice = 1 + (int) (Math.random() * 10) % 6;
            int secondDice = 1 + (int) (Math.random() * 10) % 6;
            int throwSum = firstDice + secondDice;
            
            if (throwSum == 7 || throwSum == 11) {
                System.out.println("Congratulations! You win on your first roll! You rolled " 
            + firstDice + " and " + secondDice + " for a total of " + throwSum);
            }
            else if (throwSum == 2 || throwSum == 3 || throwSum == 12) {
                System.out.println("Sorry, you crapped out, you lose! You rolled " + firstDice +
                " and " + secondDice + " for a total of " + throwSum);
            } else {
                playerPoint = throwSum;
                System.out.println("You rolled " + firstDice + " + " + secondDice + " which is "
                + playerPoint);
                System.out.println("Your point is " + playerPoint + " now. Good luck.");
                
                while (throwSum != 7 ) {
                    firstDice = 1 + (int) (Math.random() * 10) % 6;
                    secondDice = 1 + (int) (Math.random() * 10) % 6;
                    throwSum = firstDice + secondDice;
                    
                    if (throwSum != 7) {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        
                        if (throwSum == playerPoint) {
                            System.out.println("Congratulations! You win. You reached your point.");
                        break;
                        }
                        System.out.println("Your point is " + playerPoint + ". Good luck.");
                    }
                     else {
                        System.out.println("You rolled " + firstDice + " + " + secondDice + 
                        " which is " + throwSum);
                        System.out.println("Sorry, you crapped out, you lose."); 
                        System.out.println("You rolled 7 before reaching your point.");
                        break;
                    }
                }       
            }
            System.out.println("Do you want to play again? yes/no: ");
            
                 // even if user enters "no" the loop does not exit     
            proceed = input.nextLine(); 
        }
        System.out.println("Thanks for playing.");
        enter.close();
        input.close();
    }
}
Spectric
  • 30,714
  • 6
  • 20
  • 43