简体   繁体   English

循环“ If Else语句” java

[英]Looping an “If Else Statement” java

I am attempting to make a simple program in java using Scanner that will allow the user to shoot craps (play dice). 我正在尝试使用Scanner在Java中制作一个简单的程序,该程序将允许用户射击胡扯(玩骰子)。

1.This code asks the user to enter how much money they have. 1.此代码要求用户输入他们有多少钱。

  1. The code will ask user to input a bet. 该代码将要求用户输入一个赌注。

  2. Using a random number generator, it will inform the user what they rolled and inform the user how much money they've won/lost. 使用随机数生成器,它将通知用户他们滚了什么,并通知用户他们赢了/亏了多少钱。

I have been able to successfully tell the computer to inform the user when they have won or lost, when they roll 2,3,7,11, or 12. 我已经能够成功地告诉计算机,当他们赢了或输了,何时掷出2,3、7、11或12时,通知用户。

I am not sure how to tell the computer to allow the user to keep rolling the dice when other random values, such as 4,5,6,8,9, and 10 are rolled though, please help. 我不确定当其他随机值(例如4,5、6、8、9和10)滚动时,如何告诉计算机允许用户继续滚动骰子,请提供帮助。 Here's my code: 这是我的代码:

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" );
}

Use an outer while loop to keep the game going until the user runs out of money. 使用外部while循环使游戏继续进行,直到用户用完钱为止。

Something like this. 这样的事情。 Start with an initial amount in the purse, then start the game. 从钱包里的初始金额开始,然后开始游戏。

If the user wins or loses, they make a new bet and roll again. 如果用户获胜或失败,他们将进行新的下注并再次滚动。

If the user neither wins nor loses, they just roll again with the current bet. 如果用户既没有赢也没有输,他们只是用当前的赌注再次滚动。

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 !")

You could add conditions to break out of the while loop if the user wants to quit before they run out of money, etc. 如果用户想在钱用光之前退出,则可以添加条件以打破while循环。

关键,如果要继续滚动特定数字,请将所​​有代码放入while循环或do-while循环中。

Here's a simple algorithm in plain words that may help to picture the actual code: 这是一个简单的简单算法,可能有助于描述实际代码:


In a method, call it roll, use a random number generator to simulate the user's roll 在一种方法中,将其称为滚动,使用随机数生成器来模拟用户的滚动

If the user's roll does not equal 4,5,6,8,9, or 10, which according to your explanation are the problematic numbers, proceed with your calculations 如果用户的点数不等于4,5、6、8、9或10(根据您的解释是有问题的数字),请继续进行计算

Otherwise, if the user did in fact roll a bad number, make a recursive call to the roll method. 否则,如果用户实际上确实滚动了一个错误的数字,请对roll方法进行递归调用。 This will generate another random roll and go through the checks again until an "acceptable" roll is generated 这将生成另一个随机滚动,并再次进行检查,直到生成“可接受的”滚动


Not sure if you know about recursion yet, but basically, it's a method calling itself. 不确定您是否知道递归,但是基本上,这是一个调用自身的方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM