0

我对尝试编程很陌生,我什至不知道如何正确地问我的问题。我有一个错误,阻止我在用户离开程序时向他们显示随机卡号。(见第 21 行:Console.WriteLine("\n" + "Your lucky card For today is: " + yourCard[luckyFiftyTwo]));数组、随机发生器等都包含在方法“Get Fortune”中。

我可以将方法切分,然后放入第二个随机器,但这会让我重复一行代码来做到这一点,在我看来,我应该能够调用实例(我又不知道了如果我说的正确,请随时纠正我,我真的很想学习-谢谢)当有人退出程序时。所以我需要能够将'yourCard [luckyFiftyTwo]'的结果从getFortune方法传递到magic8ball方法中/使其可访问。

任何帮助表示赞赏。我希望我已经解释了我正在尝试做的事情。再说一次,我真的很笨,试图做超出老师要求范围的事情——我认为这是一种很好的学习方式(我们甚至还没有使用数组——我可以用 breakCase 语句来做这一切,但我认为数组会更清晰/更紧凑)。

我现在在胡说八道。请帮忙。

代码如下:

using System;

namespace a040___Methods_Array___Crazy8ball
{
    class Program
    {
        //test in chunks
        public void Magic8ball()
        {
            Console.WriteLine(" - - - - - - - -  = = = =  Wisdom of Magic 8 Ball  = = = = - - - - - - - - \n \n ");
            Console.WriteLine("Would you like to consult the wisdom of the Magic 8 Ball? \n");

            PlayAgain();//we do here to ask if they want to play (Y)(N)(?)

            do
            {
                GetFortune();
            } while (PlayAgain()); //while they keep wanting to play

            Console.WriteLine("Thank you for consulting the wisdom of the Magic 8 Ball.");
            Console.WriteLine("\n" + "Your lucky card For today is: " + yourCard[luckyFiftyTwo]);
        }


        public Boolean PlayAgain()
        {
            Console.WriteLine("\n" + "Please enter \"Y\" to play, or \"N\" If you would prefer not to continue to play.");
            String command = Console.ReadLine().ToLower(); //Read in data

            if (command == "y") return true;
            if (command == "n") return false;

            Console.WriteLine("\n" + "What was that? I'm sorry but the Magic 8 Ball didn't understand your entry.");
            return false;
        }


