{"id":294350,"date":"2024-05-20T15:08:04","date_gmt":"2024-05-20T07:08:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/294350.html"},"modified":"2024-05-20T15:08:11","modified_gmt":"2024-05-20T07:08:11","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88nextint%e5%92%8cnum%e4%bc%9a%e6%8a%a5%e7%ba%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/294350.html","title":{"rendered":"\u4e3a\u4ec0\u4e48nextInt\u548cnum\u4f1a\u62a5\u7ea2"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27141354\/675e85bb-dd66-4ca9-9756-ce41f242a297.webp\" alt=\"\u4e3a\u4ec0\u4e48nextInt\u548cnum\u4f1a\u62a5\u7ea2\" \/><\/p>\n<p><p>\u5728Java\u7f16\u7a0b\u4e2d\uff0c\u5f53\u60a8\u9047\u5230<code>nextInt<\/code>\u548c<code>num<\/code>\u8fd9\u4e24\u4e2a\u540d\u8bcd\u65f6\u62a5\u7ea2\uff0c\u5e38\u89c1\u539f\u56e0\u4e3b\u8981\u5305\u62ec<strong>\u672a\u5bfc\u5165\u76f8\u5e94\u7684\u5e93\u3001\u53d8\u91cf\u672a\u58f0\u660e\u524d\u4f7f\u7528\u3001\u65b9\u6cd5\u8c03\u7528\u4e0d\u6b63\u786e<\/strong>\u3001\u4ee5\u53ca<strong>\u4e0a\u4e0b\u6587\u73af\u5883\u9519\u8bef<\/strong>\u3002\u7279\u522b\u5730\uff0c<strong>\u672a\u5bfc\u5165\u76f8\u5e94\u7684\u5e93<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u95ee\u9898\u4e4b\u4e00\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528<code>Scanner<\/code>\u7c7b\u7684<code>nextInt()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6574\u6570\u524d\uff0c\u9700\u8981\u5148\u5bfc\u5165Java\u7684<code>util<\/code>\u5305\uff0c\u901a\u8fc7<code>import java.util.Scanner;<\/code>\u8bed\u53e5\u3002\u5982\u679c\u672a\u5bfc\u5165\u8fd9\u4e2a\u5305\uff0c\u4efb\u4f55\u5c1d\u8bd5\u4f7f\u7528<code>Scanner<\/code>\u7c7b\u529f\u80fd\u7684\u4ee3\u7801\u90fd\u4f1a\u62a5\u7ea2\uff0c\u63d0\u793a\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6240\u9700\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u5df2\u7ecf\u6b63\u786e\u5bfc\u5165\u4e86\u9700\u8981\u4f7f\u7528\u7684\u7c7b\u6216\u63a5\u53e3\u3002\u4f8b\u5982\uff0c\u5f53\u4f7f\u7528<code>Scanner<\/code>\u7c7b\u8fdb\u884c\u8f93\u5165\u64cd\u4f5c\u65f6\uff0cJava\u73af\u5883\u8981\u6c42\u5148\u5bfc\u5165\u8be5\u7c7b\u6240\u5728\u7684\u5305\u3002<\/p>\n<\/p>\n<p><p>\u672a\u5bfc\u5165<code>java.util.Scanner<\/code>\u4f1a\u5bfc\u81f4\u8c03\u7528<code>nextInt<\/code>\u65b9\u6cd5\u65f6\u51fa\u73b0\u7f16\u8bd1\u9519\u8bef\uff0c\u56e0\u4e3aJava\u7f16\u8bd1\u5668\u65e0\u6cd5\u8bc6\u522b\u672a\u5bfc\u5165\u7c7b\u7684\u65b9\u6cd5\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u5728\u6587\u4ef6\u9876\u90e8\u52a0\u5165<code>import java.util.Scanner;<\/code>\u8bed\u53e5\uff0c\u786e\u4fdd<code>Scanner<\/code>\u7c7b\u53ef\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5bfc\u5165<code>java.util.Scanner<\/code>\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>Scanner<\/code>\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5b83\u6765\u63a5\u53d7\u7528\u6237\u8f93\u5165\u3002\u4f8b\u5982\uff0c<code>Scanner scanner = new Scanner(System.in);<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684<code>Scanner<\/code>\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u4ece\u6807\u51c6\u8f93\u5165\uff08\u952e\u76d8\uff09\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u53d8\u91cf\u58f0\u660e\u548c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728Java\u4e2d\uff0c\u53d8\u91cf\u5728\u4f7f\u7528\u524d\u5fc5\u987b\u88ab\u58f0\u660e\uff0c\u5e76\u4e14\u5b83\u7684\u4f7f\u7528\u5e94\u8be5\u5728\u5176\u4f5c\u7528\u57df\u4e4b\u5185\u3002<\/p>\n<\/p>\n<p><p><strong>\u53d8\u91cf<code>num<\/code>\u672a\u58f0\u660e\u6216\u8d85\u51fa\u4f5c\u7528\u57df<\/strong>\u4e5f\u4f1a\u5bfc\u81f4\u62a5\u7ea2\u3002\u5982\u679c\u4f60\u5c1d\u8bd5\u5728\u58f0\u660e\u53d8\u91cf<code>num<\/code>\u4e4b\u524d\u5c31\u4f7f\u7528\u5b83\uff0c\u6216\u8005\u4f7f\u7528\u4e86\u4e00\u4e2a\u5728\u5f53\u524d\u4f5c\u7528\u57df\u5916\u58f0\u660e\u7684\u53d8\u91cf\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u8bc6\u522b\u8be5\u53d8\u91cf\uff0c\u4ece\u800c\u62a5\u9519\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u5728\u4f7f\u7528\u53d8\u91cf<code>num<\/code>\u524d\uff0c\u786e\u4fdd\u5176\u5df2\u88ab\u58f0\u660e\u4e14\u5728\u5f53\u524d\u4f5c\u7528\u57df\u5185\u3002\u4f8b\u5982\uff0c<code>int num = scanner.nextInt();<\/code>\uff0c\u8fd9\u6837\u65e2\u58f0\u660e\u4e86<code>num<\/code>\u4e5f\u901a\u8fc7<code>nextInt()<\/code>\u4e3a\u5176\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><p>\u53d8\u91cf\u58f0\u660e\u4e0d\u4ec5\u4ec5\u5c40\u9650\u4e8e\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u4e5f\u9002\u7528\u4e8e\u5bf9\u8c61\u548c\u7ed3\u6784\u4f53\u3002\u786e\u4fdd\u5728\u9002\u5f53\u7684\u4f5c\u7528\u57df\u5185\u58f0\u660e\u5e76\u521d\u59cb\u5316\u53d8\u91cf\u662f\u907f\u514d\u7f16\u8bd1\u65f6\u9519\u8bef\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u65b9\u6cd5\u8c03\u7528\u7684\u6b63\u786e\u6027<\/h3>\n<\/p>\n<p><p>\u65b9\u6cd5\u8c03\u7528\u7684\u6b63\u786e\u6027\u5bf9\u4e8e\u7a0b\u5e8f\u7684\u6b63\u786e\u6267\u884c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p><strong>\u65b9\u6cd5\u8c03\u7528\u4e0d\u6b63\u786e<\/strong>\uff0c\u6bd4\u5982\u4f20\u5165\u4e86\u9519\u8bef\u7684\u53c2\u6570\u7c7b\u578b\u3001\u53c2\u6570\u6570\u91cf\u4e0d\u5bf9\uff0c\u6216\u8005\u662f\u8c03\u7528\u4e86\u4e0d\u5b58\u5728\u7684\u65b9\u6cd5\uff0c\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002\u786e\u4fdd\u5728\u8c03\u7528<code>nextInt<\/code>\u65f6\uff0c\u5176\u6240\u5728\u7684\u5bf9\u8c61\u5df2\u6b63\u786e\u58f0\u660e\uff08\u5982<code>Scanner<\/code>\u5bf9\u8c61\uff09\uff0c\u5e76\u4e14\u8c03\u7528\u65f6\u4e0d\u9700\u8981\u4efb\u4f55\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u786e\u4fdd\u6b63\u786e\u4f7f\u7528<code>Scanner<\/code>\u5bf9\u8c61\u7684<code>nextInt()<\/code>\u65b9\u6cd5\u6765\u8bfb\u53d6\u4e0b\u4e00\u4e2a\u6574\u6570\uff0c\u800c\u4e0d\u662f\u9519\u8bef\u5730\u4f20\u5165\u4e86\u4e0d\u5fc5\u8981\u7684\u53c2\u6570\u6216\u671f\u671b\u5b83\u6267\u884c\u4e0d\u5c5e\u4e8e\u5b83\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4e0a\u4e0b\u6587\u73af\u5883\u7684\u5339\u914d\u6027<\/h3>\n<\/p>\n<p><p>\u7a0b\u5e8f\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u9519\u8bef\uff0c\u50cf\u662f\u5728\u975e\u9759\u6001\u73af\u5883\u4e2d\u76f4\u63a5\u8c03\u7528\u9759\u6001\u65b9\u6cd5\u6216\u53d8\u91cf\uff0c\u6216\u662f\u53cd\u4e4b\uff0c\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e0a\u4e0b\u6587\u73af\u5883\u9519\u8bef<\/strong>\u901a\u5e38\u6d89\u53ca\u5230\u9759\u6001\uff08static\uff09\u4e0e\u975e\u9759\u6001\uff08instance\uff09\u6210\u5458\u7684\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u4ece\u9759\u6001\u65b9\u6cd5\u4e2d\u76f4\u63a5\u8c03\u7528\u975e\u9759\u6001\u53d8\u91cf\u6216\u65b9\u6cd5\u65e0\u6cd5\u901a\u8fc7\u7f16\u8bd1\u3002\u786e\u4fdd\u4f60\u7684\u65b9\u6cd5\u8c03\u7528\u53ca\u53d8\u91cf\u8bbf\u95ee\u4e0e\u5b83\u4eec\u6240\u5728\u7684\u4e0a\u4e0b\u6587\uff08\u9759\u6001\u6216\u975e\u9759\u6001\uff09\u5339\u914d\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c\u89e3\u51b3<code>nextInt<\/code>\u548c<code>num<\/code>\u62a5\u7ea2\u7684\u5173\u952e\u5728\u4e8e\u7406\u89e3Java\u7684\u57fa\u672c\u89c4\u5219\uff0c\u5305\u62ec\u5e93\u7684\u5bfc\u5165\u3001\u53d8\u91cf\u7684\u58f0\u660e\u4e0e\u4f5c\u7528\u57df\u3001\u65b9\u6cd5\u7684\u6b63\u786e\u8c03\u7528\u4ee5\u53ca\u4e0a\u4e0b\u6587\u73af\u5883\u7684\u9002\u7528\u6027\u3002\u53ea\u6709\u6df1\u5165\u7406\u89e3\u8fd9\u4e9b\u6982\u5ff5\uff0c\u624d\u80fd\u6709\u6548\u5730\u8bca\u65ad\u5e76\u89e3\u51b3\u7f16\u8bd1\u65f6\u51fa\u73b0\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4e3a\u4ec0\u4e48Eclipse\u663e\u793anextInt\u548cnum\u62a5\u9519\uff1f<\/strong><\/p>\n<p>\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u662f\u56e0\u4e3aEclipse\u8ba4\u4e3anextInt\u548cnum\u662f\u672a\u5b9a\u4e49\u7684\u53d8\u91cf\u6216\u65b9\u6cd5\u3002\u8fd9\u53ef\u80fd\u662f\u56e0\u4e3a\u4f60\u6ca1\u6709\u5728\u4ee3\u7801\u4e2d\u6b63\u786e\u58f0\u660e\u6216\u8d4b\u503c\u5176\u503c\u3002\u786e\u4fdd\u4f60\u5728\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u6216\u65b9\u6cd5\u4e4b\u524d\u5148\u58f0\u660e\u5e76\u8d4b\u4e88\u5b83\u4eec\u6b63\u786e\u7684\u503c\u3002<\/p>\n<p><strong>nextInt\u548cnum\u62a5\u7ea2\u7684\u5e38\u89c1\u539f\u56e0\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u4e00\u79cd\u5e38\u89c1\u7684\u539f\u56e0\u662f\uff0c\u4f60\u53ef\u80fd\u5fd8\u8bb0\u5bfc\u5165\u6240\u9700\u7684\u7c7b\u6216\u5305\u3002\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fJava\u6807\u51c6\u5e93\u4e2d\u7684\u65b9\u6cd5\u6216\u7c7b\uff0c\u4f8b\u5982nextInt()\u65b9\u6cd5\uff0c\u4f60\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165java.util.Scanner\u7c7b\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u53ef\u80fd\u7684\u539f\u56e0\u662f\u4f60\u6b63\u5728\u4f7f\u7528\u8fd9\u4e9b\u53d8\u91cf\u6216\u65b9\u6cd5\u7684\u4f5c\u7528\u57df\u4e0d\u6b63\u786e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u65b9\u6cd5\u5185\u90e8\u58f0\u660e\u4e86\u53d8\u91cfnum\uff0c\u5e76\u4e14\u5728\u6b64\u65b9\u6cd5\u4e4b\u5916\u7684\u5730\u65b9\u5f15\u7528\u4e86\u5b83\uff0c\u90a3\u4e48\u5728\u5f15\u7528\u5b83\u7684\u5730\u65b9\u5c31\u4f1a\u62a5\u9519\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3nextInt\u548cnum\u62a5\u7ea2\u95ee\u9898\uff1f<\/strong><\/p>\n<p>\u7b2c\u4e00\u6b65\u662f\u786e\u4fdd\u4f60\u5bfc\u5165\u4e86\u6240\u9700\u7684\u7c7b\u6216\u5305\u3002\u5982\u679c\u4f60\u5728\u4f7f\u7528nextInt()\u65b9\u6cd5\uff0c\u4f60\u9700\u8981\u5728\u4ee3\u7801\u7684\u5f00\u59cb\u6216\u7c7b\u7684\u5f00\u5934\u6dfb\u52a0import\u8bed\u53e5\uff1aimport java.util.Scanner;<\/p>\n<p>\u7b2c\u4e8c\u6b65\u662f\u68c0\u67e5\u53d8\u91cf\u6216\u65b9\u6cd5\u7684\u4f5c\u7528\u57df\u662f\u5426\u6b63\u786e\u3002\u5982\u679c\u4f60\u5728\u67d0\u4e2a\u65b9\u6cd5\u5185\u90e8\u58f0\u660e\u4e86\u53d8\u91cfnum\uff0c\u5e76\u4e14\u60f3\u5728\u5176\u4ed6\u65b9\u6cd5\u6216\u7c7b\u7684\u5730\u65b9\u4f7f\u7528\u5b83\uff0c\u4f60\u53ef\u4ee5\u5c06\u5176\u58f0\u660e\u4e3a\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u6216\u4f7f\u7528\u53c2\u6570\u4f20\u9012\u5b83\u3002<\/p>\n<p>\u6700\u540e\uff0c\u786e\u4fdd\u4f60\u6b63\u786e\u4f7f\u7528\u4e86\u8fd9\u4e9b\u53d8\u91cf\u6216\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u4ecd\u7136\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u7684\u6587\u6863\u6216\u5bfb\u6c42\u4e13\u4e1a\u4eba\u58eb\u7684\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Java\u7f16\u7a0b\u4e2d\uff0c\u5f53\u60a8\u9047\u5230nextInt\u548cnum\u8fd9\u4e24\u4e2a\u540d\u8bcd\u65f6\u62a5\u7ea2\uff0c\u5e38\u89c1\u539f\u56e0\u4e3b\u8981\u5305\u62ec\u672a\u5bfc\u5165\u76f8\u5e94\u7684\u5e93\u3001\u53d8\u91cf\u672a\u58f0\u660e\u524d [&hellip;]","protected":false},"author":3,"featured_media":294354,"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\/294350"}],"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=294350"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/294350\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/294354"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=294350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=294350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=294350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}