{"id":673,"date":"2022-10-04T12:20:37","date_gmt":"2022-10-04T04:20:37","guid":{"rendered":"https:\/\/qaqaq.top\/?p=673"},"modified":"2022-12-19T19:31:11","modified_gmt":"2022-12-19T11:31:11","slug":"%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e4%b8%ad-%e9%a1%b9%e7%9b%ae%e4%ba%8c%e8%bd%af%e4%bb%b6%e7%bb%93%e6%9e%84%e8%ae%be%e8%ae%a1%e3%80%81%e9%a1%b9%e7%9b%ae%e4%ba%8ccmutility%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=673","title":{"rendered":"\u9762\u5411\u5bf9\u8c61(\u4e2d)-\u9879\u76ee\u4e8c\u5ba2\u6237\u4fe1\u606f\u7ba1\u7406\u8f6f\u4ef6"},"content":{"rendered":"\n<p>\u9762\u5411\u5bf9\u8c61(\u4e2d)-\u9879\u76ee\u4e8c\u8f6f\u4ef6\u7ed3\u6784\u8bbe\u8ba1\u3001\u9879\u76ee\u4e8cCMUtility\u5de5\u5177\u7c7b\u7684\u529f\u80fd\u4ecb\u7ecd\u3001\u9879\u76ee\u4e8c\uff1aCustomer\u7c7b\u7684\u8bbe\u8ba1\u3001CustomerList\u7c7b\u7684\u8bbe\u8ba1\u3001CustomerView\u7684\u6574\u4f53\u6846\u67b6\u7684\u642d\u5efa\u3001CustomerView\u9000\u51fa\u529f\u80fd\u7684\u5b9e\u73b0\u3001CustomerView\u5ba2\u6237\u5217\u8868\u529f\u80fd\u7684\u5b9e\u73b0\u3001CustomerView\u6dfb\u52a0\u5ba2\u6237\u529f\u80fd\u7684\u5b9e\u73b0\u3001CustomerView\u4fee\u6539\u5ba2\u6237\u529f\u80fd\u7684\u5b9e\u73b0\u3001CustomerView\u5220\u9664\u5ba2\u6237\u529f\u80fd\u7684\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P249.bean;\n\n\/**\n * \n * @Description Customer\u4e3a\u5b9e\u4f53\u7c7b\uff0c\u7528\u6765\u5c01\u88c5\u5ba2\u6237\u4fe1\u606f\n * @author RichieZhang Email:18358892@qq.com\n * @version\n * @date 2022\u5e7410\u67083\u65e5\u4e0b\u53483:37:47\n *\n *\/\n\npublic class Customer {\n\n\tprivate String name; \/\/ \u5ba2\u6237\u59d3\u540d\n\tprivate char gender; \/\/ \u6027\u522b\n\tprivate int age; \/\/ \u5e74\u9f84\n\tprivate String phone; \/\/ \u7535\u8bdd\u53f7\u7801\n\tprivate String email; \/\/ \u7535\u5b50\u90ae\u7bb1\n\n\tpublic Customer() {\n\t}\n\n\tpublic Customer(String name, char gender, int age, String phone, String email) {\n\t\tthis.name = name;\n\t\tthis.gender = gender;\n\t\tthis.age = age;\n\t\tthis.phone = phone;\n\t\tthis.email = email;\n\t}\n\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\n\tpublic void setName(String name) {\n\t\tthis.name = name;\n\t}\n\n\tpublic char getGender() {\n\t\treturn gender;\n\t}\n\n\tpublic void setGender(char gender) {\n\t\tthis.gender = gender;\n\t}\n\n\tpublic int getAge() {\n\t\treturn age;\n\t}\n\n\tpublic void setAge(int age) {\n\t\tthis.age = age;\n\t}\n\n\tpublic String getPhone() {\n\t\treturn phone;\n\t}\n\n\tpublic void setPhone(String phone) {\n\t\tthis.phone = phone;\n\t}\n\n\tpublic String getEmail() {\n\t\treturn email;\n\t}\n\n\tpublic void setEmail(String email) {\n\t\tthis.email = email;\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P249.seriver;\n\nimport top.qaqaq.java.P249.bean.Customer;\n\n\/**\n * \n* @Description\tCustomerList\u4e3aCustomer\u5bf9\u8c61\u7684\u7ba1\u7406\u6a21\u5757\uff0c\n* \u5185\u90e8\u7528\u6570\u7ec4\u7ba1\u7406\u4e00\u7ec4Customer\u5bf9\u8c61\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u6dfb\u52a0\u3001\u4fee\u6539\u3001\u5220\u9664\u548c\u904d\u5386\u65b9\u6cd5\uff0c\n* \u4f9bCustomerView\u8c03\u7528\n* @author RichieZhang Email:18358892@qq.com\n* @version\n* @date 2022\u5e7410\u67083\u65e5\u4e0b\u53483:41:45\n*\n *\/\n\npublic class CustomerList {\n\t\n\tprivate Customer&#91;] customers; \/\/ \u7528\u6765\u4fdd\u5b58\u5ba2\u6237\u5bf9\u8c61\u7684\u6570\u7ec4\n\tprivate int total; \/\/ \u8bb0\u5f55\u5df2\u4fdd\u5b58\u5ba2\u6237\u5bf9\u8c61\u7684\u6570\u91cf\n\t\n\t\/**\n\t * \u7528\u6765\u521d\u59cb\u5316customers\u6570\u7ec4\u7684\u6784\u9020\u5668\n\t * @param totalCustomer\t\u6307\u5b9a\u6570\u7ec4\u7684\u957f\u5ea6\n\t *\/\n\tpublic CustomerList(int totalCustomer) {\n\t\tcustomers = new Customer&#91;totalCustomer];\n\t}\n\t\n\t\/**\n\t * \n\t* @Description \u5c06\u6307\u5b9a\u7684\u5ba2\u6237\u6dfb\u52a0\u5230\u6570\u7ec4\u4e2d\n\t* @author RichieZhang\n\t* @date 2022\u5e7410\u67083\u65e5\u4e0b\u53484:42:45\n\t* @param customer\n\t* @return\ttrue:\u6dfb\u52a0\u6210\u529f\tfalst:\u6dfb\u52a0\u5931\u8d25\n\t *\/\n\tpublic boolean addCustomer(Customer customer) {\n\t\t\n\t\tif(total &gt;= customers.length) {\n\t\t\treturn false;\n\t\t}\n\t\t\n\/\/\t\tcustomers&#91;total] = customer;\n\/\/\t\ttotal++;\n\t\t\/\/\u6216\n\t\tcustomers&#91;total++] = customer;\n\t\treturn true;\n\t}\n\t\n\t\/**\n\t * \n\t* @Description \u4fee\u6539\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u7684\u5ba2\u6237\u4fe1\u606f\n\t* @author RichieZhang\n\t* @date 2022\u5e7410\u67083\u65e5\u4e0b\u53484:49:29\n\t* @param index\n\t* @param cust\n\t* @return\ttrue:\u4fee\u6539\u6210\u529f\tfalse:\u4fee\u6539\u5931\u8d25\n\t *\/\n\tpublic boolean replaceCustomer(int index, Customer cust) {\n\t\t\n\t\tif(index &lt; 0 || index &gt;= total) {\n\t\t\treturn false;\n\t\t}\n\t\t\n\t\tcustomers&#91;index] = cust;\n\t\treturn true;\n\t}\n\t\n\t\/**\n\t * \u5220\u9664\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u4e0a\u7684\u5ba2\u6237\n\t* @Description \n\t* @author RichieZhang\n\t* @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:11:44\n\t* @param index\n\t* @return\ttrue:\u5220\u9664\u6210\u529f\tfalse:\u5220\u9664\u5931\u8d25\n\t *\/\n\tpublic boolean deleteCustomer(int index) {\n\t\t\n\t\tif(index &lt; 0 || index &gt;= total) {\n\t\t\treturn false;\n\t\t}\n\t\t\n\t\tfor(int i = index; i &lt; total - 1; i++) {\n\t\t\tcustomers&#91;i] = customers&#91;i + 1];\n\t\t}\n\t\t\n\t\t\/\/\u6700\u540e\u6709\u6570\u636e\u7684\u5143\u7d20\u9700\u8981\u7f6e\u7a7a\n\/\/\t\tcustomers&#91;total - 1] = null;\n\/\/\t\ttotal--;\n\t\tcustomers&#91;--total] = null;\n\t\treturn true;\n\t}\n\t\n\t\/**\n\t * \n\t* @Description \u83b7\u53d6\u6240\u6709\u7684\u5ba2\u6237\u4fe1\u606f\n\t* @author RichieZhang\n\t* @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:32:35\n\t* @return\n\t *\/\n\tpublic Customer&#91;] getAllCustomers() {\n\/\/\t\treturn customers;\n\t\t\n\t\tCustomer&#91;] custs = new Customer&#91;total];\n\t\tfor(int i = 0; i &lt; total; i++) {\n\t\t\tcusts&#91;i] = customers&#91;i];\n\t\t}\n\t\treturn custs;\n\t\t\n\t}\n\t\n\t\/**\n\t * \n\t* @Description \u83b7\u53d6\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u4e0a\u7684\u5ba2\u6237\n\t* @author RichieZhang\n\t* @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:36:59\n\t* @param index\n\t* @return\t\u5982\u679c\u627e\u5230\u4e86\u5143\u7d20\uff0c\u5219\u8fd4\u56de\uff1b\u5982\u679c\u6ca1\u6709\u627e\u5230\uff0c\u5219\u8fd4\u56denull\n\t *\/\n\tpublic Customer getCustomer(int index) {\n\t\tif(index &lt; 0 || index &gt;= total) {\n\t\t\treturn null;\n\t\t}\n\t\t\n\t\treturn customers&#91;index];\n\t}\n\n\t\/**\n\t * \n\t* @Description \u83b7\u53d6\u5b58\u50a8\u7684\u5ba2\u6237\u7684\u6570\u91cf\n\t* @author RichieZhang\n\t* @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:40:06\n\t* @return\n\t *\/\n\tpublic int getTotal() {\n\t\treturn total;\n\/\/\t\treturn customers.length;\/\/\u9519\u8bef\u7684\n\t\t\n\t}\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P249.ui;\n\nimport top.qaqaq.java.P249.bean.Customer;\nimport top.qaqaq.java.P249.seriver.CustomerList;\nimport top.qaqaq.java.P249.util.CMUtility;\n\n\/**\n * \n * @Description CustomerView\u4e3a\u4e3b\u6a21\u5757\uff0c\u8d1f\u8d23\u83dc\u5355\u7684\u663e\u5f0f\u548c\u5904\u7406\u7528\u6237\u64cd\u4f5c\n * @author RichieZhang Email:18358892@qq.com\n * @version\n * @date 2022\u5e7410\u67083\u65e5\u4e0b\u53483:44:55\n *\n *\/\npublic class CustomerView {\n\n\tprivate CustomerList customerList = new CustomerList(10);\n\t\n\tpublic CustomerView() {\n\t\tCustomer customer = new Customer(\"\u738b\u6d9b\", '\u7537', 23, \"13212341234\", \"wt@gmail.com\");\n\t\tcustomerList.addCustomer(customer);\n\t}\n\n\t\/**\n\t * \u663e\u793a\u300a\u5ba2\u6237\u4fe1\u606f\u7ba1\u7406\u8f6f\u4ef6\u300b\u754c\u9762\u7684\u65b9\u6cd5\n\t * \n\t * @Description\n\t * @author RichieZhang\n\t * @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:46:05\n\t *\/\n\tpublic void enterMainMent() {\n\n\t\tboolean isFlag = true;\n\t\twhile(isFlag) {\n\t\t\tSystem.out.println(\"\\n-----------------\u5ba2\u6237\u4fe1\u606f\u7ba1\u7406\u8f6f\u4ef6-----------------\\n\");\n\t\t\tSystem.out.println(\"                   1 \u6dfb \u52a0 \u5ba2 \u6237\");\n\t\t\tSystem.out.println(\"                   2 \u4fee \u6539 \u5ba2 \u6237\");\n\t\t\tSystem.out.println(\"                   3 \u5220 \u9664 \u5ba2 \u6237\");\n\t\t\tSystem.out.println(\"                   4 \u5ba2 \u6237 \u5217 \u8868\");\n\t\t\tSystem.out.println(\"                   5 \u9000       \u51fa\\n\");\n\t\t\tSystem.out.print(\"                   \u8bf7\u9009\u62e9(1-5)\uff1a\");\n\t\t\t\n\t\t\tchar menu = CMUtility.readMenuSelection();\n\t\t\tswitch(menu) {\n\t\t\tcase '1':\n\t\t\t\taddNewCustomer();\n\t\t\t\tbreak;\n\t\t\tcase '2':\n\t\t\t\tmodifyCustomer();\n\t\t\t\tbreak;\n\t\t\tcase '3':\n\t\t\t\tdeleteCustmoer();\n\t\t\t\tbreak;\n\t\t\tcase '4':\n\t\t\t\tlistAllCustmoers();\n\t\t\t\tbreak;\n\t\t\tcase '5':\n\/\/\t\t\t\tSystem.out.println(\"\u9000\u51fa\");\n\t\t\t\t\n\t\t\t\tSystem.out.print(\"\u786e\u8ba4\u662f\u5426\u9000\u51fa(Y\/N)\uff1a\");\n\t\t\t\tchar isExit = CMUtility.readConfirmSelection();\n\t\t\t\tif(isExit == 'Y') {\n\t\t\t\t\tisFlag = false;\t\t\t\t\t\n\t\t\t\t}\n\/\/\t\t\t\tbreak;\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n\n\t\/**\n\t * \u6dfb\u52a0\u5ba2\u6237\u7684\u64cd\u4f5c\n\t * \n\t * @Description\n\t * @author RichieZhang\n\t * @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:46:33\n\t *\/\n\tprivate void addNewCustomer() {\n\n\/\/\t\tSystem.out.println(\"\u6dfb\u52a0\u5ba2\u6237\u7684\u64cd\u4f5c\");\n\t\tSystem.out.println(\"---------------------\u6dfb\u52a0\u5ba2\u6237---------------------\");\n\t\tSystem.out.print(\"\u59d3\u540d\uff1a\");\n\t\tString name = CMUtility.readString(10);\n\t\tSystem.out.print(\"\u6027\u522b\uff1a\");\n\t\tchar gender = CMUtility.readChar();\n\t\tSystem.out.print(\"\u5e74\u9f84\uff1a\");\n\t\tint age = CMUtility.readInt();\n\t\tSystem.out.print(\"\u7535\u8bdd\uff1a\");\n\t\tString phone = CMUtility.readString(13);\n\t\tSystem.out.print(\"\u90ae\u7bb1\uff1a\");\n\t\tString email = CMUtility.readString(30);\n\t\t\n\t\t\/\/\u5c06\u4e0a\u8ff0\u6570\u636e\u5c01\u88c5\u5230\u5bf9\u8c61\u4e2d\n\t\tCustomer customer = new Customer(name, gender, age, phone, email);\n\t\t\t\t\n\t\tboolean isSussess = customerList.addCustomer(customer);\n\t\tif(isSussess) {\n\t\t\tSystem.out.println(\"---------------------\u6dfb\u52a0\u5b8c\u6210---------------------\");\n\t\t}else {\n\t\t\tSystem.out.println(\"----------------\u5ba2\u6237\u76ee\u5f55\u5df2\u6ee1\uff0c\u6dfb\u52a0\u5931\u8d25----------------\");\n\t\t}\n\t}\n\n\t\/**\n\t * \u4fee\u6539\u5ba2\u6237\u7684\u64cd\u4f5c\n\t * \n\t * @Description\n\t * @author RichieZhang\n\t * @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:46:57\n\t *\/\n\tprivate void modifyCustomer() {\n\n\/\/\t\tSystem.out.println(\"\u4fee\u6539\u5ba2\u6237\u7684\u64cd\u4f5c\");\n\t\t\n\t\tSystem.out.println(\"---------------------\u4fee\u6539\u5ba2\u6237---------------------\");\n\t\t\n\t\tCustomer cust;\n\t\tint number;\n\t\tfor(; ;) {\n\t\t\tSystem.out.print(\"\u8bf7\u9009\u62e9\u5f85\u4fee\u6539\u5ba2\u6237\u7f16\u53f7(-1\u9000\u51fa)\uff1a\");\n\t\t\tnumber = CMUtility.readInt();\n\t\t\t\n\t\t\tif(number == -1) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\n\t\t\tcust = customerList.getCustomer(number - 1);\n\t\t\tif(cust == null) {\n\t\t\t\tSystem.out.println(\"\u65e0\u6cd5\u627e\u5230\u6307\u5b9a\u5ba2\u6237\uff01\");\n\t\t\t}else {\/\/\u627e\u5230\u4e86\u76f8\u5e94\u7f16\u53f7\u7684\u5ba2\u6237\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t\/\/\u4fee\u6539\u5ba2\u6237\u4fe1\u606f\n\t\tSystem.out.print(\"\u59d3\u540d(\" + cust.getName() + \"):\");\n\t\tString name = CMUtility.readString(10, cust.getName());\n\t\tSystem.out.print(\"\u6027\u522b(\" + cust.getGender() + \"):\");\n\t\tchar gender = CMUtility.readChar(cust.getGender());\n\t\tSystem.out.print(\"\u5e74\u9f84(\" + cust.getAge() + \"):\");\n\t\tint age = CMUtility.readInt(cust.getAge());\n\t\tSystem.out.print(\"\u7535\u8bdd(\" + cust.getPhone() + \"):\");\n\t\tString phone = CMUtility.readString(13, cust.getPhone());\n\t\tSystem.out.print(\"\u90ae\u7bb1(\" + cust.getEmail() + \"):\");\n\t\tString email = CMUtility.readString(30, cust.getEmail());\n\t\t\n\t\tCustomer newCust = new Customer(name, gender, age, phone, email);\n\t\t\n\t\tboolean isRepalaced = customerList.replaceCustomer(number - 1, newCust);\n\t\tif(isRepalaced) {\n\t\t\tSystem.out.println(\"---------------------\u4fee\u6539\u5b8c\u6210---------------------\");\n\t\t}else {\n\t\t\tSystem.out.println(\"---------------------\u4fee\u6539\u5931\u8d25---------------------\");\n\t\t}\n\t}\n\n\t\/**\n\t * \u5220\u9664\u5ba2\u6237\u7684\u64cd\u4f5c\n\t * \n\t * @Description\n\t * @author RichieZhang\n\t * @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:47:29\n\t *\/\n\tprivate void deleteCustmoer() {\n\n\/\/\t\tSystem.out.println(\"\u5220\u9664\u5ba2\u6237\u7684\u64cd\u4f5c\");\n\t\t\n\t\tSystem.out.println(\"---------------------\u5220\u9664\u5ba2\u6237---------------------\");\n\t\t\n\t\tint number;\n\t\tfor(; ;) {\n\t\t\t\n\t\t\tSystem.out.println(\"\u8bf7\u9009\u62e9\u5f85\u5220\u9664\u5ba2\u6237\u7f16\u53f7(-1\u9000\u51fa)\uff1a\");\n\t\t\tnumber = CMUtility.readInt();\n\t\t\t\n\t\t\tif(number == -1) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\t\n\t\t\tCustomer customer = customerList.getCustomer(number - 1);\n\t\t\tif(customer == null) {\n\t\t\t\tSystem.out.println(\"\u65e0\u6cd5\u627e\u5230\u6307\u5b9a\u5ba2\u6237\uff01\");\n\t\t\t}else {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\t\n\t\t\/\/\u627e\u5230\u4e86\u6307\u5b9a\u7684\u5ba2\u6237\n\t\tSystem.out.println(\"\u786e\u8ba4\u662f\u5426\u5220\u9664(Y\/N)\uff1a\");\n\t\tchar isDelete = CMUtility.readConfirmSelection();\n\t\tif(isDelete == 'Y') {\n\t\t\tboolean deleteSuccess = customerList.deleteCustomer(number - 1);\n\t\t\tif(deleteSuccess) {\n\t\t\t\tSystem.out.println(\"---------------------\u5220\u9664\u5b8c\u6210---------------------\");\n\t\t\t}else {\n\t\t\t\tSystem.out.println(\"---------------------\u5220\u9664\u5931\u8d25---------------------\");\n\t\t\t}\n\t\t}else {\n\t\t\treturn;\n\t\t}\n\t}\n\n\t\/**\n\t * \u663e\u793a\u5ba2\u6237\u5217\u8868\u7684\u64cd\u4f5c\n\t * \n\t * @Description\n\t * @author RichieZhang\n\t * @date 2022\u5e7410\u67083\u65e5\u4e0b\u53485:47:34\n\t *\/\n\tprivate void listAllCustmoers() {\n\n\/\/\t\tSystem.out.println(\"\u663e\u793a\u5ba2\u6237\u5217\u8868\u7684\u64cd\u4f5c\");\n\t\tSystem.out.println(\"---------------------------\u5ba2\u6237\u5217\u8868---------------------------\");\n\t\t\n\t\tint total = customerList.getTotal();\n\t\tif(total == 0) {\n\t\t\tSystem.out.println(\"\u6ca1\u6709\u5ba2\u6237\u8bb0\u5f55\uff01\");\n\t\t\t\n\t\t}else {\n\t\t\tSystem.out.println(\"\u7f16\u53f7\\t\u59d3\u540d\\t\u6027\u522b\\t\u5e74\u9f84\\t\u7535\u8bdd\\t\\t\u90ae\u7bb1\");\n\t\t\tCustomer&#91;] custs = customerList.getAllCustomers();\n\t\t\tfor(int i = 0; i &lt; custs.length; i++) {\n\t\t\t\tCustomer cust = custs&#91;i];\n\t\t\t\tSystem.out.println(i + 1 + \"\\t\" + cust.getName() + \n\t\t\t\t\t\t\"\\t\" + cust.getGender() + \"\\t\" + cust.getAge() + \n\t\t\t\t\t\t\"\\t\" + cust.getPhone() + \"\\t\" + cust.getEmail());\n\t\t\t}\n\t\t\t\n\t\t}\n\t\t\n\t\t\n\t\tSystem.out.println(\"-------------------------\u5ba2\u6237\u5217\u8868\u5b8c\u6210-------------------------\");\n\t}\n\n\tpublic static void main(String&#91;] args) {\n\t\tCustomerView view = new CustomerView();\n\t\tview.enterMainMent();\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P249.util;\n\n\nimport java.util.*;\n\/**\nCMUtility\u5de5\u5177\u7c7b\uff1a\n\u5c06\u4e0d\u540c\u7684\u529f\u80fd\u5c01\u88c5\u4e3a\u65b9\u6cd5\uff0c\u5c31\u662f\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u8c03\u7528\u65b9\u6cd5\u4f7f\u7528\u5b83\u7684\u529f\u80fd\uff0c\u800c\u65e0\u9700\u8003\u8651\u5177\u4f53\u7684\u529f\u80fd\u5b9e\u73b0\u7ec6\u8282\u3002\n*\/\npublic class CMUtility {\n    private static Scanner scanner = new Scanner(System.in);\n    \/**\n\t\u7528\u4e8e\u754c\u9762\u83dc\u5355\u7684\u9009\u62e9\u3002\u8be5\u65b9\u6cd5\u8bfb\u53d6\u952e\u76d8\uff0c\u5982\u679c\u7528\u6237\u952e\u5165\u20191\u2019-\u20195\u2019\u4e2d\u7684\u4efb\u610f\u5b57\u7b26\uff0c\u5219\u65b9\u6cd5\u8fd4\u56de\u3002\u8fd4\u56de\u503c\u4e3a\u7528\u6237\u952e\u5165\u5b57\u7b26\u3002\n\t*\/\n\tpublic static char readMenuSelection() {\n        char c;\n        for (; ; ) {\n            String str = readKeyBoard(1, false);\n            c = str.charAt(0);\n            if (c != '1' &amp;&amp; c != '2' &amp;&amp; \n                c != '3' &amp;&amp; c != '4' &amp;&amp; c != '5') {\n                System.out.print(\"\u9009\u62e9\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff1a\");\n            } else break;\n        }\n        return c;\n    }\n\t\/**\n\t\u4ece\u952e\u76d8\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n\t*\/\n    public static char readChar() {\n        String str = readKeyBoard(1, false);\n        return str.charAt(0);\n    }\n\t\/**\n\t\u4ece\u952e\u76d8\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n\t\u5982\u679c\u7528\u6237\u4e0d\u8f93\u5165\u5b57\u7b26\u800c\u76f4\u63a5\u56de\u8f66\uff0c\u65b9\u6cd5\u5c06\u4ee5defaultValue \u4f5c\u4e3a\u8fd4\u56de\u503c\u3002\n\t*\/\n    public static char readChar(char defaultValue) {\n        String str = readKeyBoard(1, true);\n        return (str.length() == 0) ? defaultValue : str.charAt(0);\n    }\n\t\/**\n\t\u4ece\u952e\u76d8\u8bfb\u53d6\u4e00\u4e2a\u957f\u5ea6\u4e0d\u8d85\u8fc72\u4f4d\u7684\u6574\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n\t*\/\n    public static int readInt() {\n        int n;\n        for (; ; ) {\n            String str = readKeyBoard(2, false);\n            try {\n                n = Integer.parseInt(str);\n                break;\n            } catch (NumberFormatException e) {\n                System.out.print(\"\u6570\u5b57\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff1a\");\n            }\n        }\n        return n;\n    }\n\t\/**\n\t\u4ece\u952e\u76d8\u8bfb\u53d6\u4e00\u4e2a\u957f\u5ea6\u4e0d\u8d85\u8fc72\u4f4d\u7684\u6574\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n\t\u5982\u679c\u7528\u6237\u4e0d\u8f93\u5165\u5b57\u7b26\u800c\u76f4\u63a5\u56de\u8f66\uff0c\u65b9\u6cd5\u5c06\u4ee5defaultValue \u4f5c\u4e3a\u8fd4\u56de\u503c\u3002\n\t*\/\n    public static int readInt(int defaultValue) {\n        int n;\n        for (; ; ) {\n            String str = readKeyBoard(2, true);\n            if (str.equals(\"\")) {\n                return defaultValue;\n            }\n\n            try {\n                n = Integer.parseInt(str);\n                break;\n            } catch (NumberFormatException e) {\n                System.out.print(\"\u6570\u5b57\u8f93\u5165\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff1a\");\n            }\n        }\n        return n;\n    }\n\t\/**\n\t\u4ece\u952e\u76d8\u8bfb\u53d6\u4e00\u4e2a\u957f\u5ea6\u4e0d\u8d85\u8fc7limit\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n\t*\/\n    public static String readString(int limit) {\n        return readKeyBoard(limit, false);\n    }\n\t\/**\n\t\u4ece\u952e\u76d8\u8bfb\u53d6\u4e00\u4e2a\u957f\u5ea6\u4e0d\u8d85\u8fc7limit\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n\t\u5982\u679c\u7528\u6237\u4e0d\u8f93\u5165\u5b57\u7b26\u800c\u76f4\u63a5\u56de\u8f66\uff0c\u65b9\u6cd5\u5c06\u4ee5defaultValue \u4f5c\u4e3a\u8fd4\u56de\u503c\u3002\n\t*\/\n    public static String readString(int limit, String defaultValue) {\n        String str = readKeyBoard(limit, true);\n        return str.equals(\"\")? defaultValue : str;\n    }\n\t\/**\n\t\u7528\u4e8e\u786e\u8ba4\u9009\u62e9\u7684\u8f93\u5165\u3002\u8be5\u65b9\u6cd5\u4ece\u952e\u76d8\u8bfb\u53d6\u2018Y\u2019\u6216\u2019N\u2019\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u3002\n\t*\/\n    public static char readConfirmSelection() {\n        char c;\n        for (; ; ) {\n            String str = readKeyBoard(1, false).toUpperCase();\n            c = str.charAt(0);\n            if (c == 'Y' || c == 'N') {\n                break;\n            } else {\n                System.out.print(\"\u9009\u62e9\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff1a\");\n            }\n        }\n        return c;\n    }\n\n    private static String readKeyBoard(int limit, boolean blankReturn) {\n        String line = \"\";\n\n        while (scanner.hasNextLine()) {\n            line = scanner.nextLine();\n            if (line.length() == 0) {\n                if (blankReturn) return line;\n                else continue;\n            }\n\n            if (line.length() &lt; 1 || line.length() &gt; limit) {\n                System.out.print(\"\u8f93\u5165\u957f\u5ea6\uff08\u4e0d\u5927\u4e8e\" + limit + \"\uff09\u9519\u8bef\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\uff1a\");\n                continue;\n            }\n            break;\n        }\n\n        return line;\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9762\u5411\u5bf9\u8c61(\u4e2d)-\u9879\u76ee\u4e8c\u8f6f\u4ef6\u7ed3\u6784\u8bbe\u8ba1\u3001\u9879\u76ee\u4e8cCMUtility\u5de5\u5177\u7c7b\u7684\u529f\u80fd\u4ecb\u7ecd\u3001\u9879\u76ee\u4e8c\uff1aCustomer\u7c7b\u7684\u8bbe\u8ba1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-673","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/673"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=673"}],"version-history":[{"count":2,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/673\/revisions"}],"predecessor-version":[{"id":2652,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/673\/revisions\/2652"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}