{"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":"
\n

\u9996\u5148\u8ba9\u6211\u4eec\u6765\u89c2\u5bdf\u4e00\u4e0b\u9700\u8981\u5904\u7406\u7684\u7f51\u9875\u7684source code.<\/p>\n\n\n\n\n
\n
\n \u8d27\u5e01\u540d\u79f0<\/font><\/font><\/b>\n <\/div>\n<\/td>\n
\n
\n \u73b0\u6c47\u4e70\u5165\u4ef7<\/font><\/font><\/b>\n <\/div>\n<\/td>\n
\n
\n \u73b0\u949e\u4e70\u5165\u4ef7<\/font><\/font><\/b>\n <\/div>\n<\/td>\n
\n
\n \u5356\u51fa\u4ef7<\/font><\/font><\/b>\n <\/div>\n<\/td>\n
\n
\n \u4e2d\u95f4\u4ef7<\/font><\/b>\n <\/div>\n<\/td>\n
\n
\n \u57fa\u51c6\u4ef7<\/font><\/b>\n <\/div>\n<\/td>\n<\/tr>\n
\n
\n \u7f8e\u5143 USD\n <\/div>\n<\/td>\n
\n
\n 621.8700 \n <\/div>\n<\/td>\n
\n
\n 616.8900 \n <\/div>\n<\/td>\n
\n
\n 624.3700 \n <\/div>\n<\/td>\n
\n
\n 623.1200 \n <\/div>\n<\/td>\n
\n
\n 615.5700 \n <\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\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

\\(.*?\\): now \"\\1\" means \"\u7f8e\u5143 USD\"\\(.*?\\) <\/p>\n

\u5bf9\u4e8e\u4e0a\u8ff0\u5b57\u7b26\u7684\u8bc6\u522b\u53ef\u4ee5\u7528\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u5b9e\u73b0:<\/p>\n

str.startwith(token), where token = \"<\/p>\n

str.startwith(\"<\/p>\n

str.startwith(\"<\/p>\n

to extract the unit of the money<\/p>\n

import java.util.regex.*;<\/p>\n

String patt = \"<\/p>\n

\\\\(.*?\\\\)\" <\/p>\n

Pattern r = Pattern.compile(patt);<\/p>\n

Matcher m = r.matcher(str);<\/p>\n

m.group(1); \/\/ is the result, such as \"\u7f8e\u5143 USD\"<\/p>\n

\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u53ef\u4ee5\u83b7\u5f97rates<\/p>\n

\u7136\u540e\u7f16\u5199\u4ee3\u7801\u8fdb\u884c\u5b9e\u73b0, \u5177\u4f53\u5b9e\u73b0\u601d\u8def\u5982\u4e0b:<\/p>\n

\u5b9e\u73b0html\u6e90\u4ee3\u7801\u4e2d\u5173\u4e8e\u6c47\u7387\u8868\u7684\u76f8\u5173\u5185\u5bb9\u884c\u7684\u8bc6\u522b<\/p>\n

\u7136\u540e, \u4ece\u7279\u5b9a\u7684\u884c\u4e2d\u63d0\u53d6\u51fa\u76f8\u5e94\u7684\u6c47\u7387\u4fe1\u606f<\/p>\n

classRate {\/\/\u4ece\u7f51\u7ad9:http:\/\/www.usd-cny.com\/\u4e2d\u83b7\u53d6\u6700\u65b0\u7684\u6c47\u7387\u4fe1\u606f<\/p>\n

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

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

public static void update() throwsException {
\n <\/p>\n

URL hp= newURL(webSite);<\/p>\n

URLConnection hpCon=hp.openConnection();<\/p>\n

System.out.println(\"== Content ==\");<\/p>\n

InputStream input=(InputStream)hpCon.getInputStream();<\/p>\n

BufferedReader br= new BufferedReader(newInputStreamReader(input,\"gb2312\"));<\/p>\n

String str= null;boolean inTable = false;int nRows = 0;<\/p>\n

String matchStr= null;while (( str = br.readLine() ) != null) {
\n <\/p>\n

str=str.trim();\/\/\u5224\u65ad\u662f\u5426\u8fdb\u5165\u6c47\u7387\u8868\u7684\u52bf\u529b\u8303\u56f4\u5185\u90e8<\/p>\n

if (str.startsWith(\"<\/p>\n

inTable= true;continue;<\/p>\n

}if (str.startsWith(\"<\/p>\n

}if (inTable == false)continue;if (str.startsWith(\"<\/p>\n

nRows+= 1;\/\/\u5ffd\u7565\u7b2c\u4e00\u884c\u7684\u6807\u9898<\/p>\n

if (nRows == 1) continue;\/\/\u6c47\u7387\u8868\u7684\u8bfb\u53d6\u53ea\u5230\u6e2f\u5e01<\/p>\n

if (nRows == RateInfo.NKINDS+2) break;\/\/\u83b7\u5f97\u7b2c\u4e00\u5217\u7684\u5b8c\u6574\u4ee3\u7801<\/p>\n

str =br.readLine().trim();<\/p>\n

str= str +br.readLine().trim();\/\/\u83b7\u53d6\u5e01\u79cd\u7f29\u5199<\/p>\n

String patt = \"<\/p>\n

(.*)\"; <\/p>\n

Pattern r=Pattern.compile(patt);<\/p>\n

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

if(m.find()) {
\n <\/p>\n

matchStr= m.group(1);<\/p>\n

matchStr= (matchStr.split(\" \"))[1];<\/p>\n

System.out.println(matchStr);<\/p>\n

}else{
\n <\/p>\n

System.out.println(\"No Match\");<\/p>\n

}for (int i = 0; i < RateInfo.NELEM; i++) {
\n <\/p>\n

str=br.readLine();<\/p>\n

String pattE= \"<\/p>\n

(.*?) \"; <\/p>\n

r=Pattern.compile(pattE);<\/p>\n

m=r.matcher(str);if(m.find())<\/p>\n

System.out.println(m.group(1));elseSystem.out.println(\"No Match\");<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

input.close();<\/p>\n

}\/\/\u8bbe\u7f6e\u4e0d\u540c\u8d27\u5e01\u4e4b\u95f4\u7684\u5229\u7387\/\/1 $from * $rate = 1 $to<\/p>\n

public static void setRate(String from, String to, doublerate) {
\n <\/p>\n

rateTable.put(from+to, newDouble(rate));<\/p>\n

}public staticDouble getRate(String from, String to) {return 615.65;\/\/return (Double) rateTable.get(from + to);<\/p>\n

}\/\/\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

public staticMoney exchangeRate(Money m, String to) {if (m.unit.equals(to)) return newMoney(m);<\/p>\n

Double rate=getRate(m.unit, to);if (rate == null) {throw newIllegalArgumentException();<\/p>\n

}return new Money(m.amount*rate.doubleValue(), to);<\/p>\n

}<\/p>\n

}<\/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}]}}