今天看了算法的公開課,有一個把數字轉換成人民幣大寫的算法。
後面就想着自己寫一個界面的,好幾天沒有寫一個完整的程序的。
當然有一些BUG,沒有時間優化,後面在慢慢優化。
自己的名言:每天努力一點兒,總會有回報的。
package com.langxikeji.Demo;
import java.awt.Button;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class DigitFormat {
// 主頁面
public static Frame frame = new Frame("人民幣大寫格式轉換器");
// 放按鈕的面板
public static Panel panel = new Panel();
// 字體的大小
public static Font font = new Font("黑體", Font.BOLD, 30);
//文本提示
public static Label in_label = new Label("請輸入待轉換數字:");
public static Label out_label = new Label("轉換後的數字爲:");
// 輸入文本框
public static TextField in_tf = new TextField();
// 結果文本框
public static TextField out_tf = new TextField();
// 轉換按鈕
public static Button button = new Button("轉換");
// 零壹貳叄肆伍陸柒捌玖 數字模板
public static String CAPITAL = "零壹貳叄肆伍陸柒捌玖";
// 四位數計算進位
public static int WAN_YUAN = 10000;
// 一位數的情況
public static String read_One(int x) {
return "" + CAPITAL.charAt(x);
}
// 四位數的情況
public static String read_Four(int x) {
int[] nums = new int[4];
for (int i = 0; i < nums.length; i++) {
nums[i] = x % 10;
x /= 10;
}
String s = read_One(nums[3]) + "仟" + read_One(nums[2]) + "佰"
+ read_One(nums[1]) + "拾" + read_One(nums[0]);
s = s.replaceAll("零仟", "");
s = s.replaceAll("零佰", "");
s = s.replaceAll("零零", "");
if (s.endsWith("零") && s.length() > 1) {
s = s.substring(0, s.length() - 1);
}
return s;
}
public static String read(long x) {
long a = x % WAN_YUAN;// 個位
x /= WAN_YUAN;
long b = x % WAN_YUAN;// 萬位
long c = x / WAN_YUAN;// 億位
String s = read_Four((int) c) + "億" + read_Four((int) b) + "萬"
+ read_Four((int) a);
s = s.replace("零億", "");
s = s.replace("零萬", "");
s = s.replace("零零", "");
s = s.replace("零拾", "");
s = s.replace("億萬", "");
if (s.startsWith("億")) {
s = s.substring(1, s.length() - 1);
}
if (s.endsWith("零") && s.length() > 1) {
s = s.substring(0, s.length() - 1);
}
return s;
}
public static void init() {
in_tf.setText("0");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseClicked(e);
String x = in_tf.getText();
x = read(Long.parseLong(x));
out_tf.setText(x);
}
});
}
public static void main(String[] args) {
// 窗體大小
frame.setBounds(100, 100, 600, 500);
// 採用絕對定位
frame.setLayout(null);
in_label.setFont(font);
in_label.setBounds(50, 60, 260, 140);
out_label.setFont(font);
out_label.setBounds(50, 200, 260, 140);
in_tf.setFont(font);
in_tf.setBounds(330, 200, 200, 40);
out_tf.setFont(font);
out_tf.setBounds(30, 340, 550, 40);
button.setFont(font);
button.setBounds(400, 400, 80, 80);
frame.add(in_label);
frame.add(out_label);
frame.add(in_tf);
frame.add(out_tf);
frame.add(button);
init();
// panel.add(button);
// panel.add(tf);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
frame.dispose();
}
});
frame.setVisible(true);
frame.setResizable(false);
}
}