在提示用户执行操作后执行先前执行的While语句

时间:2016-02-28 06:42:05

标签: java if-statement while-loop

您好我正在构建一个Craps版本,它随机滚动两个骰子并将信息存储在两个不同的卷中,其中滚动了7次以及滚动11次100次卷的次数。我已经设法得到它,我可以输出我获得了多少卷,总共有多少胜利,但现在当我提示用户他们想要再次播放时,我无法看到让它执行我创建的while方法滚动循环。有没有办法可以让它在最后一个if语句中重新执行它,如果是这样,我会在if语句中放入什么来实现这个或者是否有其他类型的方法我应该在这里做呢?它最近关闭并指向堆栈上的字符串比较页面,但没有回答我的问题。我理解如何比较字符串和==做什么。但是,这并没有告诉我在提示用户采取行动后重新执行先前执行的while循环所需的原因或内容。

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

0 个答案:

没有答案