2013-11-15 131 views
0

當我嘗試運行我的代碼時,我收到錯誤消息'沒有超載的方法'說話'需要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(); 
     } 

回答

4

你打電話newcritter.talk();(不帶參數),而你的方法需要一個參數public void talk(string m)

所以,你需要一個參數(string)傳遞給talk方法:

newcritter.talk("GOOD"); 
3

talk方法需要一個字符串參數,所以你需要提供一個:

newcritter.talk("fine"); 

而且,你打電話給Console.WriteLine的方式是錯誤的。 0參數

沒有方法talk:試試這個:

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

ErrorDescribes更清楚

我想這:

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

應該是:

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

這裏的代碼調用通話線路:

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

這裏是你的演講確定指標:

public void talk(string m) 

我認爲錯誤是很明顯的。

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(); 
    }