怎么错了

阿杰不爱 2013-02-14 12:30:35
package dvd;

public class DVD {
public String[]name ={};//DVD名字
public boolean []flag={};//DVD借出状态
public int []index={};
public String[] str = {} ;
}package dvd;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class DVDManager {

DVD d = new DVD();
Scanner input = new Scanner(System.in);
public int j;
public void show(){
System.out.println("欢迎使用迷你DVD管理器");
System.out.println("---------------------------------------------");
System.out.println("1.新增DVD");
System.out.println("2.查看DVD");
System.out.println("3.删除DVD");
System.out.println("4.借出DVD");
System.out.println("5.归还DVD");
System.out.println("6.排行榜");
System.out.println("7.退出");
System.out.println("---------------------------------------------");
System.out.print("请选择:");
while(!input.hasNextInt()){
System.out.println("您输入的不是整数,请重新输入:");
String str = input.next();
}
int i = input.nextInt();
switch(i){
case 1:
System.out.println("-->新增DVD");
addDVD();
backMenu();
System.out.println("******************************");
break;
case 2:
System.out.println("-->查看DVD");
seeDVD();
backMenu();
System.out.println("******************************");
break;
case 3:
System.out.println("-->删除DVD");
deleteDVD();
backMenu();
System.out.println("******************************");
break;
case 4:
System.out.println("-->借出DVD");
lendDVD();
backMenu();
System.out.println("******************************");
break;
case 5:
System.out.println("-->归还DVD");
returnDVD();
backMenu();
System.out.println("******************************");
break;
case 6:
System.out.println("-->排行榜");
rankList();
backMenu();
System.out.println("******************************");
break;
case 7:

return;
default :
while(i<1||i>7){
System.out.println("您输入的不是1-7整数,请重新输入");
i = input.nextInt();
}
break;
}
}
public void enlargeLength(){
String[]name1 =new String[d.name.length+1];
System.out.println(name1.length);
System.arraycopy(d.name, 0, name1, 0, d.name.length);
d.name= name1;
boolean []flag1 = new boolean[d.flag.length+1];
System.arraycopy(d.flag, 0, flag1, 0, d.flag.length);
d.flag =flag1;
String[]str1 =new String[d.str.length+1];
System.arraycopy(d.str, 0, name1, 0,d.str.length);
d.str= str1;
}
public void cutLength(int i){
String []name1 = new String[d.name.length-1];
System.arraycopy(d.name, 0, name1, 0, i+1);
System.arraycopy(d.name, i+1, name1, i, name1.length-i);
d.name =name1;
boolean []flag1 = new boolean[d.flag.length-1];
System.arraycopy(d.flag, 0,flag1, 0, i+1);
System.arraycopy(d.flag, i+1, flag1, i, flag1.length-i);
d.flag =flag1;
String []str1 = new String[d.str.length-1];
System.arraycopy(d.str, 0, str1, 0, i+1);
System.arraycopy(d.str, i+1, name1, i, d.str.length-i);
d.str =str1;
}
public void addDVD(){
String str ;
do{
enlargeLength();
System.out.print("请输入DVD的名称:");
String name1 = input.next();
d.name[j] =name1;
System.out.println("新增《"+d.name[j]+"》成功");
j++;
System.out.print("是否继续输入(y/n)");
str = input.next();
while(!str.equalsIgnoreCase("y")&&!str.equalsIgnoreCase("n")){
System.out.println("您的输入有误,请重新输入(y/n)");
str =input.next();
}
}while(str.equalsIgnoreCase("y"));
}
public void seeDVD(){
System.out.println("序号\t\t状态\t\t名称\t\t借出日期");
for (int i = 0; i < d.name.length; i++) {

if(d.flag[i]==true){
System.out.println((i+1)+"\t\t已借出"+"\t\t"+d.name[i]+"\t\t"+d.str[i]);
}else{
System.out.println((i+1)+"\t\t可借"+"\t\t"+d.name[i]);
}
}
}
public void deleteDVD(){
System.out.print("请输入DVD名称:");
String name = input.next();
for (int i = 0; i < d.name.length; i++) {
if(d.name[i].equals(name)&&d.flag[i]==false){
cutLength(i);
break;
}else if(d.name[i].equals(name)&&d.flag[i]==true){
System.out.println("未归还,不能删除"+name);
}
if(i==d.name.length-1&&!d.name[i].equals(name)){
System.out.println("没有"+name);
break;
}
}
System.out.println("删除《"+name+"》成功");
}
public void lendDVD(){
System.out.print("输入DVD名称:");
String name =input.next();
for (int i = 0; i < d.name.length; i++) {
if(d.name[i].equals(name)&&d.flag[i]==false){
System.out.print("请输入借出日期(年-月-日)");
String str =input.next();
d.str[i] =str;
d.flag[i] =true;
d.index[i]++;
System.out.println("借出"+name+"成功");
break;
}else if(d.name[i].equals(name)&&d.flag[i]==true){
System.out.println("已被借出");
}
if(i==d.name.length-1&&!d.name[i].equals(name)){
System.out.println("没有"+name);
break;
}
}
}
public void returnDVD(){
System.out.print("请输入DVD名称:");
String name = input.next();
for (int i = 0; i < d.name.length; i++) {
if(d.name[i].equals(name)&&d.flag[i]==true){
System.out.print("请输入归还日期(年-月-日)");
String str = input.next();
d.flag[i] =false;
System.out.println("归还"+name+"成功");
System.out.println("借出日期为"+d.str[i]);
System.out.println("归还日期为"+str);
long charge = charge(d.str[i],str);
System.out.println("应付租金为"+charge);
}else if(d.name[i].equals(name)&&d.flag[i]==false){
System.out.println("您输入的DVD"+name+"未借出");
}
}
}
public long charge(String dstr1,String dstr2){
long charge=0;
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
try{
Date d1 = sd.parse(dstr1);
Date d2 = sd.parse(dstr2);
charge=(d2.getTime()-d1.getTime())/(24*60*60*1000);
}catch(ParseException e){
e.printStackTrace();
}
return charge;
}
public void rankList(){
for (int i = 0; i <d.index.length-1; i++) {
for (int j = i; j < d.index.length-1; j++) {
if(d.index[j]<d.index[j+1]){
int temp =d.index[j];
d.index [j] =d.index[j+1];
d.index [j+1]=temp;
}
}
}
for (int i = 0; i < d.index.length; i++) {
System.out.println("次数\t\t名称");
System.out.println(d.index[i]+"\t\t"+d.name[i]);
}
}
public void backMenu(){
System.out.print("输入0返回:");
int i= input.nextInt();
if(i!=0){
System.out.println("您的输入有误,请重新输入,输入0返回");
i = input.nextInt();
}
show();
}

}
package dvd;

