samedi 27 février 2016

Executing a while method in an if statement? [duplicate]

This question already has an answer here:

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?

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!");
    }
    }
}

Aucun commentaire:

Enregistrer un commentaire