{"id":64294,"date":"2025-05-03T12:05:07","date_gmt":"2025-05-03T04:05:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64294\/"},"modified":"2025-05-03T12:05:07","modified_gmt":"2025-05-03T04:05:07","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8a%e9%9a%8f%e6%9c%ba%e6%95%b0%e6%94%be%e5%85%a5%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64294\/","title":{"rendered":"java\u600e\u4e48\u628a\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u4f7f\u7528 random \u5bf9\u8c61\u53ef\u4ee5\u751f\u6210\u968f\u673a\u6570\u5e76\u5c06\u5176\u653e\u5165\u6570\u7ec4\u4e2d\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a 1. \u521b\u5efa random \u5bf9\u8c61\uff1b2. \u521b\u5efa\u6570\u7ec4\uff1b3. \u4f7f\u7528 random.nextint() \u751f\u6210\u968f\u673a\u6570\u5e76\u5c06\u5176\u5b58\u5165\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110303572474354.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628a\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628a\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Random \u7c7b\u6765\u751f\u6210\u968f\u673a\u6570\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\u4e2d\u3002<\/p>\n<p><strong>1. \u521b\u5efa\u4e00\u4e2a Random \u5bf9\u8c61<\/strong><\/p>\n<pre>Random random = new Random();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>int[] arr = new int[10];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 random.nextInt() \u65b9\u6cd5\u751f\u6210\u968f\u673a\u6574\u6570\u5e76\u5c06\u5176\u5b58\u5165\u6570\u7ec4\u3002nextInt() \u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u8981\u751f\u6210\u7684\u968f\u673a\u6570\u7684\u6700\u5927\u503c\u3002<\/p>\n<pre>for (int i = 0; i &lt; arr.length; i++) {\n  arr[i] = random.nextInt(100);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<pre>import java.util.Random;\n\npublic class RandomArray {\n\n  public static void main(String[] args) {\n    Random random = new Random();\n    int[] arr = new int[10];\n\n    for (int i = 0; i &lt; arr.length; i++) {\n      arr[i] = random.nextInt(100);\n    }\n\n    System.out.println(\"\u968f\u673a\u6570\u6570\u7ec4\uff1a\");\n    for (int num : arr) {\n      System.out.println(num);\n    }\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/p>\n<pre>\u968f\u673a\u6570\u6570\u7ec4\uff1a\n55\n22\n79\n13\n98\n35\n64\n53\n71\n20<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628a\u968f\u673a\u6570\u653e\u5165\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\u4f7f\u7528 random \u5bf9\u8c61\u53ef\u4ee5\u751f\u6210\u968f\u673a\u6570\u5e76\u5c06\u5176\u653e\u5165\u6570\u7ec4\u4e2d\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a 1. \u521b\u5efa random \u5bf9\u8c61\uff1b2. \u521b\u5efa\u6570\u7ec4\uff1b3. \u4f7f\u7528 random.nextint() \u751f\u6210\u968f\u673a\u6570\u5e76\u5c06\u5176\u5b58\u5165\u6570\u7ec4\u3002 Java \u4e2d\u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Random \u7c7b\u6765\u751f\u6210\u968f\u673a\u6570\uff0c\u7136\u540e\u5c06\u8fd9\u4e9b\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4\u4e2d\u3002 1. \u521b\u5efa\u4e00\u4e2a Random \u5bf9\u8c61 Random random = new Random(); \u767b\u5f55\u540e\u590d\u5236 2. \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = new int[10]; \u767b\u5f55\u540e\u590d\u5236 3. \u5c06\u968f\u673a\u6570\u653e\u5165\u6570\u7ec4 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 random.nextInt() \u65b9\u6cd5\u751f\u6210\u968f\u673a\u6574\u6570\u5e76\u5c06\u5176\u5b58\u5165\u6570\u7ec4\u3002nextInt() \u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u8868\u793a\u8981\u751f\u6210\u7684\u968f\u673a\u6570\u7684\u6700\u5927\u503c\u3002 for (int i = 0; i &lt; arr.length; i++) { [&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-64294","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64294","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=64294"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64294\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}