{"id":66441,"date":"2025-05-03T10:10:46","date_gmt":"2025-05-03T02:10:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66441\/"},"modified":"2025-05-03T10:10:46","modified_gmt":"2025-05-03T02:10:46","slug":"java%e6%80%8e%e4%b9%88%e5%88%9b%e5%bb%ba%e4%b8%80%e7%bb%b4%e9%9a%8f%e6%9c%ba%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66441\/","title":{"rendered":"java\u600e\u4e48\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\uff1a\u786e\u5b9a\u6570\u7ec4\u5927\u5c0f\u521b\u5efa\u6570\u7ec4\u4f7f\u7528 math.random() \u751f\u6210\u968f\u673a\u6570\u586b\u5145\u6570\u7ec4\u4f7f\u7528\u6570\u7ec4\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111603484655921.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4<\/strong><\/p>\n<p>\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\u662f\u6307\u6bcf\u4e2a\u5143\u7d20\u5305\u542b\u4e00\u4e2a\u968f\u673a\u503c\u7684\u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\uff1a<\/p>\n<ol>\n<li> <strong>\u786e\u5b9a\u6570\u7ec4\u5927\u5c0f<\/strong>\uff1a\u9996\u5148\uff0c\u9700\u8981\u786e\u5b9a\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\uff0c\u5373\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/li>\n<li>\n<p><strong>\u521b\u5efa\u6570\u7ec4<\/strong>\uff1a\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\u6570\u7ec4\uff1a<\/p>\n<pre>int[] arrayName = new int[size];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d arrayName \u662f\u6570\u7ec4\u540d\u79f0\uff0csize \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<\/li>\n<li> <strong>\u751f\u6210\u968f\u673a\u6570<\/strong>\uff1a\u4f7f\u7528 Math.random() \u65b9\u6cd5\u751f\u6210\u8303\u56f4\u5728 0 \u5230 1 \u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a double \u503c\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\uff0c\u4f8b\u5982 int\u3002<\/li>\n<li>\n<p><strong>\u586b\u5145\u6570\u7ec4<\/strong>\uff1a\u4f7f\u7528 Math.random() \u751f\u6210\u7684\u968f\u673a\u6570\u586b\u5145\u6570\u7ec4\uff1a<\/p>\n<pre>for (int i = 0; i &lt; arrayName.length; i++) {\n arrayName[i] = (int) (Math.random() * upperBound);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8be5\u5faa\u73af\u4e2d\uff0cupperBound \u662f\u8981\u751f\u6210\u7684\u968f\u673a\u6570\u7684\u4e0a\u9650\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6570\u7ec4<\/strong>\uff1a\u521b\u5efa\u6570\u7ec4\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 arrayName \u53d8\u91cf\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6253\u5370\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1a<\/p>\n<pre>for (int num : arrayName) {\n System.out.println(num);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<p>\u5047\u8bbe\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a 10 \u7684\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\uff0c\u5176\u4e2d\u5143\u7d20\u7684\u6700\u5927\u503c\u4e3a 100\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u8be5\u6570\u7ec4\uff1a<\/p>\n<pre>int[] randomArray = new int[10];\n\nfor (int i = 0; i &lt; randomArray.length; i++) {\n    randomArray[i] = (int) (Math.random() * 100);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0crandomArray \u662f\u4e00\u4e2a\u5305\u542b 10 \u4e2a\u4ecb\u4e8e 0 \u548c 100 \u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u7684\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\uff1a\u786e\u5b9a\u6570\u7ec4\u5927\u5c0f\u521b\u5efa\u6570\u7ec4\u4f7f\u7528 math.random() \u751f\u6210\u968f\u673a\u6570\u586b\u5145\u6570\u7ec4\u4f7f\u7528\u6570\u7ec4 \u5982\u4f55\u5728 Java \u4e2d\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4 \u4e00\u7ef4\u968f\u673a\u6570\u7ec4\u662f\u6307\u6bcf\u4e2a\u5143\u7d20\u5305\u542b\u4e00\u4e2a\u968f\u673a\u503c\u7684\u6570\u7ec4\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u521b\u5efa\u4e00\u7ef4\u968f\u673a\u6570\u7ec4\uff1a \u786e\u5b9a\u6570\u7ec4\u5927\u5c0f\uff1a\u9996\u5148\uff0c\u9700\u8981\u786e\u5b9a\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u6570\u91cf\uff0c\u5373\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u521b\u5efa\u6570\u7ec4\uff1a\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u521b\u5efa\u6570\u7ec4\uff1a int[] arrayName = new int[size]; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d arrayName \u662f\u6570\u7ec4\u540d\u79f0\uff0csize \u662f\u6570\u7ec4\u7684\u5927\u5c0f\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u751f\u6210\u968f\u673a\u6570\uff1a\u4f7f\u7528 Math.random() \u65b9\u6cd5\u751f\u6210\u8303\u56f4\u5728 0 \u5230 1 \u4e4b\u95f4\u7684\u968f\u673a\u6570\u3002\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a double \u503c\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u7c7b\u578b\uff0c\u4f8b\u5982 int\u3002 \u586b\u5145\u6570\u7ec4\uff1a\u4f7f\u7528 Math.random() \u751f\u6210\u7684\u968f\u673a\u6570\u586b\u5145\u6570\u7ec4\uff1a for (int i = 0; i &lt; arrayName.length; i++) { arrayName[i] = (int) (Math.random() * upperBound); } \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-66441","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=66441"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66441\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}