Java Do While循环问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Do While循环问题相关的知识,希望对你有一定的参考价值。

编辑,只有锅值不循环的问题。

我正在开发一个简单的赌博游戏中我的循环问题。游戏完成了我想要的一切,直到它循环回来。我不希望锅重置回我设置的静态$ 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);
    }
}
答案

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

....
int r1 = ran.nextInt(6) + 1; 
int r2 = ran.nextInt(6) + 1;
int r3 = ran.nextInt(6) + 1;
....
do{
 r1 = ran.nextInt(6) + 1; // again random at the start of do-while loop is necessary
 r2 = ran.nextInt(6) + 1;
 r3 = ran.nextInt(6) + 1;
....
}.....

以上是关于Java Do While循环问题的主要内容,如果未能解决你的问题,请参考以下文章

JAVA复习5 Java循环结构 - for, while 及 do...while

java_do-while循环和while循环区别

JAVA循环结构

将“do while”循环从java重写为python [重复]

Java入门八 循环结构——for循环while循环do-while循环

JS中 do while循环问题