首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使代码再试一次?

如何使代码再试一次?
EN

Stack Overflow用户
提问于 2015-04-06 23:56:02
回答 1查看 2.2K关注 0票数 0

这是我的代码.我想我没有正确地使用do/while循环.

如何正确地使用do/while?

我应该用不同的方法吗?

我想知道怎么

代码语言:javascript
复制
   public static void main(String[] args) {
    // TODO code application logic here
    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();
    String tryAgain = "y";
    do {
    System.out.println("Dice   Rolls\tYahtzee's Percentage  Odds\t");
    for (int i = min; i <= max; i++) {
        int count = 0;
        boolean success = true;
        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);
    }
        System.out.print("Run another simulation [y/n]? ");
        tryAgain = input.nextLine();
} while(!tryAgain.equals("n"));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-07 00:21:57

我认为,immibis在第一条评论中指出了这一点。你就快猜对了了。试试这个:

代码语言:javascript
复制
public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner(System.in);
        Random generator = new Random();

        String tryAgain = "y";
        do {
            System.out.print("enter minimum of Yahtzee dice 2-9:");
            int min = input.nextInt();
            input.nextLine();
            System.out.print("enter maximum of Yahtzee dice 3-10:");
            int max = input.nextInt();
            input.nextLine();

            System.out.println("Dice   Rolls\tYahtzee's Percentage  Odds\t");

            for (int i = min; i <= max; i++) {
                int count = 0;
                boolean success = true;
                for (int j = 0; j <= 5000; j++) {
                    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);
            }

            System.out.print("Run another simulation [y/n]? ");
            tryAgain = input.nextLine();

        } while (!tryAgain.equals("n"));

}

ps。在玩游戏的时候,我移动了你的输入,却不知道你想做什么。如果这是你想要的话,你应该把它们从循环中移回来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29481544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档