0
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Coin {
    public static void main(String[] args)
    {
        boolean validFinalInput = false;
        boolean validBetInput = false;
        boolean validGuessInput = false; //Determines if the guess is a valid input
        boolean validBet = false; //Determines if the bet is valid
        boolean goAgain = true;
        double num; //Unparsed Side Determiner
        double balance = 100; 
        String Bet = null; //Bet Input
        double bet = 0; //Parsed Bet
        String Guess = null; //H or T
        String Side = null; //Determines Side
        String GoAgain = null; //Y/N Retry input
        Scanner in = new Scanner(System.in);

        while (goAgain == true){ //While true, the player will keep playing.
            num = Math.round(Math.random()); //Declares Number
            if (num == 1){
                Side = "H";
            } else if (num == 0){
                Side = "T";
            } 
            while (validGuessInput == false){ //Determines if the input is valid
                System.out.println("Guess: (H/T)");
                Guess = in.next();
            if (Guess.equals("H") || Guess.equals("T")){
                validGuessInput = true;
            } else {
                JOptionPane.showMessageDialog(null, "Invalid Guess!");
                validGuessInput = false;
            }
        }
        while (validBet == false){
            while (validBetInput == false){
                try {
                    System.out.println("Bet? (Your balance is $" + balance);
                    Bet = in.next();
                    bet = Double.parseDouble(Bet);
                    if (bet > 0){
                        validBetInput = true;
                    }
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "Invalid Bet!");
                }
            }
            if (bet <= balance && bet >= 0){
                validBet = true;
            } else {
                JOptionPane.showMessageDialog(null, "Invalid Bet!");
                validBet = false;
            }
        }
        if (Guess.equals(Side)){
            balance = balance + (bet * 2);
            System.out.println("Correct! The side was: " + Side);
            System.out.println("Your balance is now: $" + balance);
        } else {
            balance = balance - bet;
            System.out.println("Incorrect! The side was: " + Side);
            System.out.println("Your balance is now: $" + balance);
        }
        if (balance == 0){
            System.out.println("You ran out of money. Ending Game.");
            break;
        }
        System.out.println("Go again? (Y/N)");
        GoAgain = in.next();
        while (validFinalInput == false);
            System.out.println("Go again? (Y/N)");
            GoAgain = in.next();
            validGuessInput = false;
            validBet = false;
            validBetInput = false;
        if (GoAgain.equals("Y")){
            goAgain = true;
            validFinalInput = true;
        } else if (GoAgain.equals("N")){
            goAgain = false;
            validFinalInput = true;
            System.out.println("Thanks for playing! You ended with: $" + balance);
        } else {
            System.out.println("Invalid Input!");
            }
        }
   }
}

所以我为“正面或反面”风格的游戏编写了这段代码。它最初可以工作,但后来我决定添加一些功能,但它失败了。基本上,它会结束,但卡在最后一个输入(Y/N)上。

有人可以告诉我我做错了什么吗?

很抱歉没有发表评论。

我也知道我的变量并不完全遵循约定,对此也很抱歉。

谢谢。

4

3 回答 3

5
  1. 去掉这里的分号;

    while (validFinalInput == false);

  2. 正确缩进你的代码。

  3. 在这里检查你的逻辑;您正在为两个条件(true 和 false)设置 validFinalInput = true。

    if (GoAgain.equals("Y")){ ......... validFinalInput = true; }

    否则 if (GoAgain.equals("N")){ ....... validFinalInput = true; }

于 2013-10-08T20:29:35.617 回答
1

goAgain 的值在以 开头的循环中永远不会改变,while (goAgain == true){因为它以}之前的结尾while (validBet == false){。那个右括号放错了位置。

于 2013-10-08T20:28:29.527 回答
0

你有这个while语句

    while (validFinalInput == false);

这是一个无限循环!由于分号,它永远不会超过这部分代码。如果你像这样重写它应该可以工作:

     while (validFinalInput == false)
     {
        System.out.println("Go again? (Y/N)");
        GoAgain = in.next();
        validGuessInput = false;
        validBet = false;
        validBetInput = false;
        if (GoAgain.equals("Y"))
        {
        goAgain = true;
        validFinalInput = true;
        } 
        else if (GoAgain.equals("N"))
        {
            goAgain = false;
            validFinalInput = true;
            System.out.println("Thanks for playing! You ended with: $" + balance);
        } 
        else
        {
            System.out.println("Invalid Input!");
        }
    }
于 2013-10-08T20:37:12.930 回答