【问题标题】:Having trouble with play again又玩不下去了
【发布时间】:2015-04-06 22:09:02
【问题描述】:

所以这是我的代码....我想找到一种方法如何再次播放...使用 while 循环或其他方法....任何人都可以帮我解决这个问题吗?

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
    System.out.print("enter minimum of Yahtzee dice 2-9: ");
    int min = input.nextInt();
    System.out.print("enter maximum of Yahtzee dice 3-10: ");
    int max = input.nextInt();
    System.out.println("Dice   Rolls\tYahtzee's Percentage  Odds\t");
    for (int i = min; i <= max; i++) {
        int count = 0;
        for (int j = 0; j <= 5000000; j++) {
            Random generator = new Random();
            YahtzeeDice d = new YahtzeeDice(generator, i);
            if (d.IsAYahtzee()) {
                count++;
            }
        }
        System.out.printf("%d     ", i);
        System.out.printf("%d\t", 5000000);
        System.out.printf("%d\t  ", count);
        System.out.printf("%f   ", (double) count / 5000000);
        double per = count / (double) 5000000;
        int odds = (int) (1 / per);
        System.out.printf("1 in %d\n", odds);
    }
}

【问题讨论】:

  • 将您想要/需要执行多次的代码包装到do-while 循环中。

标签: java for-loop while-loop


【解决方案1】:

您可以在代码中添加环绕的循环代码,并在其中询问“再次播放”。像这样:

do{

   //your code

   System.out.print("Gib was ein: ");
   String answer = br.readLine();
}while(answer.equal("yes"))

您也可以将代码放入函数中。

do{

   mcFunction

   System.out.print("Gib was ein: ");
   String answer = br.readLine();
}while(answer.equal("yes"))

public void myFunction(){
   //your code
}

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多