package net.figo;public class DVDSet { /**
 * @param args
 */
String DVDName[] = new String[100];
int DVDNo[] = new int[100];

}
--------------------------------------------------------------package net.figo;import java.util.Scanner;
public class Addordelete {
Scanner input = new Scanner(System.in);
DVDSet dm = new DVDSet();
int cnt=0;
public void addDVD(){

System.out.println("请输入需要添加的碟片名");
String DName = input.next();
dm.DVDName[cnt] = DName;
System.out.println("请输入需要添加的碟片张数");
int No = input.nextInt();
dm.DVDNo[cnt] = No;
cnt++;
}

public void deleteDVD(){
System.out.println("输入删除的碟片名");
String DName = input.next();
for(int i=0; i<cnt;i++){
if((dm.DVDName[i]).equals(DName)){
for(i=cnt;i<100-1;i++){
dm.DVDName[i]=dm.DVDName[i+1];
 dm.DVDNo[i]=dm.DVDNo[i+1];
 cnt--;
}

}
}
}
}
------------------------------------------------------------------------------------package net.figo;import java.util.Scanner;
public class DVDTest { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Addordelete ad = new Addordelete();
DVDSet ds = new DVDSet();
Scanner input = new Scanner(System.in);
char sel='y';
while(sel=='y'){
ad.addDVD();
System.out.println("需要继续添加吗");
sel=input.next().charAt(0);
}

ad.deleteDVD();
int cnt;
for( cnt=0;cnt<5;cnt++){
System.out.println(ds.DVDName[cnt]+" "+ds.DVDNo[cnt]);
}
}}
------------------------------------------------------------------------------------------
//运行结果请输入需要添加的碟片名
天空
请输入需要添加的碟片张数
1
需要继续添加吗
y
请输入需要添加的碟片名
白云
请输入需要添加的碟片张数
3
需要继续添加吗
y
请输入需要添加的碟片名
高山流水
请输入需要添加的碟片张数
3
需要继续添加吗
n
输入删除的碟片名
白云
null 0
null 0
null 0
null 0
null 0

解决方案 »

  1.   

    public class Addordelete {
    Scanner input = new Scanner(System.in);
    DVDSet dm = new DVDSet();//这一行去掉
    private DVDSet dm;
    public Addordelete(DVDSet dm)
    {
      this.dm = dm;
    }

    int cnt=0;
    public void addDVD(){System.out.println("请输入需要添加的碟片名");
    String DName = input.next();
    dm.DVDName[cnt] = DName;
    System.out.println("请输入需要添加的碟片张数");
    int No = input.nextInt();
    dm.DVDNo[cnt] = No;
    cnt++;
    }public void deleteDVD(){
    System.out.println("输入删除的碟片名");
    String DName = input.next();
    for(int i=0; i<cnt;i++){
    if((dm.DVDName[i]).equals(DName)){
    for(i=cnt;i<100-1;i++){
    dm.DVDName[i]=dm.DVDName[i+1];
    dm.DVDNo[i]=dm.DVDNo[i+1];
    cnt--;
    }}
    }
    }
    }
    //调用
    DVDSet ds = new DVDSet();
    Addordelete ad = new Addordelete(ds);