//試著寫(xiě)了一個(gè)~~
創(chuàng)新互聯(lián)建站是一家專注于網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),林州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:林州等地區(qū)。林州做網(wǎng)站價(jià)格咨詢:028-86922220
///: Filename: ForeignCurrency.java
public class ForeignCurrency {
/**font color='blue'外幣兌本幣,Bca/B 要兌換的外幣金額*/
public double f2RMB(Currency src,double ca){
return ca*src.getExchangeRate();
}
/**font color='blue'本幣兌外幣,BRMB/B 要兌換的本幣金額*/
public double RMB2F(Currency dest,double RMB){
return RMB/dest.getExchangeRate();
}
/**font color='blue'外幣兌外幣,Bcash/B 要兌換的原外幣(src)金額*/
public double f2F(Currency src,Currency dest,double cash){
return src.getExchangeRate()/dest.getExchangeRate()*cash;
}
public static void main(String[] args){
ForeignCurrency x = new ForeignCurrency();
Currency rmb = new Currency(CurType.本幣,1);//定義一個(gè)RMB類,兌RMB的匯率是 1
Currency us$ = new Currency(CurType.美元,7.3);//定義一個(gè)美金類,兌RMB的匯率是 7.3
Currency au$ = new Currency(CurType.澳元,9.65);//定義一個(gè)澳元類,兌RMB的匯率是 9.65
Currency jp$ = new Currency(CurType.日元,0.0101);//定義一個(gè)日元類,兌RMB的匯率是 0.0101
double RMB = 888;
double US$ = 123;
double AU$ = 234;
double JP$ = 5678.9;
//美元兌換RMB;
double us2rmb = x.f2RMB(us$, US$);
System.out.println(us$.getCURName()+": "+US$+" - "+rmb.getCURName()+": "+us2rmb+" 匯率:"+us$.getExchangeRate()/rmb.getExchangeRate());
//澳元兌換RMB
double au2rmb = x.f2RMB(au$, AU$);
System.out.println(au$.getCURName()+": "+AU$+" - "+rmb.getCURName()+": "+au2rmb+" 匯率:"+au$.getExchangeRate()/rmb.getExchangeRate());
//日元兌換RMB
double jp2rmb = x.f2RMB(jp$, JP$);
System.out.println(jp$.getCURName()+": "+JP$+" - "+rmb.getCURName()+": "+jp2rmb+" 匯率:"+jp$.getExchangeRate()/rmb.getExchangeRate());
//美元兌換澳元
double us2au = x.f2F(us$, au$,US$);
System.out.println(us$.getCURName()+": "+US$+" - "+au$.getCURName()+": "+us2au+" 匯率:"+us$.getExchangeRate()/au$.getExchangeRate());
//....................
//RMB兌換澳元
double rmb2au = x.RMB2F(au$,RMB);
System.out.println(rmb.getCURName()+": "+RMB+" - "+au$.getCURName()+": "+rmb2au+" 匯率:"+rmb.getExchangeRate()/au$.getExchangeRate());
//....................
}
}
//貨幣名稱枚舉
enum CurType{
本幣,美元,澳元,日元,法郎//分別指:人民幣,美金,澳元,日元,法郎...
}
//貨幣類,所有匯率都是以RMB為基準(zhǔn)的
class Currency{
private double xr;//對(duì)RMB的匯率
private CurType type;//名稱,比如US$
/**font color='red'參數(shù) CUR 貨幣名稱,double 對(duì)RMB的匯率*/
public Currency(CurType c,double xRMB){
xr = xRMB;
type = c;
}
/**font color='blue'獲取外幣對(duì)RMB的匯率*/
public double getExchangeRate() {
return xr;
}
/**font color='blue'獲取貨幣名稱*/
public String getCURName(){return type.name();}
}
java輸出貨比符號(hào)測(cè)試方法:
import?java.text.NumberFormat;
import?java.util.Locale;
public?class?FormatTest?{
public?static?void?main(String?args[])?{
//?不使用格式化輸出數(shù)
double?d?=?10.0?/?3.0;
System.out.println("無(wú)格式化輸出:"?+?d);
//?使用本地默認(rèn)格式輸出數(shù)
NumberFormat?numberFormat?=?NumberFormat.getNumberInstance();
//numberFormat.setMaximumFractionDigits(4);
//numberFormat.setMinimumIntegerDigits(6);
String?numberString?=?numberFormat.format(d);
System.out.println("本地默認(rèn)格式輸出數(shù):"?+?numberString);
//?使用本地默認(rèn)格式輸出貨幣值
NumberFormat?currencyFormat?=?NumberFormat.getCurrencyInstance();
System.out.println("本地默認(rèn)格式輸出貨幣值:"?+?currencyFormat.format(d));
//?使用本地默認(rèn)格式輸出百分?jǐn)?shù)
NumberFormat?percentFormat?=?NumberFormat.getPercentInstance();
System.out.println("本地默認(rèn)格式輸出百分?jǐn)?shù):"?+?percentFormat.format(d));
//?在不同的國(guó)家和地區(qū)數(shù)字表示的格式也有區(qū)別。如德國(guó)
//?使用德國(guó)的格式化輸出數(shù)
NumberFormat?numberFormatG?=?NumberFormat
.getNumberInstance(Locale.GERMANY);
System.out.println("德國(guó)數(shù)字輸出形式:"?+?numberFormatG.format(d));
//?使用德國(guó)貨幣輸出形式
NumberFormat?currencyFormatG?=?NumberFormat
.getCurrencyInstance(Locale.GERMANY);
System.out.println("德國(guó)貨幣輸出形式:"?+?currencyFormatG.format(d));
//?使用美國(guó)貨幣輸出形式
NumberFormat?currencyFormatA?=?NumberFormat
.getCurrencyInstance(Locale.US);
System.out.println("美國(guó)貨幣輸出形式:"?+?currencyFormatA.format(d));
//?使用德國(guó)百分?jǐn)?shù)輸出形式
NumberFormat?percentFormatG?=?NumberFormat
.getPercentInstance(Locale.GERMANY);
System.out.println("德國(guó)百分?jǐn)?shù)輸出形式:"?+?percentFormatG.format(d));
System.exit(0);
}
}
程序:
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class test8{ public static void main(String[] args) { JFrame f=new JFrame(); f.setSize(200, 200); JTextArea t1 = new JTextArea(1,5); JTextArea t2 = new JTextArea(1,5); JButton b1=new JButton("test it"); Label l1,l2; l1 = new Label(); l2 = new Label(); l1.setText("Please Enter The Number"); l2.setText("The Result Of The Test"); Choice c = new Choice(); c.add("RMB"); c.add("KRW"); c.add("DOLLAR"); Choice c1 = new Choice(); c1.add("RMB"); c1.add("KRW"); c1.add("DOLLAR"); f.add(l1); f.add(t1); f.add(c); f.add(l2); f.add(t2); f.add(c1); f.add(b1); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {if(c.getSelectedItem()=="RMB"c1.getSelectedItem()=="DOLLAR") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*0.1452; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="DOLLAR"c1.getSelectedItem()=="RMB") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*6.8866; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="RMB"c1.getSelectedItem()=="KRW") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*164.9824; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="KRW"c1.getSelectedItem()=="RMB") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*0.0061; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="DOLLAR"c1.getSelectedItem()=="KRW") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*1136.2500; String r = Double.toString(w); t2.setText(r); } else if(c.getSelectedItem()=="KRW"c1.getSelectedItem()=="DOLLAR") {String s; s=t1.getText(); int q=0; q=Integer.parseInt(s); double w; w=q*0.0009; String r = Double.toString(w); t2.setText(r); } } } ); f.setLayout(new FlowLayout()); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
操作環(huán)境:java 版型號(hào):8.64
拓展資料:
1、Java是一門面向?qū)ο缶幊陶Z(yǔ)言,不僅吸收了C++語(yǔ)言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語(yǔ)言具有功能強(qiáng)大和簡(jiǎn)單易用兩個(gè)特征。Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,極好地實(shí)現(xiàn)了面向?qū)ο罄碚?,允許程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、健壯性、安全性、平臺(tái)獨(dú)立與可移植性、多線程、動(dòng)態(tài)性等特點(diǎn)。Java可以編寫(xiě)桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等
2、由于C++所具有的優(yōu)勢(shì),該項(xiàng)目組的研究人員首先考慮采用C++來(lái)編寫(xiě)程序。但對(duì)于硬件資源極其匱乏的單片式系統(tǒng)來(lái)說(shuō),C++程序過(guò)于復(fù)雜和龐大。另外由于消費(fèi)電子產(chǎn)品所采用的嵌入式處理器芯片的種類繁雜,如何讓編寫(xiě)的程序跨平臺(tái)運(yùn)行也是個(gè)難題。為了解決困難,他們首先著眼于語(yǔ)言的開(kāi)發(fā),假設(shè)了一種結(jié)構(gòu)簡(jiǎn)單、符合嵌入式應(yīng)用需要的硬件平臺(tái)體系結(jié)構(gòu)并為其制定了相應(yīng)的規(guī)范,其中就定義了這種硬件平臺(tái)的二進(jìn)制機(jī)器碼指令系統(tǒng)(即后來(lái)成為“字節(jié)碼”的指令系統(tǒng)),以待語(yǔ)言開(kāi)發(fā)成功后,能有半導(dǎo)體芯片生產(chǎn)商開(kāi)發(fā)和生產(chǎn)這種硬件平臺(tái)。對(duì)于新語(yǔ)言的設(shè)計(jì),Sun公司研發(fā)人員并沒(méi)有開(kāi)發(fā)一種全新的語(yǔ)言,而是根據(jù)嵌入式軟件的要求,對(duì)C++進(jìn)行了改造,去除了留在C++的一些不太實(shí)用及影響安全的成分,并結(jié)合嵌入式系統(tǒng)的實(shí)時(shí)性要求,開(kāi)發(fā)了一種稱為Oak的面向?qū)ο笳Z(yǔ)言。
操作環(huán)境:java 版型號(hào):8.64
操作環(huán)境:C++ 版型號(hào):8.2.64
import java.util.ArrayList;
/*
* 一元錢硬幣有多少種表達(dá)方式
* 可供選擇:1分,2分,5分,1角,2角,5角,1元
* 如:
* 1元=1元;
* 1元=5角+5角;
* ....
*/
public class Test {
private static int count;
public static void main(String args[]){
int max = 100;//一元
int[] cents = {100,50,20,10,5,2,1};//幣值
String[] money = {"1元","5角","2角","1角","5分","2分","1分"};
ArrayList collect = new ArrayList();
collectMoney(cents, money,0, max, 0, collect);
System.out.println("總共有"+count+"種搭配方法!");
}
public static void collectMoney(int[] cents,String[] money,int beginIndex,int max,int result,ArrayList collect){
if(result=max){
if(result==max){
count++;
System.out.print("1元=");
for(int i=0;icollect.size();i++){
System.out.print(money[(Integer) collect.get(i)]);
if(icollect.size()-1){
System.out.print("+");
}
}
System.out.println();
}
return;
}
for(int i=beginIndex;icents.length;i++){
int cent = cents[i];
collect.add(i);
collectMoney(cents, money,i, max, result+cent, collect);
collect.remove(Integer.valueOf(i));
}
}
}
因?yàn)槲覀冇玫亩际侵形沫h(huán)境(默認(rèn)),所以你的程序只能輸入中國(guó)的貨幣符號(hào),要通過(guò)Locale類的: public static void setDefault(Locale newLocale)方法設(shè)置下語(yǔ)言環(huán)境
具體代碼可參考如下的:
import java.util.Currency;
import java.util.Locale;
import java.util.Scanner;
/**
*
* @author top
*/
public class CurrencySymbol {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Please input a valid ISO 4217 currency code: ");
Scanner scan = new Scanner(System.in);
String code1 = scan.nextLine();
Locale.setDefault(Locale.CHINA);//中文語(yǔ)言環(huán)境下
Currency currency1 = Currency.getInstance(code1);
System.out.println(currency1.getSymbol());
String code2 = scan.nextLine();
Locale.setDefault(Locale.US);//美國(guó)
Currency currency2 = Currency.getInstance(code2);
System.out.println(currency2.getSymbol());
}
}