c# - “ 表达式表示一个‘变量’,其中需要一个‘方法组’ “这是什么意思?
问题描述
我正在学习 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();
}
}
我做错了什么,我该如何调试它?
解决方案
问题在这里:
roll = numberGen(1, 7);
唯一可以使用variable(...)
语法的时间variable
是类型化委托(在这种情况下编译器将其解释为variable.Invoke(...)
)。在所有其他情况下,预计您通过变量访问某些方法/属性/字段/索引器/事件,使用 或 之一variable.Foo(...)
(variable.Foo
或variable[index]
代替->
if.
是非variable
托管指针)。
在这种情况下,您需要:
roll = numberGen.Next(1, 7);
推荐阅读
- asp.net - 如何使用 asp.net 提交海量数据表单?
- c# - 如何从文本框中更改图像的名称并将其保存到文件夹中?
- c# - 使用安装时已存在的数据创建应用程序
- javascript - 如何将 React-Bootstrap 折叠功能添加到 Ant-Design
- 元素
- machine-learning - 如何使用 keras 深度学习对图像序列进行分类
- python - 具有重复条目的数据透视表,无需使用 scala 和 spark-sql 进行聚合/分组
- ios - UICollectioviewCell 内的 Uiview 的左下角和右下角半径问题
- angular - 动态更改表单控件验证并立即以角度反应形式应用新验证
- java - 清除应用数据后如何退出谷歌?
- image - 用来自另一个文件的 Base64 编码字符串替换电子邮件正文中的 cid:image 引用