[英]“ Expression denotes a `variable', where a `method group' was expected “ what does this mean?
我正在學習 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]
(或->
in .
如果variable
是非托管指針的位置)。
在這種情況下,您需要:
roll = numberGen.Next(1, 7);
在您的代碼中,您創建了一個名為 - 'numberGen' 的變量。 由於它是 class '隨機'的變量,因此您需要使用此變量調用此 class 的方法,例如 -
numberGen.Next(1,7);
這里的'Next'是一個class Random的方法,它有兩個參數,最小值和最大值。
您得到的錯誤是因為您將變量用作方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.