{"id":64302,"date":"2025-05-03T13:44:57","date_gmt":"2025-05-03T05:44:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/64302\/"},"modified":"2025-05-03T13:44:57","modified_gmt":"2025-05-03T05:44:57","slug":"java%e8%be%93%e5%85%a5%e4%b8%80%e7%bb%84%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%86%99-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/64302\/","title":{"rendered":"java\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\u6709\u4e09\u79cd\u65b9\u5f0f\uff1a\u4ece\u952e\u76d8\u8f93\u5165\uff1a\u4f7f\u7528 scanner \u7c7b\u4ece\u952e\u76d8\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u503c\u3002\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u8f93\u5165\uff1a\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362\u4e3a int \u7c7b\u578b\u5e76\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002\u4ece\u6587\u672c\u6587\u4ef6\u8f93\u5165\uff1a\u4ece\u6587\u672c\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5143\u7d20\u503c\u5e76\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/03\/2024110304154871516.jpg\" class=\"aligncenter\" title=\"java\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"java\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5982\u4f55\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u952e\u76d8\u8f93\u5165<\/strong><\/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        \/\/ \u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5e76\u6307\u5b9a\u5143\u7d20\u4e2a\u6570\n        int[] numbers = new int[5];\n\n        \/\/ \u4f7f\u7528\u5faa\u73af\u4ece\u952e\u76d8\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u503c\n        for (int i = 0; i &lt; numbers.length; i++) {\n            System.out.print(\"\u8bf7\u8f93\u5165\u7b2c \" + (i + 1) + \" \u4e2a\u5143\u7d20\uff1a\");\n            numbers[i] = scanner.nextInt();\n        }\n\n        \/\/ \u5173\u95ed Scanner \u5bf9\u8c61\n        scanner.close();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u547d\u4ee4\u884c\u53c2\u6570<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>public class ArrayInput {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a\u547d\u4ee4\u884c\u53c2\u6570\u7684\u4e2a\u6570\n        int[] numbers = new int[args.length];\n\n        \/\/ \u5c06\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362\u4e3a int \u7c7b\u578b\u5e76\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\n        for (int i = 0; i &lt; args.length; i++) {\n            numbers[i] = Integer.parseInt(args[i]);\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u65f6\uff0c\u9700\u8981\u5728\u8fd0\u884c\u7a0b\u5e8f\u65f6\u6307\u5b9a\u8981\u8f93\u5165\u7684\u6570\u5b57\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>java ArrayInput 1 2 3 4 5<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u6587\u672c\u6587\u4ef6<\/strong><\/p>\n<pre>import java.io.File;\nimport java.io.FileNotFoundException;\nimport java.util.Scanner;\n\npublic class ArrayInput {\n\n    public static void main(String[] args) {\n        try {\n            \/\/ \u521b\u5efa\u4e00\u4e2a File \u5bf9\u8c61\uff0c\u6307\u5411\u5305\u542b\u6570\u7ec4\u5143\u7d20\u7684\u6587\u672c\u6587\u4ef6\n            File file = new File(\"numbers.txt\");\n\n            \/\/ \u521b\u5efa\u4e00\u4e2a Scanner \u5bf9\u8c61\uff0c\u7528\u4e8e\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\n            Scanner scanner = new Scanner(file);\n\n            \/\/ \u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5e76\u6307\u5b9a\u5143\u7d20\u4e2a\u6570\n            int[] numbers = new int[scanner.nextInt()];\n\n            \/\/ \u5c06\u6587\u4ef6\u4e2d\u7684\u5143\u7d20\u503c\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\n            for (int i = 0; i &lt; numbers.length; i++) {\n                numbers[i] = scanner.nextInt();\n            }\n\n            \/\/ \u5173\u95ed Scanner \u5bf9\u8c61\n            scanner.close();\n        } catch (FileNotFoundException e) {\n            e.printStackTrace();\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\u600e\u4e48\u5199\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\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\u6709\u4e09\u79cd\u65b9\u5f0f\uff1a\u4ece\u952e\u76d8\u8f93\u5165\uff1a\u4f7f\u7528 scanner \u7c7b\u4ece\u952e\u76d8\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u503c\u3002\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u8f93\u5165\uff1a\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362\u4e3a int \u7c7b\u578b\u5e76\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002\u4ece\u6587\u672c\u6587\u4ef6\u8f93\u5165\uff1a\u4ece\u6587\u672c\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5143\u7d20\u503c\u5e76\u5b58\u50a8\u5728\u6570\u7ec4\u4e2d\u3002 Java \u4e2d\u5982\u4f55\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8f93\u5165\u4e00\u7ec4\u6570\u7ec4\uff1a 1. \u952e\u76d8\u8f93\u5165 import java.util.Scanner; public class ArrayInput { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); \/\/ \u521b\u5efa\u4e00\u4e2a int \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u5e76\u6307\u5b9a\u5143\u7d20\u4e2a\u6570 int[] numbers = new int[5]; \/\/ \u4f7f\u7528\u5faa\u73af\u4ece\u952e\u76d8\u83b7\u53d6\u6bcf\u4e2a\u5143\u7d20\u503c for (int i = 0; i &lt; numbers.length; i++) { System.out.print(&#8220;\u8bf7\u8f93\u5165\u7b2c &#8221; + [&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-64302","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64302","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=64302"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/64302\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=64302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=64302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=64302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}