{"id":35580,"date":"2024-11-26T12:50:08","date_gmt":"2024-11-26T04:50:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35580\/"},"modified":"2024-11-26T12:50:08","modified_gmt":"2024-11-26T04:50:08","slug":"java%e6%80%8e%e4%b9%88%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84%e7%9a%84%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35580\/","title":{"rendered":"java\u600e\u4e48\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\u7684\u6570\u636e"},"content":{"rendered":"<blockquote><p>\n  \u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u952e\u76d8\u8f93\u5165\uff0c\u4f7f\u7528 scanner \u7c7b\u4ece\u952e\u76d8\u8bfb\u53d6\u8f93\u5165\u3002arrays.fill() \u65b9\u6cd5\uff0c\u5c06\u6570\u7ec4\u5143\u7d20\u8bbe\u4e3a\u6307\u5b9a\u503c\u3002\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4f7f\u7528 string[] args \u6570\u7ec4\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002\u6587\u4ef6\u8f93\u5165\uff0c\u4f7f\u7528 fileinputstream \u548c datainputstream \u7c7b\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110406243193054.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\u7684\u6570\u636e\u63d2\u56fe\" alt=\"java\u600e\u4e48\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\u7684\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u8f93\u5165\u6570\u7ec4\u6570\u636e<\/strong><\/p>\n<p>\u8f93\u5165\u6570\u7ec4\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u952e\u76d8\u8f93\u5165<\/strong><\/p>\n<p>\u4f7f\u7528 Scanner \u7c7b\u53ef\u4ee5\u4ece\u6807\u51c6\u8f93\u5165\uff08\u901a\u5e38\u662f\u952e\u76d8\uff09\u8bfb\u53d6\u8f93\u5165\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Scanner;\n\npublic class ArrayInput {\n\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n\n        int[] numbers = new int[5];\n\n        for (int i = 0; i &lt; numbers.length; i++) {\n            System.out.println(\"Enter number \" + (i + 1) + \": \");\n            numbers[i] = scanner.nextInt();\n        }\n\n        scanner.close();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.fill() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays.fill() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8bbe\u7f6e\u4e3a\u6307\u5b9a\u503c\u3002\u8be5\u503c\u53ef\u4ee5\u662f\u57fa\u672c\u7c7b\u578b\u6216\u5bf9\u8c61\u3002<\/p>\n<pre>import java.util.Arrays;\n\npublic class ArrayInput {\n\n    public static void main(String[] args) {\n        int[] numbers = new int[5];\n\n        Arrays.fill(numbers, 10);\n\n        System.out.println(Arrays.toString(numbers)); \/\/ \u8f93\u51fa [10, 10, 10, 10, 10]\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/p>\n<p>\u5982\u679c\u4ee5\u547d\u4ee4\u884c\u53c2\u6570\u7684\u5f62\u5f0f\u4f20\u9012\u6570\u7ec4\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528 String[] args \u6570\u7ec4\u6765\u83b7\u53d6\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<pre>public class ArrayInput {\n\n    public static void main(String[] args) {\n        int[] numbers = new int[args.length];\n\n        for (int i = 0; i &lt; numbers.length; i++) {\n            numbers[i] = Integer.parseInt(args[i]);\n        }\n\n        System.out.println(Arrays.toString(numbers));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6587\u4ef6\u8f93\u5165<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 FileInputStream \u548c DataInputStream \u7c7b\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u7ec4\u6570\u636e\u3002<\/p>\n<pre>import java.io.FileInputStream;\nimport java.io.DataInputStream;\n\npublic class ArrayInput {\n\n    public static void main(String[] args) {\n        try {\n            FileInputStream fis = new FileInputStream(\"data.bin\");\n            DataInputStream dis = new DataInputStream(fis);\n\n            int[] numbers = new int[dis.readInt()];\n\n            for (int i = 0; i &lt; numbers.length; i++) {\n                numbers[i] = dis.readInt();\n            }\n\n            dis.close();\n            fis.close();\n\n            System.out.println(Arrays.toString(numbers));\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\u7684\u6570\u636e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u952e\u76d8\u8f93\u5165\uff0c\u4f7f\u7528 scanner \u7c7b\u4ece\u952e\u76d8\u8bfb\u53d6\u8f93\u5165\u3002arrays.fill() \u65b9\u6cd5\uff0c\u5c06\u6570\u7ec4\u5143\u7d20\u8bbe\u4e3a\u6307\u5b9a\u503c\u3002\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u4f7f\u7528 string[] args \u6570\u7ec4\u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\u3002\u6587\u4ef6\u8f93\u5165\uff0c\u4f7f\u7528 fileinputstream \u548c datainputstream \u7c7b\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002 \u5982\u4f55\u4f7f\u7528 Java \u8f93\u5165\u6570\u7ec4\u6570\u636e \u8f93\u5165\u6570\u7ec4\u6570\u636e\u7684\u65b9\u6cd5\u6709\u591a\u79cd\u3002\u4ee5\u4e0b\u4ecb\u7ecd\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1a 1. \u4f7f\u7528\u952e\u76d8\u8f93\u5165 \u4f7f\u7528 Scanner \u7c7b\u53ef\u4ee5\u4ece\u6807\u51c6\u8f93\u5165\uff08\u901a\u5e38\u662f\u952e\u76d8\uff09\u8bfb\u53d6\u8f93\u5165\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import java.util.Scanner; public class ArrayInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] numbers = new int[5]; for (int i = 0; i &lt; numbers.length; i++) { System.out.println(&#8220;Enter number [&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-35580","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35580","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=35580"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35580\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}