我正在学习c#。我想出了制作“echo”函数用于练习的想法。我决定制作一个关于狗的程序,并让控制台说出有关狗的行为和属性的信息。问题是随机函数让狗随机做某事,我一直遇到错误。
using System;
using System.Collections.Generic;
class Dog
{
public string Name { get; set; }
public string Breed { get; set; }
public string Size { get; set; }
public string Mood { get; set; }
public string Color { get; set; }
public Dog(string name, string breed, string size, string mood, string color)
{
Name = name;
Breed = breed;
Size = size;
Mood = mood;
Color = color;
}
public void Walk(string place = null)
{
if (place != null)
{
Console.WriteLine($"{Name} the {Breed} is walking to the {place}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is walking somewhere.");
}
}
public void Bark(string emotion = null)
{
if (emotion != null)
{
Console.WriteLine($"{Name} the {Breed} is barking {emotion}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is barking.");
}
}
public void Sleep(string timeOfDay = null)
{
if (timeOfDay != null)
{
Console.WriteLine($"{Name} the {Breed} is sleeping {timeOfDay}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is sleeping.");
}
}
public void Eat(string food = null)
{
if (food != null)
{
Console.WriteLine($"{Name} the {Breed} is eating {food}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is eating.");
}
}
public void Play(string toy = null)
{
if (toy != null)
{
Console.WriteLine($"{Name} the {Breed} is playing with the {toy}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is playing.");
}
}
public void Fetch(string item = null)
{
if (item != null)
{
Console.WriteLine($"{Name} the {Breed} is fetching the {item}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is fetching something.");
}
}
public void Dig(string place = null)
{
if (place != null)
{
Console.WriteLine($"{Name} the {Breed} is digging in the {place}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is digging.");
}
}
public void Swim(string place = null)
{
if (place != null)
{
Console.WriteLine($"{Name} the {Breed} is swimming in the {place}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is swimming.");
}
}
public void RollOver()
{
Console.WriteLine($"{Name} the {Breed} is rolling over.");
}
public void Chase(string target = null)
{
if (target != null)
{
Console.WriteLine($"{Name} the {Breed} is chasing the {target}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is chasing something.");
}
}
public void Sniff(string obj = null)
{
if (obj != null)
{
Console.WriteLine($"{Name} the {Breed} is sniffing the {obj}.");
}
else
{
Console.WriteLine($"{Name} the {Breed} is sniffing.");
}
}
public void WagTail()
{
Console.WriteLine($"{Name} the {Breed} is wagging its tail.");
}
public void Describe()
{
Console.WriteLine($"{Name} is a {Size}, {Color} {Breed} and is currently {Mood}.");
}
public void RandomActions()
{
var actions = new List<Action> { Walk, Bark, Sleep, Eat, Play, Fetch, Dig, Swim, RollOver, Chase, Sniff, WagTail };
var random = new Random();
for (int i = 0; i < 3; i++)
{
int index = random.Next(actions.Count);
actionsindex;
actions.RemoveAt(index);
}
}
}
class Program
{
static void Main()
{
Dog dog1 = new Dog("Debbie", "Labrador", "large", "happy", "golden");
dog1.Describe();
dog1.RandomActions();
Dog dog2 = new Dog("Butch", "Pitbull", "stout", "angry", "grey");
dog2.Describe();
dog2.RandomActions();
}
}
Severity Code Description Project File Line Suppression State
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1950 The best overloaded Add method 'List<Action>.Add(Action)' for the collection initializer has some invalid arguments doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS1503 Argument 1: cannot convert from 'method group' to 'System.Action' doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 158
Error (active) CS0103 The name 'actionsindex' does not exist in the current context doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 163
Error (active) CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement doggy D:\Users\Manny\source\repos\ConsoleApp1\ConsoleApp1\Program.cs 163
我的期望是控制台显示描述狗的句子,然后选择三种随机选择的方法来描述狗在控制台中的句子中所做的动作。
Action
需要无参数方法,但您的方法有可选参数。您可以使用像 () => MyMethod()
这样的 lambda 表达式来添加它们。
var actions = new List<Action> { () => Walk(), () => Bark(), () => Sleep() };