public class Demo1 {

public static void main(String[] args) {
DVDManager dm = new DVDManager();
dm.show();

}
}
...全文
211 9 打赏 收藏 举报
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
阿杰不爱 2013-02-17
谢谢各位,删除数组的时候也有错误,我已经找到,在这不一一写出了。
  • 打赏
  • 举报
回复
lcb378533070 2013-02-16
public void enlargeLength() { String[] name1 = new String[d.name.length + 1]; System.out.println(name1.length); System.arraycopy(d.name, 0, name1, 0, d.name.length); d.name = name1; boolean[] flag1 = new boolean[d.flag.length + 1]; System.arraycopy(d.flag, 0, flag1, 0, d.flag.length); d.flag = flag1; String[] str1 = new String[d.str.length + 1]; System.arraycopy(d.str, 0, name1, 0, d.str.length); //这里错了应该是System.arraycopy(d.str, 0, str1, 0, d.str.length),等于是将d.name再次置空了 d.str = str1; }
  • 打赏
  • 举报
回复
zeko075 2013-02-16
没看懂LZ想问什么,测试了一下没什么问题啊···错误代码贴出来看看···
  • 打赏
  • 举报
回复
lcb378533070 2013-02-16
public String[]name ={}; 一开始都没赋值给DVD.name数组
  • 打赏
  • 举报
回复
阿杰不爱 2013-02-16
帖子不能沉啊,麻烦大家看看
  • 打赏
  • 举报
回复
	public void enlargeLength() {
		String[] name1 = new String[d.name.length + 1];
		System.out.println(name1.length);
		System.arraycopy(d.name, 0, name1, 0, d.name.length);
		d.name = name1;
		boolean[] flag1 = new boolean[d.flag.length + 1];
		System.arraycopy(d.flag, 0, flag1, 0, d.flag.length);
		d.flag = flag1;
		String[] str1 = new String[d.str.length + 1];
		System.arraycopy(d.str, 0, name1, 0, d.str.length); //这里错了应该是System.arraycopy(d.str, 0, str1, 0, d.str.length)
		d.str = str1;
	}
  • 打赏
  • 举报
回复
阿杰不爱 2013-02-16
引用 3 楼 lcb378533070 的回复:
public String[]name ={}; 一开始都没赋值给DVD.name数组
嗯,我想增加数组长度再赋值。
  • 打赏
  • 举报
回复
阿杰不爱 2013-02-16

大家看看,查看DVD的时候为什么是null
  • 打赏
  • 举报
回复
阿杰不爱 2013-02-14
坐等回复,为什么DVD名字 是null
  • 打赏
  • 举报
回复
相关推荐
发帖
Java SE
加入

6.2w+

社区成员

Java 2 Standard Edition
社区管理员
  • Java SE
申请成为版主
帖子事件
创建了帖子
2013-02-14 12:30
社区公告
暂无公告