黑马程序员技术交流社区

标题: 几道编程题分享 [打印本页]

作者: liujian5461267    时间: 2016-5-8 12:54
标题: 几道编程题分享

  1. public class shouPiao {

  2.   /**
  3.    * 6、 编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、
  4.    * 售票中心。
  5.    * 售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟
  6.    * 此售票过程。
  7.    *
  8.    * @author 汤柳清
  9.    */
  10.   public static void main(String[] args) {
  11.           shouPiao t = new shouPiao();
  12.     t.new Ticket();

  13.   }

  14.   class Ticket {
  15.     public Ticket() {
  16.       TicketSealCenter tsc = new TicketSealCenter(100);// 定义有100张票
  17.       for (int i = 0; i < 5; i++) {// 定义有5个窗口
  18.         new Thread(new SealWindow(i, tsc)).start();// 启动售票窗口售票
  19.       }
  20.     }
  21.   }

  22.   /**
  23.    * 售票中心类 定义了票的总数,同步售票方法
  24.    */
  25.   class TicketSealCenter {
  26.     int ticketNum = 50;
  27.     boolean flag = false; // 定义票是否卖完

  28.     public TicketSealCenter(int num) {// 定义一个改变票数的方法
  29.       this.ticketNum = num;
  30.     }

  31.     public synchronized void sellTicket(SealWindow s) {
  32.       if (ticketNum > 0) {//票数如果大于0
  33.         int n = s.num + 1;//n表示第几号窗口
  34.         System.out
  35.             .println("第--" + n + "--售票窗口卖出了第" + ticketNum + "张票!");
  36.         ticketNum--;//卖出一张票后减1
  37.       } else {
  38.         flag = true;
  39.       }
  40.     }
  41.   }

  42.   /**
  43.    * 售票窗口类
  44.    */
  45.   class SealWindow implements Runnable {
  46.     int num;//num表示第几号窗口-1,即i
  47.     TicketSealCenter tsc;

  48.     public SealWindow(int num, TicketSealCenter tsc) {
  49.       this.num = num;
  50.       this.tsc = tsc;
  51.     }

  52.     public final void run() {
  53.       while (!tsc.flag) {
  54.         tsc.sellTicket(this); // 调用售票中心类的同步票数
  55.         try {
  56.           Thread.sleep(100);
  57.         } catch (InterruptedException e) {
  58.           e.printStackTrace();
  59.         }
  60.       }
  61.     }
  62.   }
  63. }
复制代码

作者: liujian5461267    时间: 2016-5-8 12:55

  1. /*
  2. * 2、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。
  3. * 这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。
  4. 提示:十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2 ,这次得到的余数就是次低位,
  5. 如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),
  6. 就很容易理解十进制数转二进制数的这种方式。

  7. */
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;

  11. public class ZiFuZhuanHuan {
  12.         public static void main(String[] args) throws IOException {
  13.                 System.out.println(Integer.MAX_VALUE);
  14.                 System.out.println(Integer.MIN_VALUE);
  15.                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  16.                 String line = in.readLine();
  17.                 if (checkDigital(line))
  18.                         throw new RuntimeException("转换错误,含有非数字字符");
  19.                 try {
  20.                         int value = Integer.parseInt(line);
  21.                         String binary = toBinary(value);
  22.                         System.out.println("right :      :" + Integer.toBinaryString(value));
  23.                         System.out.println("my result :" + binary);
  24.                 } catch (Exception e) {
  25.                         throw new RuntimeException("转换错误,数字过大");
  26.                 }
  27.         }

  28.         private static String toBinary(int value) {
  29.                 StringBuilder sb = new StringBuilder();
  30.                 int y = 0;
  31.                 while (value != 0) {
  32.                         y = value % 2;
  33.                         value = value / 2;
  34.                         sb.insert(0, y);
  35.                 }
  36.                 return sb.toString();
  37.         }

  38.         private static boolean checkDigital(String line) {
  39.                 char[] cs = line.toCharArray();
  40.                 for (char c : cs) {
  41.                         if (!Character.isDigit(c)) {
  42.                                 return true;
  43.                         }
  44.                 }
  45.                 return false;
  46.         }
  47. }
