{"id":309400,"date":"2024-05-20T21:24:54","date_gmt":"2024-05-20T13:24:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/309400.html"},"modified":"2024-05-20T21:25:08","modified_gmt":"2024-05-20T13:25:08","slug":"%e8%bf%99%e9%81%93%e9%a2%98%e9%aa%b0%e5%ad%90%e6%b8%b8%e6%88%8f%e7%94%a8c%e6%80%8e%e4%b9%88%e5%81%9a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/309400.html","title":{"rendered":"\u8fd9\u9053\u9898\u9ab0\u5b50\u6e38\u620f\u7528C#\u600e\u4e48\u505a"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26225813\/19732fcc-2ba7-466e-96fb-55dc0ece4db1.webp\" alt=\"\u8fd9\u9053\u9898\u9ab0\u5b50\u6e38\u620f\u7528C#\u600e\u4e48\u505a\" \/><\/p>\n<p><p>\u5728C#\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u9ab0\u5b50\u6e38\u620f\u6d89\u53ca\u5230<strong>\u751f\u6210\u968f\u673a\u6570\u3001\u63a7\u5236\u6e38\u620f\u6d41\u7a0b\u3001\u4ee5\u53ca\u8bc4\u5224\u6e38\u620f\u7ed3\u679c<\/strong>\u3002\u5728\u6e38\u620f\u7f16\u5199\u8fc7\u7a0b\u4e2d\uff0c<strong>\u751f\u6210\u968f\u673a\u6570<\/strong>\u662f\u6700\u4e3a\u5173\u952e\u7684\u4e00\u6b65\u3002\u5b83\u786e\u4fdd\u4e86\u6e38\u620f\u7684\u516c\u5e73\u6027\u548c\u4e0d\u53ef\u9884\u6d4b\u6027\uff0c\u4f7f\u5f97\u73a9\u5bb6\u6bcf\u6b21\u63b7\u9ab0\u5b50\u7684\u7ed3\u679c\u90fd\u662f\u968f\u673a\u7684\u3002C#\u4e2d\u7684Random\u7c7b\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u7406\u89e3\u6e38\u620f\u9700\u6c42<\/h2>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u660e\u786e\u9ab0\u5b50\u6e38\u620f\u7684\u89c4\u5219\u3002\u901a\u5e38\uff0c\u4e00\u4e2a\u7b80\u5355\u7684\u9ab0\u5b50\u6e38\u620f\u53ef\u80fd\u5305\u62ec\u63b7\u9ab0\u5b50\u7684\u6b21\u6570\u3001\u51b3\u5b9a\u80dc\u8d1f\u7684\u6761\u4ef6\u7b49\u3002\u4e86\u89e3\u548c\u786e\u5b9a\u4e86\u6e38\u620f\u89c4\u5219\u540e\uff0c\u624d\u80fd\u8fdb\u884c\u9ad8\u6548\u7684\u4ee3\u7801\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>\u6e38\u620f\u89c4\u5219\u5b9a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u8bbe\u8ba1\u6e38\u620f\u4e4b\u521d\uff0c\u9700\u8981\u786e\u5b9a\u6e38\u620f\u7684\u53c2\u4e0e\u4eba\u6570\u3001\u6bcf\u4eba\u63b7\u9ab0\u5b50\u7684\u6b21\u6570\u4ee5\u53ca\u5982\u4f55\u51b3\u5b9a\u80dc\u8d1f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8bbe\u5b9a\u6e38\u620f\u4e3a\u53cc\u4eba\u5bf9\u6218\uff0c\u6bcf\u4eba\u63b7\u4e24\u6b21\u9ab0\u5b50\uff0c\u6839\u636e\u4e24\u6b21\u7684\u603b\u548c\u6765\u51b3\u5b9a\u80dc\u8005\u3002<\/p>\n<\/p>\n<p><h3>\u6e38\u620f\u6d41\u7a0b\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u7d27\u968f\u6e38\u620f\u89c4\u5219\u4e4b\u540e\u7684\u662f\u6e38\u620f\u6d41\u7a0b\u7684\u8bbe\u8ba1\u3002\u6e38\u620f\u6d41\u7a0b\u5305\u62ec\u4e86\u6e38\u620f\u7684\u5f00\u59cb\u3001\u8fdb\u884c\u4e2d\u7684\u5404\u6b65\u64cd\u4f5c\u3001\u4ee5\u53ca\u6e38\u620f\u7ed3\u675f\u540e\u7684\u80dc\u8d1f\u8bc4\u5224\u6807\u51c6\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efaC#\u9879\u76ee<\/h2>\n<\/p>\n<p><p>\u5728\u7406\u89e3\u4e86\u6e38\u620f\u7684\u57fa\u672c\u8981\u6c42\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u5728Visual Studio\u6216\u5176\u4ed6C#\u652f\u6301\u7684IDE\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Console Application\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><h3>\u9879\u76ee\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u9879\u76ee\u540e\uff0c\u786e\u4fdd\u6240\u6709\u5fc5\u8981\u7684\u4f9d\u8d56\u548c\u73af\u5883\u90fd\u5df2\u7ecf\u8bbe\u7f6e\u597d\u3002\u5bf9\u4e8e\u4e00\u4e2a\u7b80\u5355\u7684\u9ab0\u5b50\u6e38\u620f\uff0c\u6211\u4eec\u53ef\u80fd\u4e0d\u9700\u8981\u6dfb\u52a0\u592a\u591a\u989d\u5916\u7684\u5305\u6216\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5f15\u5165\u547d\u540d\u7a7a\u95f4System\uff0c\u8fd9\u4e2a\u547d\u540d\u7a7a\u95f4\u4e0b\u542b\u6709\u7684Random\u7c7b\u5c06\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u6a21\u62df\u63b7\u9ab0\u5b50\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u751f\u6210\u968f\u673a\u6570\u5b9e\u73b0\u63b7\u9ab0\u5b50<\/h2>\n<\/p>\n<p><p>\u5728C#\u4e2d\uff0cRandom\u7c7b\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\uff0c\u975e\u5e38\u9002\u5408\u7528\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\u7684\u968f\u673a\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>Random\u7c7b\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Random\u7c7b\u7684Next\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u968f\u673a\u6570\u3002\u9ab0\u5b50\u7684\u9762\u6570\u901a\u5e38\u662f6\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528Random\u5bf9\u8c61\u7684Next(1, 7)\u65b9\u6cd5\u6765\u6a21\u62df\u63b7\u51fa\u4e00\u4e2a1\u52306\u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-csharp\">Random random = new Random();<\/p>\n<p>int diceRoll = random.Next(1, 7);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6a21\u62df\u591a\u6b21\u63b7\u9ab0\u5b50<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6e38\u620f\u89c4\u5219\u4e2d\u5305\u542b\u591a\u6b21\u63b7\u9ab0\u5b50\u7684\u8fc7\u7a0b\uff0c\u53ea\u9700\u8981\u901a\u8fc7\u5faa\u73af\u7ed3\u6784\u8c03\u7528\u4e0a\u8ff0\u4ee3\u7801\u591a\u6b21\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u63a7\u5236\u6e38\u620f\u6d41\u7a0b<\/h2>\n<\/p>\n<p><p>\u6839\u636e\u6e38\u620f\u89c4\u5219\u7684\u4e0d\u540c\uff0c\u6e38\u620f\u6d41\u7a0b\u7684\u63a7\u5236\u53ef\u80fd\u5305\u62ec\u591a\u8f6e\u63b7\u9ab0\u3001\u5207\u6362\u73a9\u5bb6\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u5b9e\u73b0\u73a9\u5bb6\u8f6e\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u4eba\u6e38\u620f\u4e2d\uff0c\u9700\u8981\u63a7\u5236\u73a9\u5bb6\u7684\u8f6e\u6362\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u7ed3\u6784\u5b9e\u73b0\uff0c\u5728\u6bcf\u8f6e\u7ed3\u675f\u65f6\u5207\u6362\u5230\u4e0b\u4e00\u4e2a\u73a9\u5bb6\u3002<\/p>\n<\/p>\n<p><h3>\u5224\u65ad\u6e38\u620f\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u8bc4\u5224\u8c01\u662f\u6e38\u620f\u7684\u80dc\u8005\u3002\u6839\u636e\u6bcf\u540d\u73a9\u5bb6\u7684\u5f97\u5206\uff0c\u7528if\u6216\u8005switch\u8bed\u53e5\u6765\u51b3\u5b9a\u8c01\u83b7\u80dc\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5c55\u793a\u7ed3\u679c\u548c\u7ed3\u675f\u6e38\u620f<\/h2>\n<\/p>\n<p><p>\u6700\u540e\u8bb0\u5f97\u5728\u6e38\u620f\u7ed3\u675f\u65f6\u5c55\u793a\u6e38\u620f\u7ed3\u679c\u5e76\u63d0\u4f9b\u73a9\u5bb6\u53cd\u9988\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u679c\u5c55\u793a<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Console.WriteLine\u65b9\u6cd5\u6765\u5411\u73a9\u5bb6\u5c55\u793a\u6bcf\u8f6e\u7684\u7ed3\u679c\u4ee5\u53ca\u6700\u7ec8\u80dc\u8d1f\u3002<\/p>\n<\/p>\n<p><h3>\u6e38\u620f\u91cd\u542f\u6216\u9000\u51fa<\/h3>\n<\/p>\n<p><p>\u5411\u73a9\u5bb6\u63d0\u4f9b\u9009\u9879\uff0c\u662f\u5426\u7ee7\u7eed\u6e38\u620f\u6216\u9000\u51fa\u3002\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8f93\u5165\u8bfb\u53d6\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u4f7f\u7528C#\u7f16\u5199\u9ab0\u5b50\u6e38\u620f\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u60a8\u53ef\u4ee5\u7528C#\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u9ab0\u5b50\u6e38\u620f\uff1a<\/p>\n<ol>\n<li>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684C#\u63a7\u5236\u53f0\u5e94\u7528\u7a0b\u5e8f\u9879\u76ee\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u7a0b\u5e8f\u7684\u4e3b\u51fd\u6570\u4e2d\u6dfb\u52a0\u4ee3\u7801\u6765\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\uff0c\u8be5\u968f\u673a\u6570\u53ef\u4ee5\u6a21\u62df\u4e00\u4e2a\u9ab0\u5b50\u7684\u6295\u63b7\u7ed3\u679c\u3002<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\uff0c\u6839\u636e\u968f\u673a\u6570\u7684\u7ed3\u679c\uff0c\u8f93\u51fa\u76f8\u5e94\u7684\u9ab0\u5b50\u9762\u7684\u56fe\u50cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u6765\u6a21\u62df\u591a\u6b21\u6295\u63b7\u9ab0\u5b50\u7684\u8fc7\u7a0b\uff0c\u5e76\u7edf\u8ba1\u6bcf\u4e2a\u9762\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u540e\uff0c\u8f93\u51fa\u6bcf\u4e2a\u9ab0\u5b50\u9762\u7684\u51fa\u73b0\u6b21\u6570\u4ee5\u53ca\u6700\u5e38\u51fa\u73b0\u7684\u9762\u3002<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u9ab0\u5b50\u6e38\u620f\u7684C#\u4ee3\u7801\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><\/p>\n<p>\u5728\u7f16\u5199\u9ab0\u5b50\u6e38\u620f\u7684C#\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u5408\u9002\u7684\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u4ee5\u786e\u4fdd\u9ab0\u5b50\u7684\u6295\u63b7\u7ed3\u679c\u662f\u968f\u673a\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u6bcf\u4e2a\u9ab0\u5b50\u9762\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u4ee5\u4fbf\u540e\u7eed\u7edf\u8ba1\u548c\u5206\u6790\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u6765\u6a21\u62df\u591a\u6b21\u6295\u63b7\u9ab0\u5b50\u7684\u8fc7\u7a0b\uff0c\u5e76\u907f\u514d\u91cd\u590d\u7684\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5408\u9002\u7684\u8f93\u51fa\u683c\u5f0f\u6765\u663e\u793a\u6bcf\u4e2a\u9ab0\u5b50\u9762\u7684\u51fa\u73b0\u6b21\u6570\u4ee5\u53ca\u6700\u5e38\u51fa\u73b0\u7684\u9762\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c3d\u53ef\u80fd\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u65b9\u6cd5\uff0c\u5c06\u9ab0\u5b50\u6e38\u620f\u7684\u5404\u4e2a\u529f\u80fd\u5c01\u88c5\u6210\u72ec\u7acb\u7684\u7c7b\u548c\u65b9\u6cd5\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u8ba9C#\u7f16\u5199\u7684\u9ab0\u5b50\u6e38\u620f\u6709\u66f4\u597d\u7684\u7528\u6237\u754c\u9762\uff1f<\/strong><\/p>\n<p>\u8981\u8ba9C#\u7f16\u5199\u7684\u9ab0\u5b50\u6e38\u620f\u6709\u66f4\u597d\u7684\u7528\u6237\u754c\u9762\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u5e93\uff0c\u5982Windows Forms\u6216WPF\uff0c\u6765\u521b\u5efa\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u754c\u9762\u4e2d\u4f7f\u7528\u9002\u5f53\u7684\u63a7\u4ef6\u6765\u663e\u793a\u9ab0\u5b50\u56fe\u50cf\u548c\u6295\u63b7\u7ed3\u679c\uff0c\u5982\u56fe\u7247\u6846\u548c\u6807\u7b7e\u63a7\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u6dfb\u52a0\u6309\u94ae\u63a7\u5236\u6765\u6a21\u62df\u9ab0\u5b50\u7684\u6295\u63b7\u52a8\u4f5c\uff0c\u5e76\u663e\u793a\u6295\u63b7\u7ed3\u679c\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5408\u9002\u7684\u5e03\u5c40\u548c\u89c6\u89c9\u6548\u679c\u6765\u4f7f\u754c\u9762\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>\u8003\u8651\u6dfb\u52a0\u97f3\u6548\u548c\u52a8\u753b\u6548\u679c\u6765\u589e\u52a0\u6e38\u620f\u7684\u8da3\u5473\u6027\u548c\u5438\u5f15\u529b\u3002<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5728C#\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u9ab0\u5b50\u6e38\u620f\u6d89\u53ca\u5230\u751f\u6210\u968f\u673a\u6570\u3001\u63a7\u5236\u6e38\u620f\u6d41\u7a0b\u3001\u4ee5\u53ca\u8bc4\u5224\u6e38\u620f\u7ed3\u679c\u3002\u5728\u6e38\u620f\u7f16\u5199\u8fc7\u7a0b\u4e2d\uff0c\u751f\u6210\u968f\u673a\u6570\u662f\u6700\u4e3a\u5173 [&hellip;]","protected":false},"author":3,"featured_media":309417,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309400"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=309400"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/309400\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/309417"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=309400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=309400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=309400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}