{"id":7294,"date":"2024-04-08T22:01:01","date_gmt":"2024-04-08T14:01:01","guid":{"rendered":""},"modified":"2024-04-08T22:01:01","modified_gmt":"2024-04-08T14:01:01","slug":"java \u6c47\u7387\u6362\u7b97_[java] \u6c47\u7387\u6362\u7b97\u5668\u5b9e\u73b0(2)","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7294.html","title":{"rendered":"java \u6c47\u7387\u6362\u7b97_[java] \u6c47\u7387\u6362\u7b97\u5668\u5b9e\u73b0(2)"},"content":{"rendered":"<div style=\"font-size:16px;\">\n<p>\u9996\u5148\u8ba9\u6211\u4eec\u6765\u89c2\u5bdf\u4e00\u4e0b\u9700\u8981\u5904\u7406\u7684\u7f51\u9875\u7684source code.<\/p>\n<table cellspacing=\"0\" cellpadding=\"0\" width=\"777\" align=\"center\">\n<tbody>\n<tr bgcolor=\"#E1FDE8\">\n<td width=\"121\" height=\"28\" bgcolor=\"#E1FDE8\">\n<div align=\"center\">\n      <b><font color=\"#000000\"><font lang=\"zh-cn\">\u8d27\u5e01\u540d\u79f0<\/font><\/font><\/b>\n     <\/div>\n<\/td>\n<td width=\"133\" height=\"21\">\n<div align=\"center\">\n      <b><font color=\"#000000\"><font lang=\"zh-cn\">\u73b0\u6c47\u4e70\u5165\u4ef7<\/font><\/font><\/b>\n     <\/div>\n<\/td>\n<td width=\"137\" height=\"21\">\n<div align=\"center\">\n      <b><font color=\"#000000\"><font lang=\"zh-cn\">\u73b0\u949e\u4e70\u5165\u4ef7<\/font><\/font><\/b>\n     <\/div>\n<\/td>\n<td width=\"132\">\n<div align=\"center\">\n      <b><font color=\"#000000\"><font lang=\"zh-cn\">\u5356\u51fa\u4ef7<\/font><\/font><\/b>\n     <\/div>\n<\/td>\n<td width=\"130\">\n<div align=\"center\">\n      <b><font color=\"#000000\">\u4e2d\u95f4\u4ef7<\/font><\/b>\n     <\/div>\n<\/td>\n<td width=\"133\" height=\"21\">\n<div align=\"center\">\n      <b><font color=\"#000000\">\u57fa\u51c6\u4ef7<\/font><\/b>\n     <\/div>\n<\/td>\n<\/tr>\n<tr bgcolor=\"#FFFFFF\">\n<td height=\"28\" bgcolor=\"#FFFFFF\">\n<div align=\"center\">\n      \u7f8e\u5143 USD\n     <\/div>\n<\/td>\n<td height=\"15\" align=\"center\">\n<div align=\"right\">\n       621.8700&nbsp;\n     <\/div>\n<\/td>\n<td height=\"15\" align=\"center\">\n<div align=\"right\">\n       616.8900&nbsp;\n     <\/div>\n<\/td>\n<td align=\"center\">\n<div align=\"right\">\n       624.3700&nbsp;\n     <\/div>\n<\/td>\n<td align=\"center\">\n<div align=\"right\">\n       623.1200&nbsp;\n     <\/div>\n<\/td>\n<td height=\"15\" align=\"center\">\n<div align=\"right\">\n       615.5700&nbsp;\n     <\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5206\u6790\u53d1\u73b0, \u6c47\u7387\u8868\u7531\u5927\u5199\u7684TABLE\u5305\u62ec\u8d77\u6765, \u6bcf\u4e00\u884c\u7531TR\u5305\u56f4, \u6bcf\u4e00\u9879\u7531TD\u5305\u56f4. \u56e0\u6b64, \u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a:<\/p>\n<p>\\(.*?\\): now \"\\1\" means \"\u7f8e\u5143 USD\"\\(.*?\\) <\/p>\n<p>\u5bf9\u4e8e\u4e0a\u8ff0\u5b57\u7b26\u7684\u8bc6\u522b\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u5b9e\u73b0:<\/p>\n<p>str.startwith(token), where token = \"<\/p>\n<p>str.startwith(\"<\/p>\n<p>str.startwith(\"<\/p>\n<p>to extract the unit of the money<\/p>\n<p>import java.util.regex.*;<\/p>\n<p>String patt = \"<\/p>\n<p>\\\\(.*?\\\\)\" <\/p>\n<p>Pattern r = Pattern.compile(patt);<\/p>\n<p>Matcher m = r.matcher(str);<\/p>\n<p>m.group(1); \/\/ is the result, such as \"\u7f8e\u5143 USD\"<\/p>\n<p>\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u53ef\u4ee5\u83b7\u5f97rates<\/p>\n<p>\u7136\u540e\u7f16\u5199\u4ee3\u7801\u8fdb\u884c\u5b9e\u73b0, \u5177\u4f53\u5b9e\u73b0\u601d\u8def\u5982\u4e0b:<\/p>\n<p>\u5b9e\u73b0html\u6e90\u4ee3\u7801\u4e2d\u5173\u4e8e\u6c47\u7387\u8868\u7684\u76f8\u5173\u5185\u5bb9\u884c\u7684\u8bc6\u522b<\/p>\n<p>\u7136\u540e, \u4ece\u7279\u5b9a\u7684\u884c\u4e2d\u63d0\u53d6\u51fa\u76f8\u5e94\u7684\u6c47\u7387\u4fe1\u606f<\/p>\n<p>classRate {\/\/\u4ece\u7f51\u7ad9:http:\/\/www.usd-cny.com\/\u4e2d\u83b7\u53d6\u6700\u65b0\u7684\u6c47\u7387\u4fe1\u606f<\/p>\n<p>final static String webSite = \"http:\/\/www.usd-cny.com\/\";\/\/\u5229\u7528hashtable\u5bf9\u4e0d\u540c\u8d27\u5e01\u4e4b\u95f4\u7684\u5229\u7387\u8fdb\u884c\u5b58\u50a8\/\/key: $from+$to, value: $rate<\/p>\n<p>private static Hashtable rateTable = newHashtable();\/\/\u4ece\u7f51\u4e0a\u81ea\u52a8\u66f4\u65b0\u6c47\u7387\u4fe1\u606f\/\/\u53ea\u5c06\u524d16\u4e2a\u5177\u6709\u5b8c\u6574\u6c47\u7387\u4fe1\u606f\u7684\u5185\u5bb9\u8fdb\u884c\u5b58\u50a8<\/p>\n<p>public static void update() throwsException {<br \/>\n  <!-- --><\/p>\n<p>URL hp= newURL(webSite);<\/p>\n<p>URLConnection hpCon=hp.openConnection();<\/p>\n<p>System.out.println(\"== Content ==\");<\/p>\n<p>InputStream input=(InputStream)hpCon.getInputStream();<\/p>\n<p>BufferedReader br= new BufferedReader(newInputStreamReader(input,\"gb2312\"));<\/p>\n<p>String str= null;boolean inTable = false;int nRows = 0;<\/p>\n<p>String matchStr= null;while (( str = br.readLine() ) != null) {<br \/>\n  <!-- --><\/p>\n<p>str=str.trim();\/\/\u5224\u65ad\u662f\u5426\u8fdb\u5165\u6c47\u7387\u8868\u7684\u52bf\u529b\u8303\u56f4\u5185\u90e8<\/p>\n<p>if (str.startsWith(\"<\/p>\n<p>inTable= true;continue;<\/p>\n<p>}if (str.startsWith(\"<\/p>\n<p>}if (inTable == false)continue;if (str.startsWith(\"<\/p>\n<p>nRows+= 1;\/\/\u5ffd\u7565\u7b2c\u4e00\u884c\u7684\u6807\u9898<\/p>\n<p>if (nRows == 1) continue;\/\/\u6c47\u7387\u8868\u7684\u8bfb\u53d6\u53ea\u5230\u6e2f\u5e01<\/p>\n<p>if (nRows == RateInfo.NKINDS+2) break;\/\/\u83b7\u5f97\u7b2c\u4e00\u5217\u7684\u5b8c\u6574\u4ee3\u7801<\/p>\n<p>str =br.readLine().trim();<\/p>\n<p>str= str +br.readLine().trim();\/\/\u83b7\u53d6\u5e01\u79cd\u7f29\u5199<\/p>\n<p>String patt = \"<\/p>\n<p>(.*)\"; <\/p>\n<p>Pattern r=Pattern.compile(patt);<\/p>\n<p>Matcher m=r.matcher(str);\/\/matchStr = m.group(1);\/\/\u5c06\u6c49\u5b57\u4e0e\u7f29\u5199\u8fdb\u884c\u5206\u79bb\/\/matchStr = (matchStr.split())[1];<\/p>\n<p>if(m.find()) {<br \/>\n  <!-- --><\/p>\n<p>matchStr= m.group(1);<\/p>\n<p>matchStr= (matchStr.split(\" \"))[1];<\/p>\n<p>System.out.println(matchStr);<\/p>\n<p>}else{<br \/>\n  <!-- --><\/p>\n<p>System.out.println(\"No Match\");<\/p>\n<p>}for (int i = 0; i &lt; RateInfo.NELEM; i++) {<br \/>\n  <!-- --><\/p>\n<p>str=br.readLine();<\/p>\n<p>String pattE= \"<\/p>\n<p>(.*?)&nbsp;\"; <\/p>\n<p>r=Pattern.compile(pattE);<\/p>\n<p>m=r.matcher(str);if(m.find())<\/p>\n<p>System.out.println(m.group(1));elseSystem.out.println(\"No Match\");<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>input.close();<\/p>\n<p>}\/\/\u8bbe\u7f6e\u4e0d\u540c\u8d27\u5e01\u4e4b\u95f4\u7684\u5229\u7387\/\/1 $from * $rate = 1 $to<\/p>\n<p>public static void setRate(String from, String to, doublerate) {<br \/>\n  <!-- --><\/p>\n<p>rateTable.put(from+to, newDouble(rate));<\/p>\n<p>}public staticDouble getRate(String from, String to) {return 615.65;\/\/return (Double) rateTable.get(from + to);<\/p>\n<p>}\/\/\u5c06\u4e00\u5b9a\u91cf\u7684\u8d27\u5e01$m, \u8f6c\u53d8\u6210\u5355\u4f4d\u4e3a$to\u7684\u8d27\u5e01\u91cf\/\/return: \u76f8\u5e94\u7684\u8d27\u5e01\u503c<\/p>\n<p>public staticMoney exchangeRate(Money m, String to) {if (m.unit.equals(to)) return newMoney(m);<\/p>\n<p>Double rate=getRate(m.unit, to);if (rate == null) {throw newIllegalArgumentException();<\/p>\n<p>}return new Money(m.amount*rate.doubleValue(), to);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"java \u6c47\u7387\u6362\u7b97_[java] \u6c47\u7387\u6362\u7b97\u5668\u5b9e\u73b0(2)\u9996\u5148\u8ba9\u6211\u4eec\u6765\u89c2\u5bdf\u4e00\u4e0b\u9700\u8981\u5904\u7406\u7684\u7f51\u9875\u7684sourcecode.\u8d27\u5e01\u540d\u79f0\u73b0\u6c47\u4e70\u5165\u4ef7\u73b0\u949e\u4e70\u5165\u4ef7\u5356\u51fa...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7294"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7294"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7294\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}