复制代码

作者: liujian5461267    时间: 2016-5-8 12:57
  1. package com.test20;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;

  4. import java.io.FileInputStream;

  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.OutputStream;

  8. /*
  9. * 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
  10. *
  11. * 字节流四种方式复制文件:
  12. * 基本字节流一次读写一个字节:        共耗时:28375毫秒
  13. * 基本字节流一次读写一个字节数组: 共耗时:47毫秒
  14. * 高效字节流一次读写一个字节: 共耗时:422毫秒
  15. * 高效字节流一次读写一个字节数组: 共耗时:47毫秒
  16. */
  17. public class copyMp4Damo {
  18.         public static void main(String[] args) throws Exception {
  19.                 long start = System.currentTimeMillis();
  20.                 method4("c:\\test\\aa.BMP", "D:\\test\\aaa.BMP");
  21.                 long end = System.currentTimeMillis();
  22.                 long time = end - start;
  23.                 System.out.println(time);
  24.         }

  25.         // 基本字节流一次读写一个字节
  26.         private static void method1(String string, String string2) throws IOException {

  27.                 FileInputStream fis = new FileInputStream(string);
  28.                 FileOutputStream fos = new FileOutputStream(string2);
  29.                 int i = 0;
  30.                 while ((i = fis.read()) != -1) {
  31.                         fos.write(i);
  32.                 }
  33.                 fos.close();
  34.                 fis.close();
  35.         }

  36.         // 基本字节流一次读写一个字节数组
  37.         private static void method2(String string, String string2) throws IOException {
  38.                 FileInputStream fis = new FileInputStream(string);
  39.                 FileOutputStream fos = new FileOutputStream(string2);
  40.                 byte[] bt = new byte[1024];
  41.                 int ii = 0;
  42.                 while ((ii = fis.read(bt)) != -1) {
  43.                         fos.write(bt, 0, ii);
  44.                 }
  45.                 fis.close();
  46.                 fos.close();
  47.         }

  48.         // 高效字节流一次读写一个字节
  49.         private static void method3(String string, String string2) throws IOException {
  50.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(string));
  51.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(string2));
  52.                 int a = 0;
  53.                 while ((a = bis.read()) != -1) {
  54.                         bos.write(a);
  55.                 }
  56.                 bis.close();
  57.                 bos.close();

  58.         }

  59.         // 高效字节流一次读写一个字节数组
  60.         private static void method4(String string, String string2) throws IOException {
  61.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(string));
  62.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(string2));
  63.                 int a = 0;
  64.                 byte[] b = new byte[1024];
  65.                 while ((a = bis.read(b)) != -1) {
  66.                         bos.write(b, 0, a);
  67.                 }
  68.                 bis.close();
  69.                 bos.close();

  70.         }

  71. }
复制代码

