0

Hello I am building a version of Craps where it randomly rolls two dice and stores the information in two different rolls, where how many times it rolled a 7 and how many times it rolled an 11 in 100 rolls. I have managed to get it where I can output how many rolls I got for each and how many wins altogether, but now when I prompt the user if they want to play again I cant see to get it to execute the while method I created for the rolling loop. Is there a way I can get it to re-execute that in the last if statement and if so what would I put in that if statement to achieve this or is there some other kind of method I should be doing here instead? It was recently closed and pointed me to a string comparison page on stack that didn't answer my question. I understand how to compare strings and what == does. However that doesn't tell me why or what is needed to re-execute a previously performed while loop after prompting the user for action.

import java.util.Random;
import java.util.Scanner;

public class Craps_ws7
{
    public static void main(String args[])
    {
        Scanner keyboard1 = new Scanner(System.in);
        System.out.println("Welcome to my Craps Game!");
        boolean stop = false;
        int sevenRoll = 0;
        int elevenRoll = 0;
        int totalWins = 0;
        int gameCount = 0;
        while(gameCount <= 100)
        {
            gameCount = gameCount + 1;
            Random generator = new Random();
            int die1 = generator.nextInt(6) + 1;
            int die2 = generator.nextInt(6) + 1;
            int dieTotal = die1 + die2;

             if (dieTotal == 7)
             {
                System.out.println("You rolled: " + dieTotal);
                System.out.println("You Win!");
                sevenRoll++;
                totalWins++;
             }
             else if(dieTotal == 11)
             {
                System.out.println("You rolled: " + dieTotal);
                System.out.println("You Win!");
                elevenRoll++;
                totalWins++;
             }
             else
             {
                System.out.println("You rolled: " + dieTotal);
                System.out.println("You lose!");
             }
        }
    System.out.println("You rolled a total of " + sevenRoll + "  sevens and " + elevenRoll + " elevens for a total of " + totalWins + " wins out of 100!" );
    System,out.println("Would you like to play again? [y/n]");
    string ans = keyboard1.nextLine();
    if (ans = "y" || ans = "Y")
    {

    }
    else 
    {
        System.out.println("Thank you for playing, Good Bye!");
    }
    }
}
  • Is there anyway I can receive information on how string comparing is anywhere related to what my question is? – Average Joe Feb 28 '16 at 06:59
  • Well, `ans = "y"` is two comparison bugs in one: using `=` instead of `==` and using `==` instead of `.equals`. As for your actual question, you already know how to use while loops, so "re-executing a previously performed ..." means putting your current loop in another loop. (May be convenient to factor the current loop out into a method.) – Jeffrey Bosboom Feb 28 '16 at 07:23

0 Answers0