黑马程序员技术交流社区
标题:
【记录】代码练习-部分入学题
[打印本页]
作者:
Kevin.Kang
时间:
2015-9-2 11:14
提示:
该帖被管理员或版主屏蔽
作者:
Kevin.Kang
时间:
2015-9-2 11:15
package fanShe;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
*/
public class Test10 {
public static void main(String[] args) throws IOException {
File SrcFolder = new File("C:\\Test");
File destFolder = new File("D:\\hhh");
if (!destFolder.exists()) {
destFolder.mkdir();
}
copy(SrcFolder, destFolder);
}
private static void copy(File srcFolder, File destFolder)
throws IOException {
if (srcFolder.isDirectory()) {
File[] files = srcFolder.listFiles();
for (File f : files) {
copy(f, destFolder);
}
} else {
if (srcFolder.getName().endsWith(".txt")) {
File destFile = new File(destFolder, srcFolder.getName()
.replace(".txt", ".java"));
copyFile(srcFolder, destFile);
}
}
}
private static void copyFile(File srcFolder, File destFile)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcFolder));
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 11:18
package fanShe;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
/*
* 使用高效字符缓冲流复制文件
*/
public class Test08 {
public static void main(String[] args) throws Exception {
File srcFile = new File("a.txt");
File destFile = new File("b.txt");
BufferedReader br = new BufferedReader(new FileReader(srcFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 11:20
package com.itheima;
/*
* 统计一个文本文件中字符出现的次数,结果存入另外的一个文本文件中。例如:
* a: 21 次
* b: 15 次
* c: 15 次
* 把: 7 次
* 当: 9 次
* 前: 3 次
* ,:30 次
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Set;
import java.util.TreeMap;
public class Test5 {
public static void main(String[] args) throws Exception {
File srcFile = new File("Test5_1.txt");
File destFile = new File("Test5_2.txt");
count(srcFile, destFile);
}
private static void count(File srcFile, File destFile) throws Exception {
BufferedReader br = new BufferedReader(new FileReader(srcFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
String str = null;
while ((str = br.readLine()) != null) {
char[] chs = str.toCharArray();
for (int i = 0; i < chs.length; i++) {
if (map.containsKey(chs[i])) {
map.put(chs[i], map.get(chs[i]) + 1);
} else {
map.put(chs[i], 1);
}
}
}
Set<Character> set = map.keySet();
for (Character cc : set) {
System.out.println(cc + ":" + map.get(cc));
}
br.close();
bw.close();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 11:22
package fanShe;
/*
* 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
*/
import java.util.ArrayList;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 100; i++) {
list.add(i);
}
int last = lastOne(list);
System.out.println("最后剩下第" + last + "个人");
}
public static int lastOne(List<Integer> list) {
int num = 0;
while (list.size() > 1) {
num++;
Integer remove = (Integer) list.remove(0);
if (num != 14)
list.add(remove);
if (num == 14) {
System.out.println("移除第" + remove + "个人");
num = 0;
}
}
return list.get(0);
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 11:29
package fanShe;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;
/*
* 已知一个类,定义如下:
* package cn.itcast.heima;
* public class DemoClass {
* public void run()
* {
* System.out.println("welcome to heima!");
* }
* }
* (1) 写一个Properties格式的配置文件,配置类的完整名称。
* (2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,用反射 的方式运行run方法。
*
*/
public class Test07 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("a.Properties"));
Properties p = new Properties();
p.load(br);
String s = p.getProperty("className");
Class c = Class.forName(s);
Object obj = c.newInstance();
Method m = c.getMethod("run", null);
m.invoke(obj, null);
}
}
复制代码
作者:
freehello
时间:
2015-9-2 11:31
好的,收了
作者:
蚊子先生
时间:
2015-9-2 12:12
记不住就多敲直到记住了!
作者:
Kevin.Kang
时间:
2015-9-2 13:04
package fanShe;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
/*
* 定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5)。
*/
public class Test05 {
public static void main(String[] args) throws Exception {
File file = new File("exercise.txt");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
byte[] bys = new byte[5];
int len = 0;
while ((len = bis.read(bys)) != -1) {
String str = new String(bys, 0, len);
System.out.print(str);
}
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 13:05
package fanShe;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Arrays;
/*
* 已知文件a.txt文件中的内容为“bcdeadferwplkou”,
* 请编写程序读取该文件内容,并按照自然顺序排序后输出到b.txt文件中。即b.txt中的文件内容应为“abcd…………..”这样的顺序。
*/
public class Test04 {
public static void main(String[] args) throws Exception {
File srcFile = new File("a.txt");
File destFile = new File("b.txt");
BufferedReader br = new BufferedReader(new FileReader(srcFile));
String str = br.readLine();
char[] chs = str.toCharArray();
Arrays.sort(chs);
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));
bw.write(chs);
bw.flush();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 13:06
package fanShe;
import java.util.ArrayList;
import java.util.ListIterator;
/*
* 一个ArrayList对象aList中存有若干个字符串元素,
* 现欲遍历该ArrayList对象,删除其中所有值为"abc"的字符串元素,请用代码实现。
*
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
// 创建集合
ArrayList<String> aList = new ArrayList<>();
// 添加元素
aList.add("abc");
aList.add("abdc");
aList.add("aasbc");
aList.add("abc");
aList.add("abfsdc");
aList.add("abc");
aList.add("asdfbc");
// 获得List集合特有迭代器
ListIterator<String> li = aList.listIterator();
// 遍历集合,如果元素为abc就删除
while (li.hasNext()) {
if (li.next().equals("abc")) {
li.remove();
}
}
// 打印新的集合
System.out.println(aList);
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 13:15
package collectionDemo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/*
* 编写程序,生成5个1至10之间的随机整数,存入一个List集合,
* 编写方法对List集合进行排序(自定义排序算法,禁用Collections.sort方法和TreeSet),
* 然后遍历集合输出。
*/
public class ArrayListDemo3 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(new Random().nextInt(11));
}
sort(list);
System.out.println(list);
}
private static void sort(List<Integer> list) {
// TODO Auto-generated method stub
Integer[] i = list.toArray(new Integer[list.size()]);
list.clear();
for (int x = 0; x < i.length - 1; x++) {
for (int y = 0; y < i.length - 1 - x; y++) {
if (i[y] < i[y + 1]) {
int temp = i[y + 1];
i[y + 1] = i[y];
i[y] = temp;
}
}
}
for (Integer num : i) {
list.add(num);
}
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-2 14:54
package fanShe;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/*
* 有类似这样的字符串:“1.2,3.4,5.6,7.8,5.56,44.55”, 请按照要求,依次完成以下试题
① 以逗号作为分隔符,把已知的字符串分成一个String类型的数组,
数组中的每一个元素类似于“1.2","3.4"这样的字符串;
② 把数组中的每一个元素以.作为分割,把.号左边的元素作为key,
把.号右边的元素作为value,封装到Map中,map中的key和value都是Object类型;
③ 把map中的key封装到Set中,并且把set中的元素输出;
④ 把map中的value封装到Collection中,把collection中的元素输出。
*/
public class Test80 {
public static void main(String[] args) {
String str = "1.2,3.4,5.6,7.8,5.56,44.55";
Map<String, String> map = new TreeMap<String, String>();
String[] ss = str.split(",");
for (String s : ss) {
String[] arr = s.split("\\.");
System.out.println(Arrays.toString(arr));
map.put(arr[0], arr[1]);
}
Set<String> set = new TreeSet<String>();
for (String key : set) {
System.out.println(key + ":" + map.get(key));
}
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 21:43
package studentDemo;
/*
* 有五个学生,每个学生有3门课(语文、数学、英语)的成绩,
* 写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩),
* 然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中。
* 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息。
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) throws Exception {
TreeSet<Student> set = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// TODO Auto-generated method stub
int num1 = s2.getSum() - s1.getSum();
int num2 = num1 == 0 ? s2.getName().compareTo(s2.getName()) : num1;
int num3 = num2 == 0 ? s2.getChinese() - s1.getChinese() : num2;
int num4 = num3 == 0 ? s2.getMath() - s1.getMath() : num3;
int num5 = num4 == 0 ? s2.getEnglish() - s1.getEnglish() : num4;
return num5;
}
});
System.out.println("录入学生信息,格式为name,30,30,30(姓名,三门课成绩)。");
for (int i = 1; i <= 5; i++) {
Student s = new Student();
Scanner sc = new Scanner(System.in);
System.out.println("请录入第" + i + "位学生信息:");
String str = sc.nextLine();
String[] arr = str.split(",");
s.setName(arr[0]);
s.setChinese(Integer.valueOf(arr[1]));
s.setMath(Integer.valueOf(arr[2]));
s.setEnglish(Integer.valueOf(arr[3]));
set.add(s);
}
File file = new File("stu.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write("姓名\t语文\t数学\t英语\t");
bw.newLine();
bw.flush();
for (Student s : set) {
bw.write(s.getName() + "\t" + s.getChinese() + "\t" + s.getMath() + "\t" + s.getEnglish());
bw.newLine();
bw.flush();
}
bw.close();
}
}
复制代码
package studentDemo;
public class Student {
private String name;
private int chinese;
private int math;
private int english;
private int sum;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum() {
return chinese + math + english;
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 21:47
package TCPDemo;
/*
* TCP上传文件
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;
/**
* 客户端
*
* @author Kevin
*
*/
public class ClientDemo {
public static void main(String[] args) throws Exception {
// 创建socket对象
Socket s = new Socket("192.168.0.120", 48264);
// 封装数据源对象
File file = new File("a.txt");
// 获取输入输出流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
// 读取文件,并写入到输出流中
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
// 通知服务器传送结束
s.shutdownInput();
// 释放资源
s.close();
bis.close();
}
}
复制代码
package TCPDemo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务器端
*
* @author Kevin
*
*/
public class ServerDemo {
public static void main(String[] args) throws Exception {
// 创建服务器端对象
ServerSocket ss = new ServerSocket(48264);
// 封装目的地文件对象
File file = new File("copy.txt");
// 获取客户端对象
Socket s = ss.accept();
// 获取输入输出流对象
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
// 读取流中的数据并写入到目的文件中
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
// 释放资源
s.close();
bos.close();
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 21:50
package ioTest;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/*
* 自定义字符输入流的包装类,通过这个包装类对底层字符输入流进行包装,
让程序通过这个包装类读取某个文本文件(例如,一个java源文件)时,
能够在读取的每行前面都加上有行号和冒号。
*/
public class BaoZhuang {
public static void main(String[] args) throws IOException {
File fi = new File("c:\\stu.txt");
String s = LineFile(fi);
System.out.println(s);
}
public static String LineFile(File fi) throws IOException {
MyLineReader mr = new MyLineReader(new FileReader(fi));
String s = null;
StringBuilder sb = new StringBuilder(); // 为了让程序拥有自主控制输出,因此创建.
mr.setLen(0); // 设置开始行号.
while ((s = mr.MyreadLine()) != null) {
sb.append(mr.getLen() + ":" + s + "\r\n");
}
return sb.toString(); // 将数据返回,让客户拥有输出权利.
}
}
class MyLineReader {
private int len; // 自定义行数变量
private Reader fi; // 读取数据流.
public MyLineReader(Reader fi) {
this.fi = fi;
}
public String MyreadLine() throws IOException {
len++; // 读取一行之后,len自增.
StringBuilder sb = new StringBuilder(); // 将数据存储起来.
int i = 0;
while ((i = fi.read()) != -1) {
if (i == '\r')
continue;
if (i == '\n') // 当独到行标记时候,将数据全部返回.
return sb.toString();
sb.append((char) i); // 没有到达行标记就继续读取.
}
if (sb.length() != 0) // StringBuilder的长度不为0,就继续返回数据.
return sb.toString();
return null; // 否则返回空.因为调用方法时候,是依靠返回是不是null来判断,是不是应该结束.
}
public int getLen() { // get,set方法,用来获取行号和设置行号.
return len;
}
public void setLen(int len) {
this.len = len;
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 21:55
/*
* 28人买可乐喝,3个可乐瓶盖可以换一瓶可乐,那么要买多少瓶可乐,
* 够28人喝?假如是50人,又需要买多少瓶可乐?
*/
public class Test {
public static void main(String[] args) {
int person = 13; // 自定义需求.
int cola = run(person); // 接受需要购买的次数.
System.out.println(cola);
}
public static int run(int person) {
int lid = 0; // 盖子数
int cola = 0; // 可以喝的可乐数
int gouMai = 0; // 需要购买的可乐数.
for (; cola < person; cola++) // 无论如何,可以喝的可乐都在增加.
{
if (lid == 3) // 当盖子够三个了,盖子数又定义为一个.
{ // 可以喝的可乐在上面已经增加了.
lid = 1;
} else {
gouMai++;// 如果不够三个,那么就要去买了,买一瓶,盖子自然增加一个.
lid++;
}
}
return gouMai; // 把购买的次数返回.
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 21:58
package classTest;
/*
* 定义一个交通灯枚举,包含红灯、绿灯、黄灯,
* 需要有获得下一个灯的方法,例如:红灯获取下一个灯是绿灯,绿灯获取下一个灯是黄灯。
*/
public class Test01 {
public static void main(String[] args) {
traffic s = traffic.RED.next(); //调用枚举元素以及方法.返回枚举元素.
System.out.println(s);
}
}
enum traffic
{
RED
{
public traffic next() //复写父类的抽象方法.返回还是枚举元素.
{
return GRE;
}
},GRE
{
public traffic next()
{
return YEL;
}
},YEL
{
public traffic next()
{
return RED;
}
};
public abstract traffic next(); //枚举的抽象方法,子类需要复写.
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 22:05
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
* 把当前文本中的所有文本拷贝,存入一个txt文件,统计每个字符出现的次数
* 并输出,例如 a : 21次 b: 12次.....
*/
public class Test02 {
public static void main(String[] args) throws IOException {
File fiReade = new File("c:\\a.txt");
File fiWrite = new File("c:\\b.txt");
IORW(fiReade, fiWrite);
}
// 1.这是一个将数据分离成字符串,并输出到指定文件的程序.(1)分离字符串,进行后续操作(2)写入文件,完成一个写入文件需求
public static void IORW(File fiReade, File fiWrite) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fiReade));
BufferedWriter bw = new BufferedWriter(new FileWriter(fiWrite));
StringBuilder sb = new StringBuilder();
String s = null;
while ((s = br.readLine()) != null) {
sb.append(s); // 先装进字符串.等结束后一次性给调用者.
bw.write(s);
}
ergodic(sb.toString()); // 这是一个注意.应该所有数据完成之后才可以进行迭代遍历出次数.
br.close();
bw.close();
}
// 2.接受分离出来的字符串,然后对字符串进行遍历,并将结果存入map集合.
public static void ergodic(String s) {
Map<Character, Integer> ma = new HashMap<Character, Integer>();
char[] ch = s.toCharArray();
for (int x = 0; x < ch.length; x++) {
int num = 1;
if (!ma.containsKey(ch[x])) {
ma.put(ch[x], 1);
} else {
num = ma.get(ch[x]); // 这是一个应该注意的,此时要记住之前的value值,再此基础上+1
ma.put(ch[x], ++num);
}
}
ergodicMap(ma);
}
// 3.将map集合进行迭代,
public static void ergodicMap(Map<Character, Integer> ma) {
Set<Character> se = ma.keySet();
for (char key : se) {
System.out.println(key + " : " + ma.get(key) + " 次");
}
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 22:32
import java.util.Scanner;
import java.util.TreeSet;
/*
* 编写程序,循环接收用户从键盘输入多个字符串,
* 直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
*/
public class Test03 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
TreeSet<String> ts = new TreeSet<>();
System.out.println("请开始输入:");
while (true) {
String str = s.nextLine(); // 字符串每次需要重新获得,因此要定义在循环内.
if (str.equals("end")) {
System.out.println("输入结束");
break;
} else {
ts.add(str);
}
}
for (String str : ts) {
System.out.println(str);
}
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 22:38
import java.util.Random;
/*
* 声明一个共享数组,起两个线程,
*两个线程分别隔一段时间(可以写一个随机数),给数组中添加数据,
*每一个线程为数组添加3个数据即可
*/
public class Test04 {
public static void main(String[] args) {
int[] in = new int[6]; // 需要控制因素
ArrayAdd aa = new ArrayAdd(in); // 既然要共享资源,那么这里就只能创建一个线程执行地.
for (int x = 0; x < 2; x++) {
new Thread(aa).start(); // 创建多个线程.
}
}
}
class ArrayAdd implements Runnable {
private int[] in; // 共享的资源,在创建时候,只能拥有一份.
private int num = 0;
ArrayAdd(int[] in) {
this.in = in;
}
public void run() {
while (num < in.length) {
synchronized (in) {
if (num < in.length) {
try {
Thread.sleep(500); // 线程只是等待一会时间,并没有释放锁.
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Random rd = new Random();
int i = rd.nextInt(20) + 1;
System.out.println(Thread.currentThread().getName() + "添加了:" + i);
in[num] = i;
num++;
try {
in.wait(1000); // 放弃cup,放弃锁.让其他线程拿锁.
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
break;
}
}
}
}
}
复制代码
作者:
Kevin.Kang
时间:
2015-9-3 22:44
package threadTest;
/*
* 编写三各类Ticket、SealWindow、TicketSealCenter
* 分别代表票信息、售票窗口、售票中心。售票中心分配一定数量的票,由若干个售票窗口进行出售,
* 利用你所学的线程知识来模拟此售票过程。
*/
public class Ticket {
private String name;
private int i;
public Ticket(String name, int i) {
super();
this.name = name;
this.i = i;
}
public Ticket() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
复制代码
package threadTest;
public class TicketCenter {
private int num;
public TicketCenter() {
super();
// TODO Auto-generated constructor stub
}
public TicketCenter(int num) {
super();
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
复制代码
package threadTest;
public class TicketWindow implements Runnable {
private TicketCenter tc;
private Ticket t;
private int tickets;
public TicketWindow(Ticket t, TicketCenter tc) {
super();
this.tc = tc;
this.t = t;
tickets = tc.getNum();
}
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (this) {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "正在出售第" + (tickets--) + "张票");
}
}
}
}
}
复制代码
作者:
地狱里的帅灵魂
时间:
2015-9-3 22:55
Kevin.Kang 发表于 2015-9-3 22:44
楼主好帅气,总结这么多,有考试里面的题吗
作者:
asinzuo
时间:
2015-9-3 22:59
Kevin.Kang 发表于 2015-9-2 13:04
这一题似乎不能用bos你用代码读中文试试
作者:
ybbh182
时间:
2015-9-3 23:14
多谢分享!
作者:
bgxpf
时间:
2015-9-4 08:35
最好还是加上注释,好多看的费解
作者:
a804876583
时间:
2015-9-4 09:21
收了,以后好好练习
作者:
xiao_D
时间:
2015-9-4 13:12
已收 谢谢分享
作者:
yangshibai
时间:
2015-9-4 16:16
多谢分享,可以借鉴学习。
作者:
Zack
时间:
2015-9-12 15:07
回复看题 学习一下
作者:
llwhcm
时间:
2015-9-12 19:21
哇哇 这个很屌
作者:
疯狂的小豆丁
时间:
2015-9-13 15:03
额,貌似。。。。。。和谐了
作者:
fjb0902
时间:
2015-9-13 21:51
值得收藏,
作者:
cloud1991
时间:
2015-9-14 11:41
楼主好厉害
作者:
pegasus
时间:
2015-9-14 13:14
Kevin.Kang 发表于 2015-9-2 11:15
你的这个程序是指定目录下所有.txt文件拷贝到另一个目的中,并将扩展名改为.java吧
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2