[英]game of craps dice roll stuck in infinte loop
I am trying to make a program for a game of craps where the user enters a bet amount and 2 six sided dice are rolled if 7 or 11 are rolled they win.我正在尝试为掷骰子游戏制作一个程序,其中用户输入下注金额,如果掷出 7 或 11,则掷出 2 个六面骰子,他们赢了。 if 2,3 or 12 roll they lose, if any other number is rolled it displays the number as a point.It will keep rolling the dice until either 7 or the point is rolled, if 7 is rolled they lose, else they win.如果掷出 2,3 或 12 他们就输了,如果掷出任何其他数字,它会将数字显示为一个点。它将继续掷骰子,直到掷出 7 或点数,如果掷出 7,他们就输了,否则他们就赢了。 For some reason is rolls the dice rolls more then once when rolling the dice to see if they won or lost.出于某种原因,掷骰子时掷骰子的次数比掷骰子的次数要多,以查看他们是赢还是输。 I am not sure how to fix this any help would be appericated我不知道如何解决这个问题,任何帮助都会得到帮助
static int RollDice()
{
int roll;
Random random = new Random();
int die1 = 0;
int die2 = 0;
die1 = random.Next(1,6);
die2 = random.Next(1,6);
roll = die1 + die2;
Console.WriteLine($"You rolled {die1} + {die2} = {roll}");
return roll;
}
static void processCraps()
{
string gameStatus;
double betAmount;
double netWinning = 0;
int point;
do
{
Console.WriteLine("Enter the amount to bet");
betAmount = double.Parse(Console.ReadLine());
if (RollDice() == 2 || RollDice() == 3 || RollDice() == 12)
{
Console.WriteLine($"You lost {betAmount}");
netWinning = netWinning - betAmount;
}
else if (RollDice() == 7 || RollDice() == 11)
{
Console.WriteLine($"You won {betAmount}");
netWinning = netWinning + betAmount;
}
else if (RollDice() != 2 || RollDice() != 3 || RollDice() != 12 || RollDice() != 7 || RollDice() != 11)
{
point = RollDice();
Console.WriteLine($"Point is {point}");
for(int rollCount =0; rollCount <= point; RollDice() )
{
if(RollDice() == 7)
{
Console.WriteLine($"You lost {betAmount}");
netWinning = netWinning - betAmount;
}
else if(RollDice() == point)
{
Console.WriteLine($"You won {betAmount}");
netWinning = netWinning + betAmount;
}
}
}
Console.WriteLine("Do you want to play again (y/n)");
gameStatus = Console.ReadLine();
} while (gameStatus == "y") ;
Console.WriteLine($"Your net winning is {netWinning}");
}
The problem is your if
statements.问题是你的if
语句。 Here for example这里例如
if (RollDice() == 2 || RollDice() == 3 || RollDice() == 12)
Every one of those RollDice()
is a call to the function.每一个RollDice()
都是对 function 的调用。 If you only want to roll once, call the function once and assign the result to a variable and check it, thusly:如果您只想滚动一次,请调用 function 一次并将结果分配给变量并检查它,因此:
int roll = RollDice();
if (roll == 2 || roll == 3 || roll == 12)
You are calling RollDice too many times.你调用 RollDice 的次数太多了。 Call it once after they enter the bet amount and store the result in a variable.在他们输入下注金额后调用一次并将结果存储在变量中。 Then use that variable to check the value that was rolled eg然后使用该变量检查滚动的值,例如
static void processCraps()
{
string gameStatus;
double betAmount;
double netWinning = 0;
int point;
do
{
Console.WriteLine("Enter the amount to bet");
betAmount = double.Parse(Console.ReadLine());
var diceRoll = RollDice();
if (diceRoll == 2 || diceRoll == 3 || diceRoll == 12)
{
Console.WriteLine($"You lost {betAmount}");
netWinning = netWinning - betAmount;
}
... // repeat for the other rolls
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.