疯狂JAVA讲义---第四章(下):数组

第四章的联系基本都是书中例子的加强,由于比较多我就用其作为一天的blog。不知不觉我已经写blog写了半个多月,加油~~~~呵呵

1.实现substring的方法,但要求按字节截取,而不是String的substring按字符截取。

  1. public class SubString {
  2.     public static String getSubString(String str, int begin, int end) {
  3.         byte[] c = str.getBytes();
  4.         byte[] c2 = new byte[end - begin + 1];
  5.         int j = 0;
  6.         for (int i = begin; i < end + 1; i++) {
  7.             c2[j] = c[i];
  8.             j++;
  9.         }
  10.        String sub=new String(c2);
  11.         return sub;
  12.     }
  13.     public static void main(String[] args) {
  14.         System.out.println(getSubString("中国abc"24));
  15.     }
  16. }

这是按字节截取的字符串,由于中文占2个字节,如上例你截取1到4会出现乱码的文字。

2.浮点数人民币读法eg.1006.333

  1.     public static String[] divide(double num) {
  2.         //将一个浮点数强制类型转换为long,即得到它的整数部分
  3.         long zheng = (long) num;
  4.         //浮点数减去整数部分,得到小数部分,小数部分乘以100后再取整得到2位小数
  5.         long xiao = Math.round((num - zheng) * 100);
  6.         //下面用了2种方法把整数转换为字符串
  7.         String xiaostr;
  8.         if (xiao > 10) {
  9.             xiaostr = String.valueOf(xiao);
  10.         } else {
  11.             xiaostr = "0" + String.valueOf(xiao);
  12.         }
  13.         return new String[]{zheng + "", xiaostr};
  14.     }
  15.     private String toHanStr(double inputNum) {
  16.         String[] numStrs = divide(inputNum);
  17.         String numStr1 = numStrs[0];
  18.         String result = "";
  19.         int numLen1 = numStr1.length();
  20.         int oldNum = -1;
  21.         //依次遍历数字字符串的每一位数字
  22.         for (int i = 0; i < numLen1; i++) {
  23.             //把char型数字转换成的int型数字,因为它们的ASCII码值恰好相差48
  24.             //因此把char型数字减去48得到int型数字,例如'4'被转换成4。
  25.             int num = numStr1.charAt(i) - 48;
  26.             //如果不是最后一位数字,而且数字不是零,则需要添加单位(千、百、十)
  27.             if (i != numLen1 - 1 && num != 0) {
  28.                 result += hanArr[num] + unitArr[numLen1 - 2 - i];
  29.             }
  30.             //否则不要添加单位
  31.             else {
  32.                 if (oldNum == 0) {
  33.                     result += hanArr[num];
  34.                 }
  35.                 oldNum = num;
  36.             }
  37.         }
  38.         result += "元";
  39.         String numStr2 = numStrs[1];
  40.         int numLen2 = numStr2.length();
  41.         for (int i = 0; i < numLen2; i++) {
  42.             int num = numStr2.charAt(i) - 48;
  43.             if (i != numLen2 && num != 0) {
  44.                 result += hanArr[num] + unitArr2[i];
  45.             }
  46.         }
  47.         return result;
  48.     }
  49.     public static void main(String[] args) {
  50.         Num2RmbPlus nr = new Num2RmbPlus();
  51.         System.out.println(nr.toHanStr(01006.303));
  52.     }
  53. }

这题要细心注意各种细节,数字读起来更口语话。

