你如何接受用户输入?

How do you take an user input?

如何将用户输入作为整数或字符串?

您可以在 C# 或 Python 驱动程序代码中将其作为 Main 方法的一部分来执行此操作,或者您可以使用最近版本中调用的对 Q# 命令行可执行文件的新支持 (0.11.2004.2825). If you follow the link for the quantum random number generator sample and scroll down, you'll see an example there 用于使用 @EntryPoint() 表示应该用于生成入口点代码的 Q# 操作。它还会导致该操作的任何参数自动成为生成的可执行文件的命令行参数。您可以通过更新代码以将 max 作为参数在示例中尝试此操作,如下所示:

@EntryPoint()
operation SampleRandomNumber(max : Int) : Int {
    Message($"Sampling a random number between 0 and {max}: ");
    return SampleRandomNumberInRange(max);
}

然后,当您通过 dotnet run 运行 示例时,您会看到它现在需要 --max 作为命令行参数,并处理将其转换为正确的输入类型Q# 操作。然后您可以像这样传递参数以获得与原始样本相同的行为:dotnet run --max 50

希望对您有所帮助!