{"id":66591,"date":"2025-05-03T13:11:54","date_gmt":"2025-05-03T05:11:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66591\/"},"modified":"2025-05-03T13:11:54","modified_gmt":"2025-05-03T05:11:54","slug":"java%e9%9a%8f%e6%9c%ba%e6%95%b0%e6%80%8e%e4%b9%88%e6%94%be%e6%95%b0%e7%bb%84%e9%87%8c-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66591\/","title":{"rendered":"java\u968f\u673a\u6570\u600e\u4e48\u653e\u6570\u7ec4\u91cc"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\uff1a\u5b9a\u4e49\u5e76\u58f0\u660e\u6570\u7ec4\uff08int[] nums = new int[10];\uff09\u4f7f\u7528 math.random() \u751f\u6210\u968f\u673a\u6570\uff08double randomnumber = math.random();\uff09\u5c06\u968f\u673a\u6570\u4e58\u4ee5\u6570\u7ec4\u5927\u5c0f\uff08int index = (int) (randomnumber * nums.length);\uff09\u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\uff08nums[index] = randomnumber;\uff09\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111607064748614.jpg\" class=\"aligncenter\" title=\"java\u968f\u673a\u6570\u600e\u4e48\u653e\u6570\u7ec4\u91cc\u63d2\u56fe\" alt=\"java\u968f\u673a\u6570\u600e\u4e48\u653e\u6570\u7ec4\u91cc\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528 Java \u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u751f\u6210\u968f\u673a\u6570\u5e76\u5c06\u5176\u653e\u5165\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u4f7f\u7528 Math.random() \u51fd\u6570\u548c new \u5173\u952e\u5b57\u3002<\/p>\n<p><strong>\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u5b9a\u4e49\u6570\u7ec4\uff1a<\/strong>\u58f0\u660e\u4e00\u4e2a int[] \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5e76\u6307\u5b9a\u5176\u5927\u5c0f\u3002\u4f8b\u5982\uff0cint[] nums = new int[10]; \u521b\u5efa\u4e00\u4e2a\u5305\u542b 10 \u4e2a\u6574\u6570\u7684\u6570\u7ec4\u3002<\/li>\n<li> <strong>\u751f\u6210\u968f\u673a\u6570\uff1a<\/strong>\u4f7f\u7528 Math.random() \u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ecb\u4e8e 0\uff08\u5305\u62ec\uff09\u548c 1\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684 double \u503c\u3002\u4f8b\u5982\uff0cdouble randomNumber = Math.random();<\/li>\n<li> <strong>\u5c06\u5176\u4e58\u4ee5\u6570\u7ec4\u5927\u5c0f\uff1a<\/strong>\u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u4e58\u4ee5\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u5f97\u5230\u4e00\u4e2a\u4ecb\u4e8e 0 \u548c\u6570\u7ec4\u5927\u5c0f\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684\u6574\u6570\u3002\u4f8b\u5982\uff0cint index = (int) (randomNumber * nums.length);<\/li>\n<li> <strong>\u5c06\u5176\u653e\u5165\u6570\u7ec4\uff1a<\/strong>\u4f7f\u7528 index \u4f5c\u4e3a\u6570\u7ec4\u7d22\u5f15\uff0c\u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\u4e2d\u3002\u4f8b\u5982\uff0cnums[index] = randomNumber;<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Arrays;\n\npublic class Main {\n    public static void main(String[] args) {\n        int[] nums = new int[10];\n\n        for (int i = 0; i &lt; nums.length; i++) {\n            double randomNumber = Math.random();\n            int index = (int) (randomNumber * nums.length);\n            nums[index] = randomNumber;\n        }\n\n        System.out.println(Arrays.toString(nums)); \/\/ \u8f93\u51fa\u6570\u7ec4\u5185\u5bb9\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>[0.33694626573866503, 0.5957938348995876, 0.9758093745811612, 0.4341388254757247, 0.7673366526232808, 0.6546455263126862, 0.6623339364373651, 0.17407251220325233, 0.8337094380423272, 0.5734828783983138]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728 Java \u4e2d\u5c06\u968f\u673a\u6570\u8f7b\u677e\u5730\u653e\u5165\u6570\u7ec4\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u968f\u673a\u6570\u600e\u4e48\u653e\u6570\u7ec4\u91cc\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\u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\uff1a\u5b9a\u4e49\u5e76\u58f0\u660e\u6570\u7ec4\uff08int[] nums = new int[10];\uff09\u4f7f\u7528 math.random() \u751f\u6210\u968f\u673a\u6570\uff08double randomnumber = math.random();\uff09\u5c06\u968f\u673a\u6570\u4e58\u4ee5\u6570\u7ec4\u5927\u5c0f\uff08int index = (int) (randomnumber * nums.length);\uff09\u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\uff08nums[index] = randomnumber;\uff09 \u5229\u7528 Java \u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u751f\u6210\u968f\u673a\u6570\u5e76\u5c06\u5176\u653e\u5165\u6570\u7ec4\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u4f7f\u7528 Math.random() \u51fd\u6570\u548c new \u5173\u952e\u5b57\u3002 \u65b9\u6cd5\uff1a \u5b9a\u4e49\u6570\u7ec4\uff1a\u58f0\u660e\u4e00\u4e2a int[] \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5e76\u6307\u5b9a\u5176\u5927\u5c0f\u3002\u4f8b\u5982\uff0cint[] nums = new int[10]; \u521b\u5efa\u4e00\u4e2a\u5305\u542b 10 \u4e2a\u6574\u6570\u7684\u6570\u7ec4\u3002 \u751f\u6210\u968f\u673a\u6570\uff1a\u4f7f\u7528 Math.random() \u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ecb\u4e8e 0\uff08\u5305\u62ec\uff09\u548c 1\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684 double \u503c\u3002\u4f8b\u5982\uff0cdouble randomNumber = Math.random(); \u5c06\u5176\u4e58\u4ee5\u6570\u7ec4\u5927\u5c0f\uff1a\u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u4e58\u4ee5\u6570\u7ec4\u7684\u5927\u5c0f\uff0c\u5f97\u5230\u4e00\u4e2a\u4ecb\u4e8e 0 \u548c\u6570\u7ec4\u5927\u5c0f\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684\u6574\u6570\u3002\u4f8b\u5982\uff0cint index [&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-66591","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66591","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=66591"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66591\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}