黑马程序员技术交流社区
标题:
几道编程题分享
[打印本页]
作者:
liujian5461267
时间:
2016-5-8 12:54
标题:
几道编程题分享
public class shouPiao {
/**
* 6、 编写三各类Ticket、SealWindow、TicketSealCenter分别代表票信息、售票窗口、
* 售票中心。
* 售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟
* 此售票过程。
*
* @author 汤柳清
*/
public static void main(String[] args) {
shouPiao t = new shouPiao();
t.new Ticket();
}
class Ticket {
public Ticket() {
TicketSealCenter tsc = new TicketSealCenter(100);// 定义有100张票
for (int i = 0; i < 5; i++) {// 定义有5个窗口
new Thread(new SealWindow(i, tsc)).start();// 启动售票窗口售票
}
}
}
/**
* 售票中心类 定义了票的总数,同步售票方法
*/
class TicketSealCenter {
int ticketNum = 50;
boolean flag = false; // 定义票是否卖完
public TicketSealCenter(int num) {// 定义一个改变票数的方法
this.ticketNum = num;
}
public synchronized void sellTicket(SealWindow s) {
if (ticketNum > 0) {//票数如果大于0
int n = s.num + 1;//n表示第几号窗口
System.out
.println("第--" + n + "--售票窗口卖出了第" + ticketNum + "张票!");
ticketNum--;//卖出一张票后减1
} else {
flag = true;
}
}
}
/**
* 售票窗口类
*/
class SealWindow implements Runnable {
int num;//num表示第几号窗口-1,即i
TicketSealCenter tsc;
public SealWindow(int num, TicketSealCenter tsc) {
this.num = num;
this.tsc = tsc;
}
public final void run() {
while (!tsc.flag) {
tsc.sellTicket(this); // 调用售票中心类的同步票数
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
作者:
liujian5461267
时间:
2016-5-8 12:55
/*
* 2、编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数,然后打印出这个十进制整数对应的二进制形式。
* 这个程序要考虑输入的字符串不能转换成一个十进制整数的情况,并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。
提示:十进制数转二进制数的方式是用这个数除以2,余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2 ,这次得到的余数就是次低位,
如此循环,直到被除数为0为止。其实,只要明白了打印出一个十进制数的每一位的方式(不断除以10,得到的余数就分别是个位,十位,百位),
就很容易理解十进制数转二进制数的这种方式。
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ZiFuZhuanHuan {
public static void main(String[] args) throws IOException {
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = in.readLine();
if (checkDigital(line))
throw new RuntimeException("转换错误,含有非数字字符");
try {
int value = Integer.parseInt(line);
String binary = toBinary(value);
System.out.println("right : :" + Integer.toBinaryString(value));
System.out.println("my result :" + binary);
} catch (Exception e) {
throw new RuntimeException("转换错误,数字过大");
}
}
private static String toBinary(int value) {
StringBuilder sb = new StringBuilder();
int y = 0;
while (value != 0) {
y = value % 2;
value = value / 2;
sb.insert(0, y);
}
return sb.toString();
}
private static boolean checkDigital(String line) {
char[] cs = line.toCharArray();
for (char c : cs) {
if (!Character.isDigit(c)) {
return true;
}
}
return false;
}
}
复制代码
作者:
liujian5461267
时间:
2016-5-8 12:57
package com.test20;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/*
* 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中
*
* 字节流四种方式复制文件:
* 基本字节流一次读写一个字节: 共耗时:28375毫秒
* 基本字节流一次读写一个字节数组: 共耗时:47毫秒
* 高效字节流一次读写一个字节: 共耗时:422毫秒
* 高效字节流一次读写一个字节数组: 共耗时:47毫秒
*/
public class copyMp4Damo {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
method4("c:\\test\\aa.BMP", "D:\\test\\aaa.BMP");
long end = System.currentTimeMillis();
long time = end - start;
System.out.println(time);
}
// 基本字节流一次读写一个字节
private static void method1(String string, String string2) throws IOException {
FileInputStream fis = new FileInputStream(string);
FileOutputStream fos = new FileOutputStream(string2);
int i = 0;
while ((i = fis.read()) != -1) {
fos.write(i);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节数组
private static void method2(String string, String string2) throws IOException {
FileInputStream fis = new FileInputStream(string);
FileOutputStream fos = new FileOutputStream(string2);
byte[] bt = new byte[1024];
int ii = 0;
while ((ii = fis.read(bt)) != -1) {
fos.write(bt, 0, ii);
}
fis.close();
fos.close();
}
// 高效字节流一次读写一个字节
private static void method3(String string, String string2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(string));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(string2));
int a = 0;
while ((a = bis.read()) != -1) {
bos.write(a);
}
bis.close();
bos.close();
}
// 高效字节流一次读写一个字节数组
private static void method4(String string, String string2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(string));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(string2));
int a = 0;
byte[] b = new byte[1024];
while ((a = bis.read(b)) != -1) {
bos.write(b, 0, a);
}
bis.close();
bos.close();
}
}
复制代码
作者:
liujian5461267
时间:
2016-5-8 12:59
package com.test20;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
//文本复制
public class fileReader {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
method1("c:\\test\\heima.doc", "D:\\test\\c.doc");
long end = System.currentTimeMillis();
long time = end - start;
System.out.println(time);
}
// 字符缓冲流一次读取一个 字符串 15毫秒
// 如何实现数据的追加写入?
// 用构造方法带第二个参数是true的情况即可
private static void method1(String s1, String s2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(s1));
// BufferedReader br = new BufferedReader(new StringReader(s1));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(s2, true)));
int linecount = 0;
String s;
while ((s = br.readLine()) != null) {
pw.println(linecount++ + ": " + s);
}
pw.close();
}
// 字符缓冲流一次读取一个 字符串 15毫秒
private static void method2(String s1, String s2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(s1));
BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
// PrintWriter pw = new PrintWriter((new FileWriter(s2,true)));
int linecount = 0;
String s;
while ((s = br.readLine()) != null) {
// pw.println(linecount++ +": "+s);//一句顶三句
bw.write(linecount++ + ": " + s);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
// pw.close();
}
// 字符缓冲流一次读取一个 字符数组 16毫秒
private static void method3(String s1, String s2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(s1));
BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
char[] char1 = new char[1024];
int s = 0;
while ((s = br.read(char1)) != -1) {
bw.write(char1, 0, s);
}
br.close();
bw.close();
}
// 字符缓冲流一次读取一个 字符 15毫秒
private static void method4(String s1, String s2) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(s1));
BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
int s = 0;
while ((s = br.read()) != -1) {
bw.write(s);
}
br.close();
bw.close();
}
// 基本字符流流一次读取一个 字符数组 15毫秒
private static void method5(String s1, String s2) throws IOException {
FileReader fr = new FileReader(s1);
FileWriter fw = new FileWriter(s2);
char[] char1 = new char[1024];
int s = 0;
while ((s = fr.read(char1)) != -1) {
fw.write(char1, 0, s);
}
fr.close();
fw.close();
}
// 基本字符流流一次读取一个 字符 31毫秒
private static void method6(String s1, String s2) throws IOException {
FileReader fr = new FileReader(s1);
FileWriter fw = new FileWriter(s2);
int s = 0;
while ((s = fr.read()) != -1) {
fw.write(s);
}
fr.close();
fw.close();
}
}
复制代码
作者:
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