如何循环这个?

时间:2014-10-27 11:52:59

标签: c#

我无法学习如何循环,我坚持如何做到这一点。基本上我被要求编程滚动一个6面模具,它会问你想要滚动多少次。根据你滚动的次数,它会显示每次降落的次数。这是我到目前为止所做的。

using System;

namespace Dice
{
    class Program
    {
        static void Main(string[] args)
        {
            bool continueRunning = true;
            int sessionNumber = 1;

            DisplayInstructions();

            while (continueRunning)
            {
                int howMany = int.Parse(getInfo("How many times do you want to roll the die?"));
                Dice aDice = new Dice();
                aDice.RollDice();

                Console.Clear();
                Console.WriteLine("Session Number: {0}", sessionNumber);
                Console.WriteLine(aDice);

                continueRunning = getYorN("Would you like to run again?");
                sessionNumber++;
                Console.Clear();
            }
        }

        public static bool getYorN(string question)
        {
            bool validInput = false;            

            while (!validInput)
            {
                Console.WriteLine("{0}", question);
                Console.WriteLine("Enter 'yes' or 'no' to continue...");
                string userResponse = Console.ReadLine().ToLower();
                if (userResponse == "yes" || userResponse == "no")
                {
                    validInput = true;
                    switch (userResponse)
                    {
                        case "yes":
                            return true;

                        case "no":
                            return false;

                        default:
                            return false;
                    }

                }
                else
                {
                    Console.Clear();
                    Console.WriteLine("You've entered an invalid term");
                }
            }

            return false;
        }

        public static void DisplayInstructions()
        {
            Console.WriteLine("Welcome to the Dice Game!!");
            Console.WriteLine("\n\n\nThis program will simulate rolling a die and will track the frequency \neach value is rolled.");
            Console.WriteLine("\n\n\nAfter rolling the die, the program will output a summary table for the session.");
            Console.WriteLine("\n\n\nPlease press any key to continue.");
            Console.ReadKey();
            Console.Clear();
        }

        public static string getInfo(string what)
        {
            Console.WriteLine(what);
            return Console.ReadLine();
        }
    }
}

我的课程是

using System;

namespace TripCalcApp
{
    class Dice
    {
        private int side1 = 0, side2 = 0, side3 = 0, side4 = 0, side5 = 0, side6 = 0;

        Random randNum = new Random();

        public Dice()
        {
        }

        public int Side1
        {
            get { return side1; }
            set { side1 = value; }
        }
        public int Side2
        {
            get { return side2; }
            set { side2 = value; }
        }
        public int Side3
        {
            get { return side3; }
            set { side3 = value; }
        }
        public int Side4
        {
            get { return side4; }
            set { side4 = value; }
        }
        public int Side5
        {
            get { return side5; }
            set { side5 = value; }

        }
        public int Side6
        {
            get { return side6; }
            set { side6 = value; }
        }

        public void RollDice()
        //RollDice = randNum.Next(1, 7)
        {

            switch (randNum.Next(1, 7))
            {
                case 1: side1++;
                    break;
                case 2: side2++;
                    break;
                case 3: side3++;
                    break;
                case 4: side4++;
                    break;
                case 5: side5++;
                    break;
                case 6: side6++;
                    break;
            }
        }

        public override string ToString()
        {
            return "                          Freq. Rolls                                         " +
                "________________________________________" +
                "\nSide 1 of Die rolled     :" + side1 +
                "\nSide 2 of Die rolled     :" + side2 +
                "\nSide 3 of Die rolled     :" + side3 +
                "\nSide 4 of Die rolled     :" + side4 +
                "\nSide 5 of Die rolled     :" + side5 +
                "\nSide 6 of Die rolled     :" + side6 +
                "\n";
        }
    }
}

我知道怎么做循环它但我还是不确定。我想到了类似的东西,但它不起作用,我希望你们能帮助我!!

        int howMany = int.Parse(getInfo("How many times would you like to roll the die?"));
        do
        {
            Dice aDice = new Dice();

            for (int counter = howMany; counter > 0; counter--)
            {
                aDice.RollDice();
            }

            while (howMany < 0)
            {
                Console.WriteLine(aDice);
            }
            Console.Clear();
            Console.WriteLine("Session Number: {0}", sessionNumber);
            Console.WriteLine(aDice);
            playAgain = getYorN("Would you like to play again?");

            sessionNumber++;
        }

1 个答案:

答案 0 :(得分:2)

你需要做的就是多次调用Dice.Roll Dice方法:

        while (continueRunning)
        {
            int howMany = int.Parse(getInfo("How many times do you want to roll the die?"));
            Dice aDice = new Dice();
            for(int i = 0; i < howMany; i++)
            {
                aDice.RollDice();
            }

            Console.Clear();
            Console.WriteLine("Session Number: {0}", sessionNumber);
            Console.WriteLine(aDice);

            continueRunning = getYorN("Would you like to run again?");
            sessionNumber++;
            Console.Clear();
        }