        public void GetFortune()//produces random results
        {
            Random rnd = new Random();
            int luckyEight = rnd.Next(19); // create random number (0 - 20)
            int luckySix = rnd.Next(5); // create random number (0 - 5)
            //int luckyFiftyTwo = rnd.Next(1, 52); // create random number (0 - 52)
            int luckyFiftyTwo = rnd.Next(53); // create random number (1 - 53)


            //type of array - name of array - declaration of array - array size
            String[] responses = new String[20];//array is a square bracket
            responses[0] = "It is certain.";
            responses[1] = "It is decidedly so.";
            responses[2] = "Yes definitely.";
            responses[3] = "You may rely on it.";
            responses[4] = "As I see it yes.";
            responses[5] = "Most likely.";
            responses[6] = "Outlook good.";
            responses[7] = "Yes";
            responses[8] = "Signs point to yes.";
            responses[9] = "Reply hazy try again.";
            responses[10] = "Ask again later.";
            responses[11] = "Better not tell you now.";
            responses[12] = "Cannot predict now.";
            responses[13] = "Concentrate and ask again.";
            responses[14] = "Don't count on it.";
            responses[15] = "My reply is no.";
            responses[16] = "My sources say no.";
            responses[17] = "Outlook not so good.";
            responses[18] = "Very doubtful.";
            responses[19] = "No.";

            //type of array - name of array - declaration of array - array size
            String[] confusiusSays = new String[6];//array is a square bracket
            confusiusSays[0] = "Confucius Say: Help, I'm prisoner in a Chinese bakery!!!\n";
            confusiusSays[1] = "A relationship is the opportunity to do something you hate with someone you love.\n";
            confusiusSays[2] = "It's ok to let a fool kiss you, but don't let a kiss fool you.\n";
            confusiusSays[3] = "Never argue with a fool...he may be doing the same thing.\n";
            confusiusSays[4] = "An Optimist is a girl who regards a bulge as a curve.\n";
            confusiusSays[5] = "A Shotgun wedding is a case of wife or death.\n";

            //type of array - name of array - declaration of array - array size
            String[] yourCard = new String[54];//array is a square bracket
            //joker
            yourCard[0] = "The Joker";
            //clubs
            yourCard[1] = "The One of Clubs";
            yourCard[2] = "The Two of Clubs";
            yourCard[3] = "The Three of Clubs";
            yourCard[4] = "The Four of Clubs";
            yourCard[5] = "The Five of Clubs";
            yourCard[6] = "The Six of Clubs";
            yourCard[7] = "The Seven of Clubs";
            yourCard[8] = "The Eight of Clubs";
            yourCard[9] = "The Nine of Clubs";
            yourCard[10] = "The Ten of Clubs";
            yourCard[11] = "The Jack of Clubs";
            yourCard[12] = "The Queen of Clubs";
            yourCard[13] = "The King of Clubs";
            //diamonds
            yourCard[14] = "The One of Diamonds";
            yourCard[15] = "The Two of Diamonds";
            yourCard[16] = "The Three of Diamonds";
            yourCard[17] = "The Four of Diamonds";
            yourCard[18] = "The Five of Diamonds";
            yourCard[19] = "The Six of Diamonds";
            yourCard[20] = "The Seven of Diamonds";
            yourCard[21] = "The Eight of Diamonds";
            yourCard[22] = "The Nine of Diamonds";
            yourCard[23] = "The Ten of Diamonds";
            yourCard[24] = "The Jack of Diamonds";
            yourCard[25] = "The Queen of Diamonds";
            yourCard[26] = "The King of Diamonds";
            //hearts
            yourCard[27] = "The One of Hearts";
            yourCard[28] = "The Two of Hearts";
            yourCard[29] = "The Three of Hearts";
            yourCard[30] = "The Four of Hearts";
            yourCard[31] = "The Five of Hearts";
            yourCard[32] = "The Six of Hearts";
            yourCard[33] = "The Seven of Hearts";
            yourCard[34] = "The Eight of Hearts";
            yourCard[35] = "The Nine of Hearts";
            yourCard[36] = "The Ten of Hearts";
            yourCard[37] = "The Jack of Hearts";
            yourCard[38] = "The Queen of Hearts";
            yourCard[39] = "The King of Hearts";
            //spade
            yourCard[40] = "The One of Spades";
            yourCard[41] = "The Two of Spades";
            yourCard[42] = "The Three of Spades";
            yourCard[43] = "The Four of Spades";
            yourCard[44] = "The Five of Spades";
            yourCard[45] = "The Six of Spades";
            yourCard[46] = "The Seven of Spades";
            yourCard[47] = "The Eight of Spades";
            yourCard[48] = "The Nine of Spades";
            yourCard[49] = "The Ten of Spades";
            yourCard[50] = "The Jack of Spades";
            yourCard[51] = "The Queen of Spades";
            yourCard[52] = "The King of Spades";
            //second joker
            yourCard[53] = "The Second Joker";

            Console.WriteLine("\n" + "Ask a question and the Magic 8 Ball will return divine insight!");//method stub
            Console.ReadLine(); 

            Console.WriteLine("\n" + "Magic Eight Ball Say: " + responses[luckyEight]);
            Console.WriteLine("\n" + "confusius say, " + confusiusSays[luckySix]);
            Console.WriteLine("\n" + "Your lucky card For today is: " + yourCard[luckyFiftyTwo]);
        }


        static void Main(string[] args) //This is the 'driver' method
        {
            Program myMethodDriver = new Program();
            myMethodDriver.Magic8ball();


            //look up 'Console.close'
            Console.Read();            
        }
    }
}
4

1 回答 1

1

一种方法是GetFortune()返回最后一个结果——将它保存在一个局部变量中:

string lastCard;
do
{
    lastCard = GetFortune();
} while (PlayAgain()); //while they keep wanting to play
Console.WriteLine("Thank you for consulting the wisdom of the Magic 8 Ball.");
Console.WriteLine("\n" + "Your lucky card For today is: " + lastCard);

GetFortune()应该是这样的:

public string GetFortune() //produces random results
{
    // ...

    return yourCard[luckyFiftyTwo];
}

作为一个侧面(语法)说明,您可以使用内联语法初始化数组,如下所示:

String[] responses = new String
{
    "It is certain.",
    "It is decidedly so.",
    "Yes definitely.",
    "You may rely on it.",
    // etc
};
于 2013-10-19T01:07:38.770 回答