繁体   English   中英

C#如何从外部方法调用数组列表项

[英]C# How to call array list item from outside method

我对尝试编程非常陌生,甚至不确定如何正确提出问题。 我遇到一个错误,该错误使我无法在用户离开程序时向其显示随机的卡号。 (请参阅第21行: Console.WriteLine("\\n" + "Your lucky card For today is: " + yourCard[luckyFiftyTwo])); 数组,随机化器等都包含在“获取财富”方法中。

我可以将方法切成薄片,再放入两个第二个随机化器,但这会让我重复一行代码来做到这一点,在我看来,我应该能够调用该实例(我再也不知道了如果我说的没错,请随时纠正我,我真的想学习-谢谢)到有人退出程序时。 所以我需要能够从getFortune方法中传递出'yourCard [luckyFiftyTwo]'的结果,并使其可以在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();            
        }
    }
}

一种方法是让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
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM