{"id":39840,"date":"2024-11-26T16:09:24","date_gmt":"2024-11-26T08:09:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39840\/"},"modified":"2024-11-26T16:09:24","modified_gmt":"2024-11-26T08:09:24","slug":"java%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e8%be%93%e5%85%a5%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39840\/","title":{"rendered":"java\u600e\u4e48\u5b9e\u73b0\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u8f38\u5165\u6578\u7d44\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 scanner \u985e\u8f38\u5165\u6574\u6578\u6578\u7d44\u3002\u4f7f\u7528 bufferedreader \u985e\u8f38\u5165\u5b57\u4e32\u6578\u7d44\u3002\u4f7f\u7528 datainputstream \u985e\u8f38\u5165\u57fa\u672c\u985e\u578b\u8cc7\u6599\u6578\u7d44\uff08\u4f8b\u5982\u6574\u6578\u6578\u7d44\uff09\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111711130082419.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5b9e\u73b0\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5b9e\u73b0\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 Scanner \u7c7b<\/strong><\/p>\n<p>Scanner \u7c7b\u63d0\u4f9b\u4e86\u4e00\u7ec4\u65b9\u6cd5\u7528\u4e8e\u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import java.util.Scanner;\n\npublic class ReadArray {\n\n    public static void main(String[] args) {\n        Scanner sc = new Scanner(System.in);\n        System.out.println(\"Enter the size of the array:\");\n        int size = sc.nextInt();\n\n        int[] arr = new int[size];\n        System.out.println(\"Enter the elements of the array:\");\n        for (int i = 0; i &lt; size; i++) {\n            arr[i] = sc.nextInt();\n        }\n\n        \/\/ Display the elements of the array\n        System.out.println(\"Elements of the array:\");\n        for (int i : arr) {\n            System.out.print(i + \" \");\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 BufferedReader \u7c7b<\/strong><\/p>\n<p>BufferedReader \u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2a readLine() \u65b9\u6cd5\uff0c\u7528\u4e8e\u9010\u884c\u8bfb\u53d6\u6587\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bfb\u53d6\u4e00\u4e2a\uff1a<\/p>\n<pre>import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\n\npublic class ReadArray {\n\n    public static void main(String[] args) throws IOException {\n        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));\n        System.out.println(\"Enter the size of the array:\");\n        int size = Integer.parseInt(reader.readLine());\n\n        String[] arr = new String[size];\n        System.out.println(\"Enter the elements of the array:\");\n        for (int i = 0; i &lt; size; i++) {\n            arr[i] = reader.readLine();\n        }\n\n        \/\/ Display the elements of the array\n        System.out.println(\"Elements of the array:\");\n        for (String s : arr) {\n            System.out.print(s + \" \");\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 DataInputStream \u7c7b<\/strong><\/p>\n<p>DataInputStream \u7c7b\u63d0\u4f9b\u4e86\u7528\u4e8e\u8bfb\u53d6\u57fa\u672c\u7c7b\u578b\u6570\u636e\u7684 readInt() \u548c readUTF() \u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8bfb\u53d6\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff1a<\/p>\n<pre>import java.io.DataInputStream;\nimport java.io.IOException;\n\npublic class ReadArray {\n\n    public static void main(String[] args) throws IOException {\n        DataInputStream dis = new DataInputStream(System.in);\n        System.out.println(\"Enter the size of the array:\");\n        int size = dis.readInt();\n\n        int[] arr = new int[size];\n        System.out.println(\"Enter the elements of the array:\");\n        for (int i = 0; i &lt; size; i++) {\n            arr[i] = dis.readInt();\n        }\n\n        \/\/ Display the elements of the array\n        System.out.println(\"Elements of the array:\");\n        for (int i : arr) {\n            System.out.print(i + \" \");\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5b9e\u73b0\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\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>\u5728 java \u4e2d\u8f38\u5165\u6578\u7d44\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 scanner \u985e\u8f38\u5165\u6574\u6578\u6578\u7d44\u3002\u4f7f\u7528 bufferedreader \u985e\u8f38\u5165\u5b57\u4e32\u6578\u7d44\u3002\u4f7f\u7528 datainputstream \u985e\u8f38\u5165\u57fa\u672c\u985e\u578b\u8cc7\u6599\u6578\u7d44\uff08\u4f8b\u5982\u6574\u6578\u6578\u7d44\uff09\u3002 \u5982\u4f55\u5728 Java \u4e2d\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8f93\u5165\u4e00\u4e2a\u6570\u7ec4\uff1a 1. \u4f7f\u7528 Scanner \u7c7b Scanner \u7c7b\u63d0\u4f9b\u4e86\u4e00\u7ec4\u65b9\u6cd5\u7528\u4e8e\u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u8f93\u5165\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8f93\u5165\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import java.util.Scanner; public class ReadArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(&#8220;Enter the size of the array:&#8221;); int size = sc.nextInt(); int[] arr = new int[size]; System.out.println(&#8220;Enter [&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-39840","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39840","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=39840"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39840\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}