努力!加油!!!
1、请将D盘下hello.txt文件拷贝至E盘下world.txt文件(至少两种方式)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Public static void main(String[] args) {
Test01 test = new Test01();
test.file();
}
private void file() {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("D:\\hello.txt");
fileOutputStream = new FileOutputStream("E:\\world.txt");
//第一种 借助中间变量移动
// byte[] m=new byte[1024*1];
// int len=0;
// while((len=fileInputStream.read(m))!=-1) {
// fileOutputStream.write(m, 0, len);
// }
//第二种 一个一个的移动
int len = 0;
while ((len= fileInputStream.read()) != -1) {
fileOutputStream.write(len);
}
} catch (FileNotFoundException e) {
System.out.println("没有发现异常");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//最后判断非空 关闭资源
try {
if(fileInputStream!=null) {
fileInputStream.close();}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、请计算E盘下文件夹dsf的大小
public static void main(String[] args) {
File file = new File("E:\\dsf");
System.out.println(size(file));
}
public static long size(File file) {
long len = 0;
if (file != null) { // 判断非空
boolean isFile = file.isFile();
if (isFile) { // 判断是文件
len = file.length();
// System.out.println(len);
} else { // 判断是文件夹
File[] list = file.listFiles(); // 把文件夹里的文件列出来
for (File v : list) {
len += size(v);
}
}
return len;
} else {
return len = -1;
}
}
3、请设计简易石头剪刀布小游戏,要求如下:
####### (1)、可用0 1 2分别代表石头、剪刀、布
####### (2)、两个玩家(提示:可用两个变量代替)
####### (3)、两个玩家中至少有一家的出拳是随机的
####### (4)、可判断输赢
public static void main(String[] args) {
int[] arr = { 0, 1, 2 };
int len = arr.length;
Random random = new Random();
int a = random.nextInt(len);
System.out.println(a);
int b = random.nextInt(len);
System.out.println(b);
// ==========方法一
// if(a==0) {//石头
// if(b==0) {
// System.out.println("平局");
// }
// if(b==1) {
// System.out.println("你输了");
// }
// if(b==2) {
// System.out.println("你赢了");
// }
// }
// else if(a==1) {//剪刀
// if(b==0) {
// System.out.println("你赢了");
// }
// if(b==1) {
// System.out.println("平局");
// }
// if(b==2) {
// System.out.println("你输了");
// }
// }
// else if(a==2) {//布
// if(b==0) {
// System.out.println("你输了");
// }
// if(b==1) {
// System.out.println("你赢了");
// }
// if(b==2) {
// System.out.println("平局");
// }
// }
// ===========方法二
// a为对方输入的数;b为自己输入的数
if (a == b) {
System.out.println("平局");
} else if (a - b == 1 || a - b == -2) {
System.out.println("你赢了");
} else {
System.out.println("你输了");
}
}
4、请将两个字符数组合并成一个新数组,要求如下:
(1)、原来两个字符数组A和B
(2)、A和B的长度不一样
(3)、合并后为一个新的字符数组C
(4)、逆序打印数组C
public static void main(String[] args) {
char[] A = { '5', 'd', 'e' };
char[] B = { 'z', 'e', 'v', 'b', 'p' };
int len = A.length + B.length;
char[] newChar = new char[len];
for (int i = 0; i < A.length; i++) {
newChar[i] = A[i];
}
for (int i = 0; i < B.length; i++) {
newChar[i + A.length] = B[i];
}
// for(int i=0;i<len;i++) {
// if(i <A.length) {
// newChar[i] = A[i];
// }else {
// newChar[i] = B[i-A.length];
// }
// }
for (int i = newChar.length - 1; i >= 0; i--) {
System.out.println(newChar[i]);
}
}
5、请用面向对象思想描述商品清单,要求如下:
(1)、商品有:商品编号,名称,价格,地址等信息
(2)、购物清单(集合)中有多种商品
(3)、可通过购物清单中某商品编号获知该商品的其他信息
(4)、可计算购物清单中商品的总价
// 商品
public class Goods {
private String name;
private long price;
private String adress;
private String number;
//构造函数
public Goods(String name, long price, String adress, String number) {
super();
this.name = name;
this.price = price;
this.adress = adress;
this.number = number;
}
//set get方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getPrice() {
return price;
}
public void setPrice(long price) {
this.price = price;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Goods [name=" + name + ", price=" + price + ", adress=" + adress + ", number=" + number + "]";}
public static void main(String[] args) {
Goods goods1 = new Goods("45215", 11, "努", "双流区");
Goods goods2 = new Goods("45545", 22, "力", "成华区");
Goods goods3 = new Goods("42115", 33, "加", "锦江区");
Goods goods4 = new Goods("45235", 44, "油", "金牛区");
HashMap<String, Goods> hashMap = new HashMap<String, Goods>();
hashMap.put(goods1.getNumber(), goods1);
hashMap.put(goods2.getNumber(), goods2);
hashMap.put(goods3.getNumber(), goods3);
hashMap.put(goods4.getNumber(), goods4);
// 表示键
Set<String> keySet = hashMap.keySet();
// 迭代器
Iterator<String> iterator = keySet.iterator();
int sum = 0;
while (iterator.hasNext()) {
String num = iterator.next();
// 求出对应的键值
Goods goods = hashMap.get(num);
long price = goods.getPrice();
//求和 累加
sum = (int) (sum + price);
}
System.out.println(sum);
}