特点:在写程序时先写出程序成功时的测试代码,之后开始完善程序,根据测试代码所反馈来的信息,不断的改进程序,直到程序实现。
这样可以避免在写程序时出现“雪盲”,在不断的测试中,不断的审视自己的代码中,避免跑偏跑远,一步一步的接近实现。
实际应用中:
代码未动,测试现行
改错误
引发新的测试需求
再改错误
......
例:
/*
整数的人民币金额大写
1050 壹仟零伍拾
*/
class RMB
{
private static String R = "零壹贰叁肆伍陆柒捌玖";
private static String read1(int x)
{
return "" + R.charAt(x);
//return "壹"; // mock
}
private static String read4(int x)
{
int[] a = new int[4];
for(int i=0; i<a.length; i++){
a[i] = x % 10;
x /= 10;
}
String s = read1(a[3]) + "仟" + read1(a[2]) + "佰" + read1(a[1]) + "拾" + read1(a[0]);
s = s.replaceAll("零仟","零");
s = s.replaceAll("零佰","零");
s = s.replaceAll("零拾","零");
s = s.replaceAll("零零","零");
s = s.replaceAll("零零","零");
s = s.replaceAll("零零","零");
return s;
}
public static String read(int x)
{
final int W = 10000;
int a = x % W; //个
x /= W;
int b = x % W; // 万
x /= W;
int c = x % W; // 亿
String s = read4(c) + "亿" + read4(b) + "万" + read4(a);
}
}
public class A
{
public static void main(String[] args)
{
System.out.println(RMB.read(0));
System.out.println(RMB.read(1));
System.out.println(RMB.read(9));
System.out.println(RMB.read(19));
System.out.println(RMB.read(1050));
System.out.println(RMB.read(321050));
//System.out.println(RMB.read(1050)); //壹仟零伍拾
}
}