【问题标题】:How do I update and print an int whenever for-loop is ran每当运行 for 循环时,如何更新和打印 int
【发布时间】:2021-01-22 19:08:39
【问题描述】:

我制作了一个程序,让用户进行 10 轮投注,以尽可能多地使用投注来赚钱。用户下注,然后选择正面或反面。循环有时有效,但有时它不会从总金额中增加或减少赌注。 for 循环是在跟踪所有的输赢还是什么?

代码如下:

package wallonsProject;

import java.util.Scanner;

public class WallonProject {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Scanner userInput = new Scanner(System.in);
        
        int totalMoney = 100000;
        
        //Coin faces
        boolean isHeads = false;
        boolean isTails = false;
        
        //CoinToss result
        int coinTossResult;
        
        //number of rounds
        int numOfRounds = 10;

            for(int i = 0; i < numOfRounds; i++)
            {

                //Betting and user Choice
                System.out.println("How much would you like to bet?");
                int userBet = userInput.nextInt();
                
                //User bet must be less than the total money they have or else game exits
                if(userBet > totalMoney)
                {
                    System.exit(0);
                }
                
                System.out.println("Press 1 for heads, or 2 for tails");
                int userChoice = userInput.nextInt();
                
                System.out.println("Your starting money is " + totalMoney);

                //coinToss
                double coinToss = Math.random();
                {
                    if(coinToss >= 0.5)
                    {
                        isHeads = true;
                        System.out.println("Toss is heads");
                        coinTossResult = 1;
                        System.out.println("The toss is " + coinTossResult);
                    }
                    if(coinToss < 0.5)
                    {
                        isTails = true;
                        System.out.println("Toss is tails");
                        coinTossResult = 2;
                        System.out.println("The toss is " + coinTossResult);
                    }
                    
                    System.out.println("You chose " + userChoice);
                    
                    //All different outcomes of WINNING results
                    if(isHeads == true && userChoice == 1)//if your choice is heads and cointossresult is heads
                    {
                        System.out.println("You win");
                        totalMoney = totalMoney + userBet;
                    }
                    if(isTails == true && userChoice == 2)//if your choice is tails and cointossresult is tails
                    {
                        System.out.println("You win");
                        totalMoney = totalMoney + userBet;
                    }
                    
                    //All different outcomes of LOSING results
                    if(isHeads == true && userChoice == 2)
                    {
                        System.out.println("You lose");
                        totalMoney = totalMoney + userBet;
                    }
                    if(isTails == true && userChoice == 1)
                    {
                        System.out.println("You lose");
                        totalMoney = totalMoney - userBet;
                    }
                    
                    
                    System.out.println("You bet " + userBet);
                    System.out.println("Your total money is " + totalMoney);
            }

        }

    }        

}

【问题讨论】:

  • 这不是您的问题,但您需要在每次迭代后重置isHeadsisTails,否则它们最终都会卡在true。为什么不只使用一个变量 true 表示正面,false 表示反面,从而避免这种风险?编辑:实际上,这就是确切的问题。让我把这个写下来。
  • 您好,您的问题得到解答了吗?如果是这样,您能否接受并投票赞成答案?如果不是,可以澄清什么?

标签: for-loop if-statement integer


【解决方案1】:

在你的 for 循环之外,你定义了两个变量:

boolean isHeads = false;
boolean isTails = false;

在循环中,如果你翻转正面,则使 isHeads 为真,如果你翻转反面,则使 isTails 为真。不幸的是,您从未重置它们,因此它们“卡在”true 位置。

因为他们被卡住了,你后来在你的条件列表中遇到了两个条件,而不仅仅是一个。你加然后减去赌注,所以总数不会改变。


还有一件小事:

                if(isHeads == true && userChoice == 2)
                {
                    System.out.println("You lose");
                    totalMoney = totalMoney + userBet;
                }

就在那儿,你可能想减去赌注,就像你做的那样。


这不是修复代码的最干净的方法,但它是最少的输入:作为循环中的最后一条指令,您应该通过将两者都设置为 false 来清除 isHeadsisTails。一种更简洁的方法需要进行一些重构:只需使用一个对头为真、对尾为假的变量,这样就可以避免这种风险。然后每次翻转时,无论如何都要重置它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多