2

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

    }
}
4

3 回答 3

4
Again();

Random ran = new Random();

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

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

bool rollAgain = Again();

Random ran = new Random();

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

您在这里有类似的问题:

} while (Again() == true);

要解决所有问题,您可能可以在语句rollAgain外部引入变量并将其分配在内部。do-while

于 2012-06-15T05:01:17.923 回答
0

你打电话Again的次数太多了。改为执行以下操作:

Console.WriteLine("\t\t\tWelcome To a Game of Dice!");            
while (Again()) {
   Random ran = new Random();
   int num1 = ran.Next(1, 7);
   int num2 = ran.Next(1, 7);
   if (num1 == NUM_6 && num2 == NUM_6) { ... 
   else .. // the rest of your checking code here
}

Again所以现在你每个循环只调用一次。如果您单步执行代码(包括单步执行Again),您应该能够看到发生了什么。

于 2012-06-15T05:01:03.897 回答
0

您可以在这里使用while循环,并调用Again一次

while (Again())
{
    Random ran = new Random();
    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);
    }
}
于 2012-06-15T05:13:26.763 回答