作者: liujian5461267    时间: 2016-5-8 12:59
  1. package com.test20;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;

  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. //文本复制
  9. public class fileReader {
  10.         public static void main(String[] args) throws Exception {

  11.                 long start = System.currentTimeMillis();
  12.                 method1("c:\\test\\heima.doc", "D:\\test\\c.doc");
  13.                 long end = System.currentTimeMillis();
  14.                 long time = end - start;
  15.                 System.out.println(time);
  16.         }

  17.         // 字符缓冲流一次读取一个 字符串 15毫秒
  18.         // 如何实现数据的追加写入?
  19.         // 用构造方法带第二个参数是true的情况即可
  20.         private static void method1(String s1, String s2) throws IOException {
  21.                 BufferedReader br = new BufferedReader(new FileReader(s1));
  22.                 // BufferedReader br = new BufferedReader(new StringReader(s1));
  23.                 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(s2, true)));
  24.                 int linecount = 0;
  25.                 String s;
  26.                 while ((s = br.readLine()) != null) {
  27.                         pw.println(linecount++ + ": " + s);
  28.                 }
  29.                 pw.close();

  30.         }

  31.         // 字符缓冲流一次读取一个 字符串 15毫秒
  32.         private static void method2(String s1, String s2) throws IOException {
  33.                 BufferedReader br = new BufferedReader(new FileReader(s1));
  34.                 BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
  35.                 // PrintWriter pw = new PrintWriter((new FileWriter(s2,true)));
  36.                 int linecount = 0;
  37.                 String s;
  38.                 while ((s = br.readLine()) != null) {
  39.                         // pw.println(linecount++ +": "+s);//一句顶三句
  40.                         bw.write(linecount++ + ": " + s);
  41.                         bw.newLine();
  42.                         bw.flush();

  43.                 }
  44.                 bw.close();
  45.                 br.close();
  46.                 // pw.close();

  47.         }

  48.         // 字符缓冲流一次读取一个 字符数组 16毫秒
  49.         private static void method3(String s1, String s2) throws IOException {
  50.                 BufferedReader br = new BufferedReader(new FileReader(s1));
  51.                 BufferedWriter bw = new BufferedWriter(new FileWriter(s2));

  52.                 char[] char1 = new char[1024];
  53.                 int s = 0;
  54.                 while ((s = br.read(char1)) != -1) {
  55.                         bw.write(char1, 0, s);
  56.                 }
  57.                 br.close();
  58.                 bw.close();

  59.         }

  60.         // 字符缓冲流一次读取一个 字符 15毫秒
  61.         private static void method4(String s1, String s2) throws IOException {
  62.                 BufferedReader br = new BufferedReader(new FileReader(s1));
  63.                 BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
  64.                 int s = 0;
  65.                 while ((s = br.read()) != -1) {
  66.                         bw.write(s);
  67.                 }
  68.                 br.close();
  69.                 bw.close();

  70.         }

  71.         // 基本字符流流一次读取一个 字符数组 15毫秒
  72.         private static void method5(String s1, String s2) throws IOException {
  73.                 FileReader fr = new FileReader(s1);
  74.                 FileWriter fw = new FileWriter(s2);

  75.                 char[] char1 = new char[1024];
  76.                 int s = 0;
  77.                 while ((s = fr.read(char1)) != -1) {
  78.                         fw.write(char1, 0, s);
  79.                 }
  80.                 fr.close();
  81.                 fw.close();

  82.         }

  83.         // 基本字符流流一次读取一个 字符 31毫秒
  84.         private static void method6(String s1, String s2) throws IOException {
  85.                 FileReader fr = new FileReader(s1);
  86.                 FileWriter fw = new FileWriter(s2);

  87.                 int s = 0;
  88.                 while ((s = fr.read()) != -1) {
  89.                         fw.write(s);
  90.                 }
  91.                 fr.close();
  92.                 fw.close();

  93.         }

  94. }
复制代码

作者: 320215787    时间: 2016-5-8 14:55
真的不错呢
作者: 代码人生?!    时间: 2016-5-8 16:15
这是多线程学的???
作者: johnli    时间: 2016-5-8 16:19
点招题吗?
作者: zhxz521    时间: 2016-5-8 16:56
还搞不懂,没学那么多....
作者: tylw123    时间: 2016-5-8 18:51
谢谢楼主,1024
作者: believedream    时间: 2016-5-8 20:07
感觉这几个例子都不错,对于初学者练手很好,收藏了,32个赞
作者: sunchuan    时间: 2016-5-8 20:24
顶贴是一种美德
作者: 冯领峰    时间: 2016-5-8 20:25
表示没怎么看懂




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2