{"id":2222,"date":"2025-03-02T10:42:32","date_gmt":"2025-03-02T02:42:32","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2222"},"modified":"2025-03-02T10:42:32","modified_gmt":"2025-03-02T02:42:32","slug":"java-scanner-%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/02\/java-scanner-%e7%b1%bb\/","title":{"rendered":"Java Scanner \u7c7b"},"content":{"rendered":"\n<p><code>Scanner<\/code> \u7c7b\u662f Java \u4e2d\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5e38\u7528\u5de5\u5177\u7c7b\uff0c\u5b83\u4f4d\u4e8e <code>java.util<\/code> \u5305\u4e2d\u3002<code>Scanner<\/code> \u53ef\u4ee5\u8bfb\u53d6\u5404\u79cd\u7c7b\u578b\u7684\u8f93\u5165\uff0c\u5305\u62ec\u4ece\u952e\u76d8\u3001\u6587\u4ef6\u3001\u5b57\u7b26\u4e32\u7b49\u6e90\u83b7\u53d6\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bfb\u53d6\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff08\u5982 <code>int<\/code>\u3001<code>double<\/code> \u7b49\uff09\u548c\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Scanner \u7c7b\u7684\u57fa\u672c\u4f7f\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1.1 \u521b\u5efa Scanner \u5bf9\u8c61<\/strong><\/h4>\n\n\n\n<p>\u8981\u4f7f\u7528 <code>Scanner<\/code>\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a <code>Scanner<\/code> \u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4e0e\u8f93\u5165\u6e90\uff08\u5982 <code>System.in<\/code>\uff0c\u5373\u952e\u76d8\u8f93\u5165\uff09\u5173\u8054\u8d77\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\npublic class ScannerExample {\n    public static void main(String&#91;] args) {\n        \/\/ \u521b\u5efa Scanner \u5bf9\u8c61\uff0c\u8bfb\u53d6\u952e\u76d8\u8f93\u5165\n        Scanner scanner = new Scanner(System.in);\n\n        System.out.print(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6574\u6570\uff1a\");\n        int number = scanner.nextInt();  \/\/ \u8bfb\u53d6\u6574\u6570\n        System.out.println(\"\u4f60\u8f93\u5165\u7684\u6574\u6570\u662f\uff1a\" + number);\n\n        System.out.print(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u6d6e\u70b9\u6570\uff1a\");\n        double floatingNumber = scanner.nextDouble();  \/\/ \u8bfb\u53d6\u6d6e\u70b9\u6570\n        System.out.println(\"\u4f60\u8f93\u5165\u7684\u6d6e\u70b9\u6570\u662f\uff1a\" + floatingNumber);\n\n        System.out.print(\"\u8bf7\u8f93\u5165\u4e00\u6bb5\u6587\u5b57\uff1a\");\n        String text = scanner.next();  \/\/ \u8bfb\u53d6\u5b57\u7b26\u4e32\uff08\u4ee5\u7a7a\u683c\u4e3a\u5206\u9694\u7b26\uff09\n        System.out.println(\"\u4f60\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u662f\uff1a\" + text);\n\n        \/\/ \u5173\u95ed scanner \u5bf9\u8c61\uff0c\u91ca\u653e\u8d44\u6e90\n        scanner.close();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Scanner \u5e38\u7528\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p><code>Scanner<\/code> \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u83b7\u53d6\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 nextInt()<\/strong><\/h4>\n\n\n\n<p>\u8bfb\u53d6\u4e0b\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int num = scanner.nextInt();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2 nextDouble()<\/strong><\/h4>\n\n\n\n<p>\u8bfb\u53d6\u4e0b\u4e00\u4e2a\u6d6e\u70b9\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>double num = scanner.nextDouble();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.3 nextLine()<\/strong><\/h4>\n\n\n\n<p>\u8bfb\u53d6\u4e00\u884c\u5b57\u7b26\u4e32\uff0c\u76f4\u5230\u9047\u5230\u6362\u884c\u7b26\u4e3a\u6b62\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String line = scanner.nextLine();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.4 next()<\/strong><\/h4>\n\n\n\n<p>\u8bfb\u53d6\u4e00\u4e2a\u5b8c\u6574\u7684\u5355\u8bcd\uff08\u4ee5\u7a7a\u683c\u6216\u6362\u884c\u7b26\u4e3a\u5206\u9694\u7b26\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String word = scanner.next();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.5 nextBoolean()<\/strong><\/h4>\n\n\n\n<p>\u8bfb\u53d6\u4e00\u4e2a\u5e03\u5c14\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>boolean flag = scanner.nextBoolean();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.6 hasNext() \u548c hasNextInt()<\/strong><\/h4>\n\n\n\n<p>\u68c0\u67e5\u662f\u5426\u8fd8\u6709\u4e0b\u4e00\u4e2a\u5143\u7d20\uff0c\u5e38\u7528\u4e8e\u5faa\u73af\u4e2d\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>hasNext()<\/code>\uff1a\u68c0\u67e5\u662f\u5426\u6709\u4e0b\u4e00\u4e2a\u5143\u7d20\uff08\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\uff09\u3002<\/li>\n\n\n\n<li><code>hasNextInt()<\/code>\uff1a\u68c0\u67e5\u4e0b\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u4e3a\u6574\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>if (scanner.hasNextInt()) {\n    int num = scanner.nextInt();\n    System.out.println(\"\u8f93\u5165\u7684\u662f\u6574\u6570\uff1a\" + num);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.7 close()<\/strong><\/h4>\n\n\n\n<p>\u5173\u95ed <code>Scanner<\/code> \u5bf9\u8c61\uff0c\u91ca\u653e\u8d44\u6e90\u3002\u6ce8\u610f\uff0c\u5173\u95ed <code>System.in<\/code> \u4f1a\u5173\u95ed\u6574\u4e2a\u8f93\u5165\u6d41\uff0c\u6240\u4ee5\u901a\u5e38\u4e0d\u63a8\u8350\u5173\u95ed <code>System.in<\/code>\uff0c\u9664\u975e\u5e94\u7528\u7ed3\u675f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>scanner.close();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u4f7f\u7528 Scanner \u8bfb\u53d6\u591a\u884c\u8f93\u5165<\/strong><\/h3>\n\n\n\n<p><code>Scanner<\/code> \u53ef\u4ee5\u8bfb\u53d6\u591a\u884c\u8f93\u5165\uff0c\u65b9\u6cd5\u662f\u4f7f\u7528 <code>nextLine()<\/code> \u6765\u8bfb\u53d6\u6bcf\u4e00\u884c\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u7528\u6237\u8f93\u5165\u7684\u591a\u884c\u6587\u672c\uff0c\u76f4\u5230\u8f93\u5165 &#8220;exit&#8221; \u4e3a\u6b62\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\npublic class MultiLineInput {\n    public static void main(String&#91;] args) {\n        Scanner scanner = new Scanner(System.in);\n        String line;\n\n        System.out.println(\"\u8bf7\u8f93\u5165\u591a\u884c\u6587\u672c\uff0c\u8f93\u5165 'exit' \u6765\u7ed3\u675f\uff1a\");\n\n        while (true) {\n            line = scanner.nextLine();\n            if (line.equalsIgnoreCase(\"exit\")) {\n                break;\n            }\n            System.out.println(\"\u4f60\u8f93\u5165\u7684\u884c\u662f\uff1a\" + line);\n        }\n\n        scanner.close();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528 Scanner \u8bfb\u53d6\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p><code>Scanner<\/code> \u4e5f\u53ef\u4ee5\u7528\u6765\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u6587\u4ef6\u5bf9\u8c61\u6216\u6587\u4ef6\u8def\u5f84\u6765\u521b\u5efa <code>Scanner<\/code> \u5bf9\u8c61\uff0c\u7136\u540e\u8bfb\u53d6\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\nimport java.io.File;\nimport java.io.IOException;\n\npublic class FileScannerExample {\n    public static void main(String&#91;] args) throws IOException {\n        \/\/ \u521b\u5efa File \u5bf9\u8c61\n        File file = new File(\"example.txt\");\n\n        \/\/ \u521b\u5efa Scanner \u5bf9\u8c61\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\n        Scanner scanner = new Scanner(file);\n\n        while (scanner.hasNextLine()) {\n            String line = scanner.nextLine();\n            System.out.println(line);\n        }\n\n        \/\/ \u5173\u95ed scanner \u5bf9\u8c61\n        scanner.close();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u793a\u4f8b\uff1a\u7efc\u5408\u4f7f\u7528 Scanner<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\npublic class ScannerComprehensiveExample {\n    public static void main(String&#91;] args) {\n        Scanner scanner = new Scanner(System.in);\n\n        \/\/ \u8f93\u5165\u59d3\u540d\n        System.out.print(\"\u8bf7\u8f93\u5165\u59d3\u540d\uff1a\");\n        String name = scanner.nextLine();\n\n        \/\/ \u8f93\u5165\u5e74\u9f84\n        System.out.print(\"\u8bf7\u8f93\u5165\u5e74\u9f84\uff1a\");\n        int age = scanner.nextInt();\n\n        \/\/ \u8f93\u5165\u85aa\u6c34\n        System.out.print(\"\u8bf7\u8f93\u5165\u85aa\u6c34\uff1a\");\n        double salary = scanner.nextDouble();\n\n        \/\/ \u8f93\u51fa\u8f93\u5165\u7684\u4fe1\u606f\n        System.out.println(\"\u59d3\u540d\uff1a\" + name);\n        System.out.println(\"\u5e74\u9f84\uff1a\" + age);\n        System.out.println(\"\u85aa\u6c34\uff1a\" + salary);\n\n        scanner.close();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Scanner \u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>next()<\/code> \u53ea\u8bfb\u53d6\u4e00\u4e2a\u5355\u8bcd\uff0c\u9047\u5230\u7a7a\u683c\u3001\u6362\u884c\u7b26\u7b49\u5206\u9694\u7b26\u65f6\u4f1a\u505c\u6b62\u3002<\/li>\n\n\n\n<li><code>nextLine()<\/code> \u8bfb\u53d6\u6574\u4e2a\u4e00\u884c\uff0c\u76f4\u5230\u9047\u5230\u6362\u884c\u7b26\u4e3a\u6b62\u3002<\/li>\n\n\n\n<li>\u5728\u8bfb\u53d6 <code>nextInt()<\/code> \u6216 <code>nextDouble()<\/code> \u540e\uff0c\u901a\u5e38\u4f1a\u5269\u4e0b\u4e00\u4e2a\u6362\u884c\u7b26\uff0c\u5982\u679c\u4f60\u540e\u7eed\u4f7f\u7528 <code>nextLine()<\/code>\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8bfb\u53d6\u4e0d\u5230\u9884\u671f\u6570\u636e\u7684\u95ee\u9898\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u8c03\u7528\u4e00\u6b21 <code>nextLine()<\/code> \u6765\u6d88\u8017\u6389\u6362\u884c\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Scanner scanner = new Scanner(System.in);\n\nSystem.out.print(\"\u8bf7\u8f93\u5165\u6574\u6570\uff1a\");\nint number = scanner.nextInt();\n\n\/\/ \u89e3\u51b3\u6362\u884c\u7b26\u95ee\u9898\nscanner.nextLine(); \/\/ \u6d88\u8017\u6389\u4e4b\u524d\u8f93\u5165\u7684\u6362\u884c\u7b26\n\nSystem.out.print(\"\u8bf7\u8f93\u5165\u4e00\u884c\u6587\u672c\uff1a\");\nString text = scanner.nextLine();\nSystem.out.println(\"\u4f60\u8f93\u5165\u7684\u6587\u672c\u662f\uff1a\" + text);\n\nscanner.close();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Scanner<\/code> \u7c7b\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u8f93\u5165\u5de5\u5177\uff0c\u53ef\u4ee5\u8bfb\u53d6\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u548c\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li>\u5b83\u53ef\u4ee5\u4ece\u6807\u51c6\u8f93\u5165\uff08\u952e\u76d8\uff09\u3001\u6587\u4ef6\u6216\u5176\u4ed6\u8f93\u5165\u6e90\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>Scanner<\/code> \u65f6\u9700\u8981\u8bb0\u5f97\u5173\u95ed\u5b83\u4ee5\u91ca\u653e\u8d44\u6e90\uff0c\u4f46\u5728\u8bfb\u53d6 <code>System.in<\/code> \u65f6\u901a\u5e38\u4e0d\u5efa\u8bae\u5173\u95ed\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u7528\u6237\u8f93\u5165\u3001\u6587\u4ef6\u8bfb\u53d6\u7b49\u529f\u80fd\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scanner \u7c7b\u662f Java \u4e2d\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5e38\u7528\u5de5\u5177\u7c7b\uff0c\u5b83\u4f4d\u4e8e java.util \u5305\u4e2d\u3002Scanne [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68],"tags":[],"class_list":["post-2222","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2222"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2222\/revisions"}],"predecessor-version":[{"id":2223,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2222\/revisions\/2223"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}