Java Do While循环问题

时间:2017-12-17 19:27:09

标签: java loops while-loop

已编辑,只有pot值无法循环的问题。

我正在开发一个简单的赌博游戏中的循环问题。游戏完成了我想要的一切,直到它循环回来。我不希望锅重置回我设置的静态$ 100金额。 它也没有给新的骰子。固定(感谢Prodian)。似乎循环将继续重置为原始数量,并且不会考虑最后一轮。我有一种感觉,这只是一个错误的变量放置的问题,但我已经尝试过移动它们,我只是无法绕过一个解决方案。任何帮助将不胜感激!

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

public class gamblinggame {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random ran = new Random();

        double bet;
        double pot = 100;
        int r1 = ran.nextInt(6) + 1;
        int r2 = ran.nextInt(6) + 1;
        int r3 = ran.nextInt(6) + 1;

        System.out.println("*** Gambling Game ***");
        System.out.println("Your starting pot is $100, please place a bet less than or equal to the pot.");

        do {

             r1 = ran.nextInt(6) + 1; // continues to re-roll dice
             r2 = ran.nextInt(6) + 1;
             r3 = ran.nextInt(6) + 1;

            bet = scanner.nextDouble();
            if (bet > pot) {
                System.out.println("Invalid amount. Please place a new bet.");
                continue;
            }
            if (bet >= 1) {
                System.out.println("Your new pot is $" + (pot - bet) + ".");
                System.out.println("Rolling the dice.");
                System.out.println(
                        "You rolled " + r1 + ", " + r2 + ", " + r3 + ".  The total amount rolled is " + (r1 + r2 + r3));
                if (r1 == r2 || r1 == r3 || r2 == r3) {
                    System.out.println("You win DOUBLE your bet back!");
                    System.out.println("Your new pot is $" + (pot + (bet * 2)) + ".");
                } else if (r1 == r2 && r1 == r3 && r2 == r3) {
                    System.out.println("You win TRIPLE your bet back!");
                    System.out.println("Your new pot is $" + (pot + (bet * 3)) + ".");
                } else if (r1 + r2 + r3 >= 10) {
                    System.out.println("You win your bet back!");
                    System.out.println("Your new pot is $" + (pot) + ".");
                }
                System.out.println("Enter a new bet equal to or below the pot.");
            }


            if (bet > pot) {
                System.out.println("Invalid amount. Please place a new bet.");
                continue;
            } 
            if (bet == 0) {
                System.out.println("Game Over. Please play again!");
            break;
            }
        } while (pot > 0);
    }
}

1 个答案:

答案 0 :(得分:1)

你应该重新掷骰子,重新掷骰的解决方案是:

O(n)