首页 > 解决方案 > “ 表达式表示一个‘变量’,其中需要一个‘方法组’ “这是什么意思?

问题描述

我正在学习 c# 作为初学者并制作一个程序,该程序为用户提供一个随机数,从骰子直到它得到一个六。这是我的完整代码:

using System;

class HelloWorld {
  static void Main() {
        Random numberGen = new Random();

        int roll = 0;
        int attempts = 0;

        Console.WriteLine("Press enter to roll the die");

        while (roll != 6) {
            Console.ReadKey();

            roll = numberGen(1, 7);
            Console.WriteLine("You rolled " + roll);
            attempts++;
        }

        Console.WriteLine("It took you " + attempts + " to roll a six");
        Console.ReadLine();
  }
}

我做错了什么,我该如何调试它?

标签: c#methods

解决方案


问题在这里:

roll = numberGen(1, 7);

唯一可以使用variable(...)语法的时间variable是类型化委托(在这种情况下编译器将其解释为variable.Invoke(...))。在所有其他情况下,预计您通过变量访问某些方法/属性/字段/索引器/事件,使用 或 之一variable.Foo(...)variable.Foovariable[index]代替->if.是非variable托管指针)。

在这种情况下,您需要:

roll = numberGen.Next(1, 7);

推荐阅读