为什么这在Visual Studio 2019而不是2022年运行? C#中的方法

发布于 2025-01-22 20:37:02 字数 1413 浏览 0 评论 0原文

试图在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放赐 2025-01-29 20:37:02

正如您的错误消息告诉您的那样,您无法使用相同名称的本地函数进行辩护:

Error   CS0128  A local variable or function named 'ThrowDice' is already defined in this scope

因此,您需要将它们命名为不同或将它们放在这样

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: " + new Dice().ThrowDice (sidor, kast));

public class Dice
{
  public int ThrowDice (int slag)
  {
    return ThrowDice (6, slag);
  }

  public 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;
  }
}

As your error message tells you, you can not delcare to local functions with the same name:

Error   CS0128  A local variable or function named 'ThrowDice' is already defined in this scope

So you need to name them different or put them in a class like this

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: " + new Dice().ThrowDice (sidor, kast));

public class Dice
{
  public int ThrowDice (int slag)
  {
    return ThrowDice (6, slag);
  }

  public 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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文