我把它写成一个简单的骰子游戏。它可以按我的意愿工作,只是它迭代了 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;
}
}
}