为什么我的循环无限?

时间:2015-03-27 02:46:52

标签: java

这是我的代码中3个类中的第2个,我不确定它是否会起作用,虽然我不确定为什么它在无限循环中?我正在努力使这个游戏工作,所以每次下注或者赢或输都会影响我的游戏,所以对于一个底池是零,游戏将结束。它似乎没有工作,但任何建议?

import java

.util.Scanner;


public class Game {
    public int potAmount = 50;
    public int betAmount= 0;
    private Scanner input = new Scanner (System.in);

    public void displayPot(){
        System.out.println("Your current pot is "+potAmount+"");
    }
    public void getbetamountfromuser(){
        System.out.println("Enter your bet amount:");
        betAmount = input.nextInt();
        potAmount= potAmount - betAmount;
    }

    public void playgame(){
        while( potAmount > 0) {
            Die Die1 = new Die();
            Die Die2 = new Die();
            Die Die3 = new Die();
            Die1.rollDice();
            Die2.rollDice();
            Die3.rollDice();
            Die1.Getdie();
            Die2.Getdie();
            Die3.Getdie();
            System.out.println("Your die are: "); 
            Die1.displayDice();
            System.out.print( " and ");
            Die2.displayDice();
            System.out.print( " and ");
            Die3.displayDice();
            int Totaldice=Die1.Getdie()+Die2.Getdie()+Die3.Getdie();


            if (Totaldice>=12){
                System.out.print("You WIN...your bet back");
            }

            else if (Totaldice<12){
                System.out.print("You LOSE....your bet ");
            }
            if (Die1==Die2 ){
                System.out.print("You WIN....double your bet");
                if (Die2==Die3 ){
                    System.out.print("You WIN....double your bet");
                    if (Die1==Die3 ){
                        System.out.print("You WIN....double your bet");
                    }
                }
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

除非您更改potAmount的值,否则它将始终为> 0,从而导致循环无限运行。

答案 1 :(得分:1)

循环中没有任何内容更改potAmount - 因此,potAmount永远不会变为0,因此循环会保持循环。

答案 2 :(得分:1)

我早就写过这样的游戏,但仍然有这个。由于我们的游戏非常相似,您可以使用它来帮助您调试。如果你愿意,我可以在以后提供OOP版本。

/**This program is a dice rolling game which asks a user to enter their bet amount, then it rolls the dice then based on these rules: 
 * If their roll is doubles - their bet amount doubles and gets added to their pot.
 * If their roll is 7 or 11 in total – then they win their bet amount and that amount will be added to their pot
 * If they're  roll is 5 or less or 10 or greater – then stay even otherwise they'll lose their bet and that bet amount gets subtracted from their pot
 */

import java.util.*;
import java.text.NumberFormat;

/**
 *
 * @author Tatakai
 */
public class DieGame {

    public static void main(String[] args) {
        int pot = 100;
        // Array for first and second die are declared and instantialized here
        // with each having 6 elements.
        int[] firstDie = { 1, 2, 3, 4, 5, 6 };
        int[] secondDie = { 1, 2, 3, 4, 5, 6 };

        Scanner input = new Scanner(System.in);
        NumberFormat formatter = NumberFormat.getCurrencyInstance();

        while (true) {

            // Random element chooser for arrays
            int dieFace1 = firstDie[(int) (Math.random() * firstDie.length)];
            int dieFace2 = secondDie[(int) (Math.random() * secondDie.length)];
            int diceTotal = dieFace1 + dieFace2;

            System.out.print("Your current pot is " + formatter.format(pot)
                    + " enter your bet amount (0 to quit): ");
            int userIn = input.nextInt();

            while (userIn > pot) {
                System.out.print("Invalid bet amount, enter bet amount: ");
                userIn = input.nextInt();

            }
            if (userIn == 0) {
                System.out.print("You left the game with "
                        + formatter.format(pot));
                break;
            }
            System.out.print("you rolled a " + dieFace1 + " and " + dieFace2
                    + "\n");

            if (dieFace1 == dieFace2) {
                pot += userIn * 2;
                System.out.println("You Win Double!!");
                System.out.println("");

            } else if (diceTotal == 7 || diceTotal == 11) {
                pot += userIn;
                System.out.println("You win this bet!!");
                System.out.println("");

            } else if (diceTotal <= 5 || diceTotal >= 10) {
                System.out.println("You Stay even!!");
                System.out.println("");

            } else {
                pot -= userIn;
                System.out.println("You lose!!");
                System.out.println("");
            }

        }
        input.close();
    }
}