3.编写控制台五子棋

  电脑随机生成2个整数,作为电脑下棋的座标,赋给board数组。
                还涉及
                   1.座标的有效性,只能是数字,不能超出棋盘范围
                   2.如果下的棋的点,不能重复下棋。
                   3.每次下棋后,需要扫描谁赢了

  1. public class GobangPlus {
  2.     //定义一个二维数组来充当棋盘
  3.     private String[][] board;
  4.     //定义棋盘的大小
  5.     private static int BOARD_SIZE = 15;
  6.     public void initBoard() {
  7.         //初始化棋盘数组
  8.         board = new String[BOARD_SIZE][BOARD_SIZE];
  9.         //把每个元素赋为"╋",用于在控制台画出棋盘
  10.         for (int i = 0; i < BOARD_SIZE; i++) {
  11.             for (int j = 0; j < BOARD_SIZE; j++) {
  12.                 board[i][j] = "╋";
  13.             }
  14.         }
  15.     }
  16.     //在控制台输出棋盘的方法
  17.     public void printBoard() {
  18.         //打印每个数组元素
  19.         for (int i = 0; i < BOARD_SIZE; i++) {
  20.             for (int j = 0; j < BOARD_SIZE; j++) {
  21.                 //打印数组元素后不换行
  22.                 System.out.print(board[j][i]);
  23.             }
  24.             //每打印完一行数组元素后输出一个换行符
  25.             System.out.print("/n");
  26.         }
  27.     }
  28.     public String checkWin(int x, int y, String color) {
  29.         //横
  30.         int x1;
  31.         int x2;
  32.         int y1;
  33.         int y2;
  34.         if (x - 4 > 0) {
  35.             x1 = x - 4;
  36.         } else {
  37.             x1 = 0;
  38.         }
  39.         if (x + 4 < 15) {
  40.             x2 = x + 4;
  41.         } else {
  42.             x2 = 14;
  43.         }
  44.         if (y - 4 > 0) {
  45.             y1 = y - 4;
  46.         } else {
  47.             y1 = 0;
  48.         }
  49.         if (y + 4 < 15) {
  50.             y2 = y + 4;
  51.         } else {
  52.             y2 = 14;
  53.         }
  54.         //横
  55.         int result = 0;
  56.         for (int i = x1; i <= x2; i++) {
  57.             if (result != 5) {
  58.                 if (i < 0 || i > 15 || y < 0 || y > 15) {
  59.                     System.out.println("error");
  60.                 }
  61.                 if (board[i][y] == color) {
  62.                     result++;
  63.                 } else {
  64.                     result = 0;
  65.                 }
  66.             } else {
  67.                 return color + " Win!!";
  68.             }
  69.         }
  70.           if (result == 5) {
  71.               return color + " Win!!";
  72.          }
  73.         //竖
  74.         result = 0;
  75.         for (int i = y1; i <= y2; i++) {
  76.             if (result != 5) {
  77.                 if (x < 0 || x > 15 || i < 0 || i > 15) {
  78.                     System.out.println("error");
  79.                 }
  80.                 if (board[x][i] == color) {
  81.                     result++;
  82.                 } else {
  83.                     result = 0;
  84.                 }
  85.             } else {
  86.                 return color + " Win!!";
  87.             }
  88.         }
  89.           if (result == 5) {
  90.               return color + " Win!!";
  91.          }
  92.         //向左斜
  93.         result = 0;
  94.         int moveX = 0;
  95.         int moveY = 0;
  96.         if (x - x1 >= y - y1) {
  97.             moveX = y - y1;
  98.         } else {
  99.             moveX = x - x1;
  100.         }
  101.         if (x2 - x >= y2 - y) {
  102.             moveY = y2 - y;
  103.         } else {
  104.             moveY = x2 - x;
  105.         }
  106.         for (int i = -moveX; i <= moveY; i++) {
  107.             if (result != 5) {
  108.                 if (x + i < 0 || x + i > 15 || y + i < 0 || y + i > 15) {
  109.                     System.out.println("error");
  110.                 }
  111.                 if (board[x + i][y + i] == color) {
  112.                     result++;
  113.                 } else {
  114.                     result = 0;
  115.                 }
  116.             } else {
  117.                 return color + " Win!!";
  118.             }
  119.         }
  120.          if (result == 5) {
  121.               return color + " Win!!";
  122.          }
  123.         //向右斜
  124.         result = 0;
  125.         int moveX2 = 0;
  126.         int moveY2 = 0;
  127.         if (x - x1 >= y2 - y) {
  128.             moveX2 = y2 - y;
  129.         } else {
  130.             moveX2 = x - x1;
  131.         }
  132.         if (x2 - x >= y - y1) {
  133.             moveY2 = y - y1;
  134.         } else {
  135.          moveY2 = x2 - x;
  136.         }
  137.        for (int i = -moveX2; i <= moveY2; i++) {
  138.             if (result != 5) {
  139.                 if (x + i < 0 || x + i > 15 || y - i < 0 || y - i > 15) {
  140.                     System.out.println("error");
  141.                 }
  142.                 if (board[x + i][y - i] == color) {
  143.                     result++;
  144.                 } else {
  145.                     result = 0;
  146.                 }
  147.             } else {
  148.                 return color + " Win!!";
  149.             }
  150.         }
  151.          if (result == 5) {
  152.               return color + " Win!!";
  153.          }
  154.         return null;
  155.     }
  156.     public static void main(String[] args) throws Exception {
  157.         GobangPlus gb = new GobangPlus();
  158.         gb.initBoard();
  159.         gb.printBoard();
  160.         System.out.println("请输入您下棋的座标,应以x,y的格式:");
  161.         //这是用于获取键盘输入的方法
  162.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  163.         String inputStr = null;
  164.         //br.readLine():每当在键盘上输入一行内容按回车,刚输入的内容将被br读取到。
  165.         while ((inputStr = br.readLine()) != null) {
  166.             //将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串
  167.             String[] posStrArr = inputStr.split(",");
  168.             //将2个字符串转换成用户下棋的座标
  169.             int xPos = Integer.parseInt(posStrArr[0]);
  170.             int yPos = Integer.parseInt(posStrArr[1]);
  171.             //把对应的数组元素赋为"●"。
  172.             gb.board[xPos - 1][yPos - 1] = "●";
  173.             String win = gb.checkWin(xPos - 1, yPos - 1"●");//每次下棋后,需要扫描谁赢了
  174.             if (win != null) {
  175.                 gb.printBoard();
  176.                 System.out.println(win);
  177.                 System.exit(0);
  178.             }
  179.             int aiX;
  180.             int aiY;
  181.             while (true) {
  182.                 Random r1 = new Random();
  183.                 aiX = r1.nextInt(BOARD_SIZE - 1);//座标的有效性,只能是数字,不能超出棋盘范围
  184.                 Random r2 = new Random();
  185.                 aiY = r2.nextInt(BOARD_SIZE - 1);
  186.                 if (gb.board[aiX][aiY] == "╋") { //如果下的棋的点,不能重复下棋。
  187.                     gb.board[aiX][aiY] = "○";
  188.                     break;
  189.                 }
  190.             }
  191.             win = gb.checkWin(aiX, aiY, "○"); //每次下棋后,需要扫描谁赢了
  192.             if (win != null) {
  193.                 gb.printBoard();
  194.                 System.out.println(win);
  195.                 System.exit(0);
  196.             }
  197.             gb.printBoard();
  198.             System.out.println("请输入您下棋的座标,应以x,y的格式:");
  199.         }
  200.     }
  201. }
  202. 这道题看看那么简单,我还搞了老半天,主要是由于checkWin中的if (result != 5) 这个条件没注意到一种特殊情况,大家可以去想想,还有什么好方法大家可以交流。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值