为什么这在Visual Studio 2019而不是2022年运行? C#中的方法
试图在C#中学习方法/函数,但是此代码不会出于某种原因运行,但是它确实在Visual Studio 2019中运行。
编辑:添加了我
static int ThrowDice(int slag)
{
return ThrowDice(6, slag);
}
static int ThrowDice(int sidor, int slag)
{
int resultat = 0;
Random rnd = new Random();
for (int i = 0; i < slag; i++)
{
int varjekast = rnd.Next(1, sidor + 1);
Console.WriteLine($"kast{i+1} blir {varjekast}");
resultat += varjekast;
}
return resultat;
}
Console.WriteLine("Ange tärningens antal sidor: ");
int sidor = int.Parse(Console.ReadLine());
Console.WriteLine("Ange tärningskast: ");
int kast = int.Parse(Console.ReadLine());
Console.WriteLine("Resultat: " + ThrowDice(sidor, kast));
Console.ReadLine();
要低于错误的错误:
Severity Code Description Project File Line Suppression State
Error CS1501 No overload for method 'ThrowDice' takes 2 arguments
Severity Code Description Project File Line Suppression State
Error CS8422 A static local function cannot contain a reference to 'this' or 'base'.
Severity Code Description Project File Line Suppression State
Error CS0128 A local variable or function named 'ThrowDice' is already defined in this scope
Severity Code Description Project File Line Suppression State
Error CS1501 No overload for method 'ThrowDice' takes 2 arguments Rollspel2
Trying to learn method/functions in c# but this code wont run for some reason, but it do run in visual studio 2019.
Edit: Added the errors I'm getting below
static int ThrowDice(int slag)
{
return ThrowDice(6, slag);
}
static int ThrowDice(int sidor, int slag)
{
int resultat = 0;
Random rnd = new Random();
for (int i = 0; i < slag; i++)
{
int varjekast = rnd.Next(1, sidor + 1);
Console.WriteLine(quot;kast{i+1} blir {varjekast}");
resultat += varjekast;
}
return resultat;
}
Console.WriteLine("Ange tärningens antal sidor: ");
int sidor = int.Parse(Console.ReadLine());
Console.WriteLine("Ange tärningskast: ");
int kast = int.Parse(Console.ReadLine());
Console.WriteLine("Resultat: " + ThrowDice(sidor, kast));
Console.ReadLine();
Errors im getting:
Severity Code Description Project File Line Suppression State
Error CS1501 No overload for method 'ThrowDice' takes 2 arguments
Severity Code Description Project File Line Suppression State
Error CS8422 A static local function cannot contain a reference to 'this' or 'base'.
Severity Code Description Project File Line Suppression State
Error CS0128 A local variable or function named 'ThrowDice' is already defined in this scope
Severity Code Description Project File Line Suppression State
Error CS1501 No overload for method 'ThrowDice' takes 2 arguments Rollspel2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您的错误消息告诉您的那样,您无法使用相同名称的本地函数进行辩护:
因此,您需要将它们命名为不同或将它们放在这样
As your error message tells you, you can not delcare to local functions with the same name:
So you need to name them different or put them in a class like this