gpt4 book ai didi

c# 迭代前 2 次没有答案。一遍又一遍地输出第三次尝试

转载 作者:行者123 更新时间:2023-11-30 21:06:38 25 4
gpt4 key购买 nike

我把它写成一个简单的骰子游戏。它按我想要的方式工作,除了它迭代 3 次而前两次迭代没有答案。第三次提出并回答问题时,出现输出。

不知道我的代码哪里出了问题,我已经一遍又一遍地检查了....大声笑任何帮助=)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


/* I am very confused but for soem reason it only returns a value every 3 tries....I dont understand
* I looked through the program and the only thing that occurs 3 times is the if else if else if statement
* but I dont see how that would do it....
*/
namespace Lab11
{
class Program
{
static void Main(string[] args)
{//declared constants
const int NUM_1 = 1;
const int NUM_6 = 6;

Console.WriteLine("\t\t\tWelcome To a Game of Dice!");
//Do while statement repeating the "roll again" question and performing the random numbers
do
{
Again();

Random ran = new Random();

//if else if else if statement doing the random numbers and telling you rolled snake or box
if (Again() == true)
{
int num1 = ran.Next(1, 7);
int num2 = ran.Next(1, 7);
//if else statement determining the output for each roll in the console.
if (num1 == NUM_6 && num2 == NUM_6)
{
Console.WriteLine("\nYou Rolled BoxCars");
}
else if (num1 == NUM_1 && num2 == NUM_1)
{
Console.WriteLine("\nYou rolled Snake-Eyes");
}
else
{
Console.WriteLine("\nYou Rolled...{0} and {1}", num1, num2);
}
}
} while (Again() == true);
//Goodbye if you press 'n'
Console.WriteLine("\n\nGoodBye");
Console.ReadLine();

}
//the yes or no method asking if you want to play again
//this is where I think the issue is but I dont see where or how....
static bool Again()
{
char response;

Console.Write("\n\nWould you like to roll the dice (y or n)? ");
response = char.Parse(Console.ReadLine());
response = char.ToLower(response);

if (response == 'y')
return true;
else
return false;
}

}
}

最佳答案

Again();

Random ran = new Random();

if (Again() == true)
{
// ...

在这里,您调用了 Again() 两次,这导致它提示用户两次。你可以这样改变它:

bool rollAgain = Again();

Random ran = new Random();

if (rollAgain == true)
{
// ...

你在这里有类似的问题:

} while (Again() == true);

要解决所有问题,您可能可以在 do-while 语句外引入 rollAgain 变量并在其中赋值。

关于c# 迭代前 2 次没有答案。一遍又一遍地输出第三次尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com