4.6.6 数组的应用举例
数组的用途是很广泛的,如果程序中有多个类型相同的变量,而且它们具有逻辑的整体性,则可以把它们定义成一个数组。
一维数组 朗读金钱
例如,在实际开发中的一个常用工具函数:需要将一个浮点数转换成人民币读法字符串,这个程序就需要使用数组。实现这个函数的思路是:
首先把这个浮点数分成整数部分和小数部分。
提取整数部分很容易,直接将这个浮点数强制类型转换成一个整数即可,这个整数就是浮点数的整数部分;
再使用浮点数减去整数将可以得到这个浮点数的小数部分。
然后分开处理整数部分和小数部分,其中小数部分的处理比较简单,直接截断到保留2位数字,转换成几角几分的字符串。整数部分的处理则稍微复杂一点,但只要认真分析不难发现,中国的数字习惯是4位一节的,一个4位的数字可被转成几千几百几十几,至于后面添加什么单位则不确定,如果这节4位数字出现在14位,则后面添加单位元;
如果这节4位数字出现在58位,则后面添加单位万;
如果这节4位数字出现在9~12位,则后面添加单位亿;
多于12位就暂不考虑了。
程序示例
因此实现这个程序的关键就是把一个4位数字字符串转换成一个中文读法。下面程序把这个需求实现了一部分。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import java.util.Arrays;
public class Num2Rmb { private String[] hanArr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; private String[] unitArr = { "十", "百", "千" };
private String[] divide(double num) { long zheng = (long) num; long xiao = Math.round((num - zheng) * 100); return new String[] { zheng + "", String.valueOf(xiao) }; }
private String toHanStr(String numStr) { String result = ""; int numLen = numStr.length(); for (int i = 0; i < numLen; i++) { int num = numStr.charAt(i) - 48; if (i != numLen - 1 && num != 0) { result += hanArr[num] + unitArr[numLen - 2 - i]; } else { result += hanArr[num]; } } return result; }
public static void main(String[] args) { Num2Rmb nr = new Num2Rmb(); System.out.println(Arrays.toString(nr.divide(236711125.123))); System.out.println(nr.toHanStr("609")); } }
|
运行上面程序,看到如下运行结果:
从上面程序的运行结果来看,初步实现了所需功能,但这个程序并不是这么简单,对零的处理比较复杂。例如,有两个零连在一起时该如何处理呢?如果最高位是零如何处理呢?最低位是零又如何处理呢?因此这个程序还需要继续完善,希望读者能把这个程序写完。
二维数组 实现 五子棋
除此之外,还可以利用二维数组来完成五子棋、连连看、俄罗斯方块、扫雷等常见小游戏。下面简单介绍利用二维数组实现五子棋。先定义一个二维数组作为下棋的棋盘,每当一个棋手下一步棋后,也就是为二维数组的一个数组元素赋值。下面程序完成了这个程序的初步功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| import java.io.*;
public class Gobang { private static int BOARD_SIZE = 15; private String[][] board;
public void initBoard() { board = new String[BOARD_SIZE][BOARD_SIZE]; for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = "╋"; } } }
public void printBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(board[i][j]); } System.out.print("\n"); } }
public static void main(String[] args) throws Exception { Gobang gb = new Gobang(); gb.initBoard(); gb.printBoard(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String inputStr = null; while ((inputStr = br.readLine()) != null) { String[] posStrArr = inputStr.split(","); int xPos = Integer.parseInt(posStrArr[0]); int yPos = Integer.parseInt(posStrArr[1]); gb.board[yPos - 1][xPos - 1] = "●";
gb.printBoard(); System.out.println("请输入您下棋的座标,应以x,y的格式:"); } } }
|
运行上面程序,将看到如下所示的界面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| G:\Desktop\随书源码\疯狂Java讲义(第4版)光盘\codes\04\4.6>java Gobang ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 5,5 ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 请输入您下棋的座标,应以x,y的格式: 5,6 ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 请输入您下棋的座标,应以x,y的格式:
|
从图4.16来看,程序上面显示的黑点一直是棋手下的棋电脑还没有下棋,电脑下棋可以使用随机生成两个坐标值来控制,当然也可以增加人工智能来控制下棋。
除此之外,读者还需要在这个程序的基础上进行完善,保证用户和电脑下的棋的坐标上不能已经有棋子(通过判断对应数组元素只能是”十”来确定),还需要进行4次循环扫描,判断横、竖、左斜、右斜是否有5个棋连在一起,从而判定胜负。