gpt4 book ai didi

c# - “Expression denotes a ` variable ', where a ` method group' was expected “这是什么意思?

转载 作者:行者123 更新时间:2023-12-05 03:43:10 25 4
gpt4 key购买 nike

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

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

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

最佳答案

问题在这里:

roll = numberGen(1, 7);

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

在这种情况下,您需要:

roll = numberGen.Next(1, 7);

关于c# - “Expression denotes a ` variable ', where a ` method group' was expected “这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66884891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com