循环一个“If Else Statement”java

时间:2016-10-08 22:25:02

标签: java loops if-statement random numbers

我正在尝试使用Scanner在java中创建一个简单的程序,允许用户拍掷掷骰子(玩骰子)。

1.此代码要求用户输入他们有多少钱。

  1. 代码会要求用户输入赌注。

  2. 使用随机数生成器,它会告知用户他们推送了什么,并通知用户他们赢了/输了多少钱。

  3. 我已经成功地告诉用户当他们赢或输时,当他们掷出2,3,7,11或12时,告知用户。

    我不知道怎么告诉计算机允许用户在其他随机值(例如4,5,6,8,9和10)滚动时继续滚动骰子,请帮忙。这是我的代码:

    System.out.println("How much is in your purse?: ");
    purse = input.nextInt();
    
    System.out.println("make a bet: ");
    bet = input.nextInt();
    
    int pNumber = rand.nextInt(12) + 1;
    
    if (pNumber == 2 || pNumber == 3 || pNumber == 12)
    {
        purse = purse - bet;
        System.out.println("you rolled a " + pNumber);
        System.out.println("you lost $" + bet);
    }
    else if (pNumber == 7 || pNumber == 11)
    {
        purse = purse + bet;
        System.out.println("you rolled a " + pNumber);
        System.out.println("you won $" + bet );
    }
    else
    {
        System.out.println("you rolled a " + pNumber + ",keep rolling" );
    }
    

4 个答案:

答案 0 :(得分:1)

使用外部while循环来保持游戏进行,直到用户用完为止。

像这样的东西。从钱包中的初始金额开始,然后开始游戏。

如果用户赢或输,他们会重新下注并再次滚动。

如果用户既不赢也不输,他们只会用当前的赌注再次滚动。

System.out.println("How much is in your purse?: ");


   purse = input.nextInt();
   boolean newBet = true;

    while(purse > 0)
    {
     if(newBet)
     {
      System.out.println("make a bet: ");
      bet = input.nextInt();
     }

     //roll the dice, new bet or not
     int pNumber = rand.nextInt(12) + 1;

     if ( pNumber == 2 || pNumber == 3 || pNumber == 12)
     {
            purse = purse - bet;
            System.out.println("you rolled a " + pNumber);
            System.out.println("you lost $" + bet);
            newBet =true; //make a new bet if you won
     }
     else if (pNumber == 7 || pNumber == 11)
     {
            purse = purse + bet;
            System.out.println("you rolled a " + pNumber);
            System.out.println("you won $" + bet );
            newBet=true; //make a new bet if you lost
     }
     else
     {
       System.out.println("you rolled a " + pNumber + ",keep rolling" );
       newBet=false; // make no new bet, neither won nor lost

     }
   } //end while


  System.out.println("Game over !")

如果用户想要在资金不足之前退出,你可以添加条件来摆脱while循环等。

答案 1 :(得分:0)

键,如果你想继续滚动特定数字,把所有代码放在while循环或do-while循环中。

答案 2 :(得分:0)

这是一个简单的算法,可以帮助描绘实际的代码:

在方法中,将其称为roll,使用随机数生成器来模拟用户的滚动

如果用户的滚动不等于4,5,6,8,9或10,根据您的解释是有问题的数字,请继续 计算

否则,如果用户确实滚动了一个错误号码,请对该号码进行递归调用 滚动法。这将生成另一个随机滚动并再次检查,直到生成“可接受的”滚动

不确定你是否知道递归,但基本上,这是一种自称的方法。

答案 3 :(得分:-2)

boolean keep_rolling = true;

While(keep_rolling== true)
{
System.out.println("How much is in your       purse?: ");
        purse = input.nextInt();
        if(purse==0){
System.out.println("you need more money to continue ");

}else{  

        System.out.println("make a bet: ");
        bet = input.nextInt();



    int pNumber = rand.nextInt(12) + 1;






        if ( pNumber == 2 || pNumber == 3 || pNumber == 12)
        {
            purse = purse - bet;
            System.out.println("you rolled a " + pNumber);
            System.out.println("you lost $" + bet);
keep_rolling= false;
        }
                    else if (pNumber == 7 || pNumber == 11)
                    {
                        purse = purse + bet;
                        System.out.println("you rolled a " + pNumber);
                        System.out.println("you won $" + bet );

        keep_rolling= false;                }
                        else
                        {

System.out.println("you rolled a " +     pNumber + ",keep rolling" );

                                        }
}//end else of purse==o
}//end while