{"id":37108,"date":"2024-11-26T14:55:28","date_gmt":"2024-11-26T06:55:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37108\/"},"modified":"2024-11-26T14:55:28","modified_gmt":"2024-11-26T06:55:28","slug":"java%e6%80%8e%e4%b9%88%e8%a6%81%e6%b1%82%e7%94%a8%e6%88%b7%e8%be%93%e5%85%a5%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37108\/","title":{"rendered":"java\u600e\u4e48\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u5165\uff0c\u901a\u8fc7 scanner \u7c7b\u63d0\u793a\u7528\u6237\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\uff0c\u518d\u9010\u4e2a\u83b7\u53d6\u5143\u7d20\u3002\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762 (gui)\uff0c\u521b\u5efa\u4e00\u4e2a swing \u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u542b\u6587\u672c\u6846\u3001\u6309\u94ae\u548c\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u7528\u4e8e\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\u548c\u9010\u4e2a\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111301184945437.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u5165<\/strong><\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528 Scanner \u7c7b\u521b\u5efa\u8f93\u5165\u5bf9\u8c61\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>Scanner input = new Scanner(System.in);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u63d0\u793a\u7528\u6237\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\uff1a<\/p>\n<pre>System.out.print(\"\u8bf7\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\uff1a\");\nint length = input.nextInt();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u521b\u5efa\u6570\u7ec4\u5e76\u9010\u4e2a\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5143\u7d20\uff1a<\/p>\n<pre>int[] array = new int[length];\nfor (int i = 0; i &lt; length; i++) {\n    System.out.printf(\"\u8bf7\u8f93\u5165\u6570\u7ec4\u7b2c %d \u4e2a\u5143\u7d20\uff1a\", i + 1);\n    array[i] = input.nextInt();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762 (GUI)<\/strong><\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a Swing \u5e94\u7528\u7a0b\u5e8f\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>import javax.swing.*;\n\npublic class ArrayInputGUI {\n\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u6587\u672c\u6846\u6765\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\n        JTextField lengthTextField = new JTextField(5);\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u9762\u677f\u6765\u5b58\u50a8\u6587\u672c\u6846\u548c\u6309\u94ae\n        JPanel panel = new JPanel();\n        panel.add(new JLabel(\"\u8bf7\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\uff1a\"));\n        panel.add(lengthTextField);\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u6309\u94ae\u6765\u89e6\u53d1\u6570\u7ec4\u8f93\u5165\n        JButton inputButton = new JButton(\"\u8f93\u5165\u6570\u7ec4\");\n\n        \/\/ \u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u5904\u7406\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\n        inputButton.addActionListener(e -&gt; {\n            int length = Integer.parseInt(lengthTextField.getText());\n\n            \/\/ \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u5e76\u9010\u4e2a\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5143\u7d20\n            int[] array = new int[length];\n            for (int i = 0; i &lt; length; i++) {\n                String element = JOptionPane.showInputDialog(\"\u8bf7\u8f93\u5165\u6570\u7ec4\u7b2c \" + (i + 1) + \" \u4e2a\u5143\u7d20\uff1a\");\n                array[i] = Integer.parseInt(element);\n            }\n\n            \/\/ \u663e\u793a\u7528\u6237\u8f93\u5165\u7684\u6570\u7ec4\n            JOptionPane.showMessageDialog(null, \"\u8f93\u5165\u7684\u6570\u7ec4\uff1a\" + Arrays.toString(array));\n        });\n\n        \/\/ \u5c06\u9762\u677f\u6dfb\u52a0\u5230\u7a97\u53e3\u5e76\u663e\u793a\u7a97\u53e3\n        JFrame frame = new JFrame(\"\u6570\u7ec4\u8f93\u5165 GUI\");\n        frame.add(panel);\n        frame.pack();\n        frame.setVisible(true);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u8981\u6c42\u7528\u6237\u8f93\u5165\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\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u5165\uff0c\u901a\u8fc7 scanner \u7c7b\u63d0\u793a\u7528\u6237\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\uff0c\u518d\u9010\u4e2a\u83b7\u53d6\u5143\u7d20\u3002\u4f7f\u7528\u56fe\u5f62\u7528\u6237\u754c\u9762 (gui)\uff0c\u521b\u5efa\u4e00\u4e2a swing \u5e94\u7528\u7a0b\u5e8f\uff0c\u5305\u542b\u6587\u672c\u6846\u3001\u6309\u94ae\u548c\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u7528\u4e8e\u83b7\u53d6\u6570\u7ec4\u957f\u5ea6\u548c\u9010\u4e2a\u5143\u7d20\u3002 \u5982\u4f55\u4f7f\u7528 Java \u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u8981\u6c42\u7528\u6237\u8f93\u5165\u6570\u7ec4\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a \u65b9\u6cd5 1\uff1a\u4f7f\u7528\u63a7\u5236\u53f0\u8f93\u5165 \u4f7f\u7528 Scanner \u7c7b\u521b\u5efa\u8f93\u5165\u5bf9\u8c61\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b Scanner input = new Scanner(System.in); \u767b\u5f55\u540e\u590d\u5236 \u63d0\u793a\u7528\u6237\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\uff1a System.out.print(&#8220;\u8bf7\u8f93\u5165\u6570\u7ec4\u957f\u5ea6\uff1a&#8221;); int length = input.nextInt(); \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u6570\u7ec4\u5e76\u9010\u4e2a\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5143\u7d20\uff1a int[] array = new int[length]; for (int i = 0; i &lt; length; i++) { System.out.printf(&#8220;\u8bf7\u8f93\u5165\u6570\u7ec4\u7b2c %d \u4e2a\u5143\u7d20\uff1a&#8221;, i + 1); [&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-37108","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37108","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=37108"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37108\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}