简体   繁体   English

我如何在c#中循环一个函数?

[英]How do I loop a function in c#?

I have been given this question, in my computer science class, and I cannot figure out how to answer it.在我的计算机科学课上,我被问到了这个问题,我不知道如何回答。 Question:题:

•Create a procedure called tossCoins with one integer parameter - numTosses • 创建一个名为 tossCoins 的过程,带有一个整数参数 - numTosses

•Inside your tossCoins procedure, call your tossCoin function from challenge #5 numTosses times. •在您的tossCoins 程序中,从挑战#5 numTosses 次调用您的tossCoin 函数。 So for example if we enter tossCoins(50), the tossCoin function will be called 50 times.例如,如果我们输入 tossCoins(50),tossCoin 函数将被调用 50 次。

•Your tossCoins procedures should then print out the number of times that the coin landed on heads and the number of times the coin landed on tails • 然后,您的 tossCoins 程序应该打印出硬币正面朝上的次数和硬币背面朝上的次数

•Extend your main program, so that the user can choose how many times to toss the coin •扩展你的主程序,让用户可以选择抛硬币的次数

I have already created the tossCoin function, but cannot figure out how to run it the amount of times the user asks, or how to create a tally of heads and tails.我已经创建了 tossCoin 函数,但无法弄清楚如何按照用户要求的次数运行它,或者如何创建正面和反面的计数。 This is the code I have so far:这是我到目前为止的代码:

    static void Main(string[] args)
    {
        Random rnd = new Random();

        Console.WriteLine(tossCoin(rnd));

        Console.Write("Please enter a number: ");
        int numTosses = Convert.ToInt16(Console.ReadLine());

        Console.ReadLine();
    }

    static string tossCoin(Random rnd)
    {
        int num = rnd.Next(1, 3);

        string heads = "Heads";
        string tails = "Tails";

        if(num == 1)
        {
            return heads;
        }
        else
        {
            return tails;
        }
    }

    static void tossCoins(int numTosses)
    {
        int headsTally = 0;
        int tailsTally = 0;

        for(int i = 0; i < numTosses; i++)
        {
            string outcome = tossCoin(rnd);
            numTosses--;
            if(outcome == heads)
            {
                headsTally++;
            }
            else
            {
                tailsTally++;
            }
        }
    }
}

If anyone can help, it would be great, as I am trying to learn the different rules of using functions, and procedures.如果有人可以提供帮助,那就太好了,因为我正在尝试学习使用函数和过程的不同规则。 Thanks, Leighton.谢谢,莱顿。

A couple things, first it looks like you've almost got it.有几件事,首先看起来你已经快要明白了。 You just need to call your tossCoins() function in your main method.你只需要在你的主方法中调用你的tossCoins()函数。 Second, you don't need to decrement numTosses in your for loop, ie.其次,您不需要在 for 循环中减少numTosses ,即。 remove the line numTosses--;删除行numTosses--;

EDIT: as Tommy said, the statement outcome == heads wont compile since heads isn't defined.编辑:正如汤米所说,语句outcome == heads不会编译,因为heads没有定义。 Either replace heads with "Heads" or define it earlier in the function.要么用"Heads"替换heads,要么在函数中更早地定义它。 And yea you might want to return/print the result.是的,您可能想要返回/打印结果。

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

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