无法从类中调用方法?

时间:2013-11-15 17:03:44

标签: c# visual-studio-2010

当我尝试运行我的代码时,我收到错误消息“没有方法的重载”谈话'需要0个参数',有人可以帮我调用talk()方法。很抱歉代码很多,但我真的不明白我是不是错了。

class Program
{
    public void Main(string[] args)
    {
        Critter newcritter = new Critter();

        Console.WriteLine("Enter critter name: ");

        var newname = Convert.ToString(Console.ReadLine());

        newcritter.Name = newname;

        Console.WriteLine("Say Hello to your new critter, {0}!", newcritter.Name);

        var option = Convert.ToString(Console.ReadLine());

        while (option != "0")
        {
            Console.WriteLine(@"
            Critter Caretaker

             0 - Quit
              1 - Listen to your critter
              2 - Feed your critter
              3 - Play with your critter
              ");
            if (option == "0")
            {
                Console.WriteLine("Good-bye.");
            }

            if (option == "1")
            {
                newcritter.talk();
            }

class Critter
    {
        public string Name { get; set; }
        public int Hunger = 0;
        public int Boredom = 0;

        public void PassTime()
        {
            Hunger += 1;
            Boredom += 1;
        }

        public void mood()
        {
            var unhappiness = Hunger + Boredom;
            string m = "";
            if (unhappiness < 5)
            {
                m = "Happy";
            }

            if (unhappiness <= 5 && 
                unhappiness <= 10)
            {
                m = "Okay";
            }

            if (unhappiness <= 11 &&
                unhappiness <= 15)
            {
                m = "Frustrated";
            }

            if (unhappiness <= 16)
            {
                m = "Mad";
            }
        }

        public void talk(string m)
        {
            Console.WriteLine("I'm", Name, "and I feel", m, "now.\n");
            PassTime();
        }

5 个答案:

答案 0 :(得分:4)

您正在调用newcritter.talk();(不带参数),而您的方法需要一个参数public void talk(string m)

所以你需要将一个参数(string)传递给talk方法:

newcritter.talk("GOOD");

答案 1 :(得分:3)

talk方法需要一个字符串参数,因此您需要提供一个:

newcritter.talk("fine");

此外,您在此处拨打Console.WriteLine的方式是错误的。试试这个:

public void talk(string m)
{
    Console.WriteLine("I'm {0} and I feel {1} now.", Name, m);
    PassTime();
}

答案 2 :(得分:1)

这是一行调用talk的代码:

 if (option == "1")
 {
     newcritter.talk();
 }

以下是您对谈话的定义:

public void talk(string m)

我认为错误非常明显。

答案 3 :(得分:0)

Error Describes更清楚:

没有方法talk带有0个参数:

我想这个:

if (option == "1")
            {
                newcritter.talk();//error here as there no zero argument talk() method
            }

应该是:

if (option == "1")
            {
               newcritter.talk("somestring");
            }

答案 4 :(得分:0)

您在此代码中遗漏了一些内容。您尚未指定需要执行的操作。 据我所知,你应该在程序课程中。

    while (option != "0")
    {
        Console.WriteLine(@"
        Critter Caretaker

         0 - Quit
          1 - Listen to your critter
          2 - Feed your critter
          3 - Play with your critter
          ");
        if (option == "0")
        {
            Console.WriteLine("Good-bye.");
        }

        if (option == "1")
        {
            newcritter.talk();
        }
         if (option == "2")
        {
            newcritter.PassTime();
        }
        if (option == "3")
        {
            newcritter.mood();
        }
    }

你的Crtter课应该是

class Critter
{
    public string Name { get; set; }
    public int Hunger = 0;
    public int Boredom = 0;
    public string m = "Happy";

   // No Change in PassTime() method and  mood() method
   //
    public void talk()
    {
        Console.WriteLine("I'm", Name, "and I feel", m, "now.\n");
        PassTime();
    }