第四章的联系基本都是书中例子的加强,由于比较多我就用其作为一天的blog。不知不觉我已经写blog写了半个多月,加油~~~~呵呵
1.实现substring的方法,但要求按字节截取,而不是String的substring按字符截取。
- public class SubString {
- public static String getSubString(String str, int begin, int end) {
- byte[] c = str.getBytes();
- byte[] c2 = new byte[end - begin + 1];
- int j = 0;
- for (int i = begin; i < end + 1; i++) {
- c2[j] = c[i];
- j++;
- }
- String sub=new String(c2);
- return sub;
- }
- public static void main(String[] args) {
- System.out.println(getSubString("中国abc", 2, 4));
- }
- }
这是按字节截取的字符串,由于中文占2个字节,如上例你截取1到4会出现乱码的文字。
2.浮点数人民币读法eg.1006.333
- public static String[] divide(double num) {
- //将一个浮点数强制类型转换为long,即得到它的整数部分
- long zheng = (long) num;
- //浮点数减去整数部分,得到小数部分,小数部分乘以100后再取整得到2位小数
- long xiao = Math.round((num - zheng) * 100);
- //下面用了2种方法把整数转换为字符串
- String xiaostr;
- if (xiao > 10) {
- xiaostr = String.valueOf(xiao);
- } else {
- xiaostr = "0" + String.valueOf(xiao);
- }
- return new String[]{zheng + "", xiaostr};
- }
- private String toHanStr(double inputNum) {
- String[] numStrs = divide(inputNum);
- String numStr1 = numStrs[0];
- String result = "";
- int numLen1 = numStr1.length();
- int oldNum = -1;
- //依次遍历数字字符串的每一位数字
- for (int i = 0; i < numLen1; i++) {
- //把char型数字转换成的int型数字,因为它们的ASCII码值恰好相差48
- //因此把char型数字减去48得到int型数字,例如'4'被转换成4。
- int num = numStr1.charAt(i) - 48;
- //如果不是最后一位数字,而且数字不是零,则需要添加单位(千、百、十)
- if (i != numLen1 - 1 && num != 0) {
- result += hanArr[num] + unitArr[numLen1 - 2 - i];
- }
- //否则不要添加单位
- else {
- if (oldNum == 0) {
- result += hanArr[num];
- }
- oldNum = num;
- }
- }
- result += "元";
- String numStr2 = numStrs[1];
- int numLen2 = numStr2.length();
- for (int i = 0; i < numLen2; i++) {
- int num = numStr2.charAt(i) - 48;
- if (i != numLen2 && num != 0) {
- result += hanArr[num] + unitArr2[i];
- }
- }
- return result;
- }
- public static void main(String[] args) {
- Num2RmbPlus nr = new Num2RmbPlus();
- System.out.println(nr.toHanStr(01006.303));
- }
- }
这题要细心注意各种细节,数字读起来更口语话。
3.编写控制台五子棋
电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组。
还涉及
1.座标的有效性,只能是数字,不能超出棋盘范围
2.如果下的棋的点,不能重复下棋。
3.每次下棋后,需要扫描谁赢了
- public class GobangPlus {
- //定义一个二维数组来充当棋盘
- private String[][] board;
- //定义棋盘的大小
- private static int BOARD_SIZE = 15;
- 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[j][i]);
- }
- //每打印完一行数组元素后输出一个换行符
- System.out.print("/n");
- }
- }
- public String checkWin(int x, int y, String color) {
- //横
- int x1;
- int x2;
- int y1;
- int y2;
- if (x - 4 > 0) {
- x1 = x - 4;
- } else {
- x1 = 0;
- }
- if (x + 4 < 15) {
- x2 = x + 4;
- } else {
- x2 = 14;
- }
- if (y - 4 > 0) {
- y1 = y - 4;
- } else {
- y1 = 0;
- }
- if (y + 4 < 15) {
- y2 = y + 4;
- } else {
- y2 = 14;
- }
- //横
- int result = 0;
- for (int i = x1; i <= x2; i++) {
- if (result != 5) {
- if (i < 0 || i > 15 || y < 0 || y > 15) {
- System.out.println("error");
- }
- if (board[i][y] == color) {
- result++;
- } else {
- result = 0;
- }
- } else {
- return color + " Win!!";
- }
- }
- if (result == 5) {
- return color + " Win!!";
- }
- //竖
- result = 0;
- for (int i = y1; i <= y2; i++) {
- if (result != 5) {
- if (x < 0 || x > 15 || i < 0 || i > 15) {
- System.out.println("error");
- }
- if (board[x][i] == color) {
- result++;
- } else {
- result = 0;
- }
- } else {
- return color + " Win!!";
- }
- }
- if (result == 5) {
- return color + " Win!!";
- }
- //向左斜
- result = 0;
- int moveX = 0;
- int moveY = 0;
- if (x - x1 >= y - y1) {
- moveX = y - y1;
- } else {
- moveX = x - x1;
- }
- if (x2 - x >= y2 - y) {
- moveY = y2 - y;
- } else {
- moveY = x2 - x;
- }
- for (int i = -moveX; i <= moveY; i++) {
- if (result != 5) {
- if (x + i < 0 || x + i > 15 || y + i < 0 || y + i > 15) {
- System.out.println("error");
- }
- if (board[x + i][y + i] == color) {
- result++;
- } else {
- result = 0;
- }
- } else {
- return color + " Win!!";
- }
- }
- if (result == 5) {
- return color + " Win!!";
- }
- //向右斜
- result = 0;
- int moveX2 = 0;
- int moveY2 = 0;
- if (x - x1 >= y2 - y) {
- moveX2 = y2 - y;
- } else {
- moveX2 = x - x1;
- }
- if (x2 - x >= y - y1) {
- moveY2 = y - y1;
- } else {
- moveY2 = x2 - x;
- }
- for (int i = -moveX2; i <= moveY2; i++) {
- if (result != 5) {
- if (x + i < 0 || x + i > 15 || y - i < 0 || y - i > 15) {
- System.out.println("error");
- }
- if (board[x + i][y - i] == color) {
- result++;
- } else {
- result = 0;
- }
- } else {
- return color + " Win!!";
- }
- }
- if (result == 5) {
- return color + " Win!!";
- }
- return null;
- }
- public static void main(String[] args) throws Exception {
- GobangPlus gb = new GobangPlus();
- gb.initBoard();
- gb.printBoard();
- System.out.println("请输入您下棋的座标,应以x,y的格式:");
- //这是用于获取键盘输入的方法
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String inputStr = null;
- //br.readLine():每当在键盘上输入一行内容按回车,刚输入的内容将被br读取到。
- while ((inputStr = br.readLine()) != null) {
- //将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串
- String[] posStrArr = inputStr.split(",");
- //将2个字符串转换成用户下棋的座标
- int xPos = Integer.parseInt(posStrArr[0]);
- int yPos = Integer.parseInt(posStrArr[1]);
- //把对应的数组元素赋为"●"。
- gb.board[xPos - 1][yPos - 1] = "●";
- String win = gb.checkWin(xPos - 1, yPos - 1, "●");//每次下棋后,需要扫描谁赢了
- if (win != null) {
- gb.printBoard();
- System.out.println(win);
- System.exit(0);
- }
- int aiX;
- int aiY;
- while (true) {
- Random r1 = new Random();
- aiX = r1.nextInt(BOARD_SIZE - 1);//座标的有效性,只能是数字,不能超出棋盘范围
- Random r2 = new Random();
- aiY = r2.nextInt(BOARD_SIZE - 1);
- if (gb.board[aiX][aiY] == "╋") { //如果下的棋的点,不能重复下棋。
- gb.board[aiX][aiY] = "○";
- break;
- }
- }
- win = gb.checkWin(aiX, aiY, "○"); //每次下棋后,需要扫描谁赢了
- if (win != null) {
- gb.printBoard();
- System.out.println(win);
- System.exit(0);
- }
- gb.printBoard();
- System.out.println("请输入您下棋的座标,应以x,y的格式:");
- }
- }
- }
- 这道题看看那么简单,我还搞了老半天,主要是由于checkWin中的if (result != 5) 这个条件没注意到一种特殊情况,大家可以去想想,还有什么好方法大家可以交流。