0

这可能有点复杂,但我会尽力解释

好吧,让我先把我的 CDList 类

* MAIN: *现在这是主程序问我的问题 主程序是测试 CDList 功能。您可以硬编码 5 次调用 add,然后 add() 将提示用户输入。您的主程序应该调用所有 CDlist 公共方法,并生成带标签的输出,清楚地解释 Page 2 of 2 输出试图演示的内容。这就是我到目前为止所拥有的 main ......只是为了测试 CDList 中的 add 方法......

输出: 这个程序的输出是一个完整的捕获测试会话,主程序产生标签说,“测试删除:”(或其他),然后显示结果。让评分员清楚地看到结果。您要 1) 将 5 张 CD(具有合理数据,每张至少 3 首曲目)添加到 CDList,2) 显示按艺术家排序的数据,3) 显示按标题排序后的列表,以及 4) 显示之前的列表并在从列表中删除其中一张 CD 之后。要获得满分,输出必须清楚地标记,CD 列表显示必须整洁且易于阅读。

编辑 5:我的 CDList 类

 package cd;

import java.util.Scanner;

public class CDList {

    public int cdnum;
    private CD[] CDlist;
    private int front, rear;
    //private String artist;      
    //private String title;

    public CDList(int size) {
        CDlist = new CD[size];
        cdnum = 0;
        front = 0;
        rear = size - 1;
    }

   public boolean add() {
        boolean result = false;
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the Artist name and the CD title: ");
        CD yourCD = new CD(input.nextLine(), input.nextLine());
        System.out.println("Enter the number of songs: ");
        int songNum = input.nextInt();
        System.out.println("Enter the name of the songs: ");

        for (int i = 0; i <= songNum; i++) {
            yourCD.addTrack(input.nextLine());
        }
        CDlist[cdnum] = yourCD;


        if (rear == front) {
            result = false;
        } else {
            if (CDlist[rear] != null) {
                rear--;
            } else {
                CDlist[rear] = yourCD;
            }
            result = true;
        }

        return result;    

    }

    public void delete() {
        Scanner deleteInput = new Scanner(System.in);
        System.out.println("Which artist you would like to delete? ");
        System.out.println("Enter artist name and title to be deleted:");
        String artist = deleteInput.nextLine();
        String title = deleteInput.nextLine();

        for (int i = 0; i <= CDlist.length - 1; i++) {

            if ((CDlist[i].getArtist().equals(artist))
                    && (CDlist[i].getTitle().equals(title))) {
                System.out.println("Found: " + CDlist[i].getArtist());
                System.out.println(CDlist[i].getTitle());

                if (deleteInput.nextInt() == 1) {
                    CDlist[i] = null;
                    cdnum--;
                }
            } else {
                System.out.println("CD not found in the list.");
            }
        }


    }

    public void SortArtist() {

        CD temp = new CD(" "," ");
        for (int i = 0; i <= CDlist.length; i++) {
            if (i < CDlist.length-1) {
                if (CDlist[i].getArtist().compareTo(CDlist[i+1].getArtist())<0){
                temp = CDlist[i];
                CDlist[i] = CDlist[i + 1];
                CDlist[i + 1] = temp;
            }
        }
    }
    }

    public void SortTitle() {
        CD temp1 ;
        for (int i = cdnum; i > 0; i--) {
            int x = 0;
            for (int j = 1; j <= i; j++) {
                if (CDlist[i].getTitle().compareTo(CDlist[i+1].getTitle())<0) {
                    x = j;
                }
            }
            temp1 = CDlist[x];
            CDlist[x] = CDlist[i];
            CDlist[i] = temp1;
        }
    }

    public void Display() {
        for (int i = 0; i <= cdnum; i++) {
            while (CDlist[i] == null) {
                i++;
            }
            CDlist[i].display();
        }
    }

    public int size() {
        return cdnum;
    }
}

输出错误:

是否要添加新CD 请输入艺术家姓名和CD 标题:eminem recovery 输入歌曲数量:3 输入歌曲名称:空间有限 不怕爱你说谎的方式 请输入艺术家姓名和CD 标题:micheal jackson 惊悚片 输入歌曲数量:3 输入歌曲名称:beat it 惊悚片 baby be mine 请输入艺术家姓名和 CD 标题:sean paul tomahawk technology 输入歌曲数量:3 输入名称歌曲:她不介意放在你身上 请输入艺术家姓名和CD名称:Manafest Glory 输入歌曲数量:3 输入歌曲名称:你在哪里失控的梦想 请输入艺术家姓名和 CD 标题:kj-52 合作 输入歌曲数量: 3 输入歌曲名称:亲爱的苗条 你在哪里 为什么

cd.Main.main(Main.java:17) 处 cd.CDList.SortArtist(CDList.java:86) 处的线程“main”java.lang.NullPointerException 异常 Java 结果:1

这是第 86 行: if (CDlist[i].getArtist().compareTo(CDlist[i+1].getArtist())<0)

第 17 行和第 17 行:list.SortArtist();


主要的

   package cd;

public class Main {

    public static void main(String[] args) {


        System.out.println("Would you like to add a new CD");
        CDList list = new CDList(5);
        for (int i = 0; i < 5; i++) {
            list.add();
        }
        list.SortArtist();
        list.Display();
        list.SortTitle();
        list.Display(); 
        list.delete();
        list.Display();


    }
}

EDIT3:我的 CD 课

package cd;

公开课 CD {

private String artist;      //stores artist name
private String title;       //stores CD title
private String track;
private tracklist list;            //tracklist variable

// constructor, sets artist and title, creates tracklist array
public CD(String artistname, String titlename) {
    artist = artistname;
    title = titlename;
    list = new tracklist();

}

public String getArtist() {
    return artist;

}

public String getTitle() {
    return title;
}

// adds a new track, return true if less than 100, flase if no space
public boolean addTrack(String trackinfo) {
    if (list.count < 100) {
        list.add(trackinfo);
        return true;
    } else {
        return false;
    }
}

public int numTracks() {
    return list.count;
}

public void display() {
    System.out.println("Artist:  \t" + artist);
    System.out.println("CD Title: \t" + title);
    list.display(5);
}

}

EDIT4:曲目列表类

    package cd;

public class tracklist {

    public String[] lists;  //array
    private String tNum;
    private String tName;
    int numElements;        // Counter to keep track of the used slots
    int count;              //loop counter

// constructor, creats array for tracks
    public tracklist() {

        numElements = 0;
        lists = new String[100];

    }

// adds tracks to tracklist
    public boolean add(String track) {
        if (numElements < 100) {
            lists[numElements] = track;
            numElements++;
            return true;
        } else {
            System.out.println("The tracklist is full.");
            return false;
        }
    }

//returns the number of elements in array
    public int count() {
        return numElements;
    }

//displays tracklist in required form
    public void display(int indent) {
        tName = "%" + indent + "s";
        tNum = "%" + (indent - 2) + "d";

        System.out.printf(tName, "Track #");
        System.out.println("\t\tSong Title");


        for (count = 0; count < numElements; count++) {
            System.out.printf(tNum, count + 1);
            System.out.println("\t\t" + lists[count]);
        }
    }
}
4

1 回答 1

0

我认为这是错误的

 int songNum = input.nextByte();

你需要使用nextInt()

我没有在您的代码中找到main 方法或任何静态块。

你还没有调用他的 add 方法

 CD[i]= new CDList(5);

CD[i].add() or  call add() method in the CD constructor

添加

您的完整工作代码:

像这样更改 SortArtist

公共无效排序艺术家(){

    CD temp = new CD(" ", " ");
    for (int i = 0; i <= CDlist.length; i++) {
        if(i<CDlist.length-1){
        if (CDlist[i].getArtist().compareTo(CDlist[i + 1].getArtist()) < 0) {
            temp = CDlist[i];
            CDlist[i] = CDlist[i + 1];
            CDlist[i + 1] = temp;
        }
    }
    }
}

添加方法::

  public boolean add() {
        boolean result = false;
        Scanner input = new Scanner(System.in);

        System.out.println("Please enter the Artist name and the CD title: ");
        CD yourCD = new CD(input.nextLine(), input.nextLine());
        System.out.println("Enter the number of songs: ");
        int songNum = input.nextByte();
        System.out.println("Enter the name of the songs: ");

        for (int i = 0; i <= songNum; i++) {
            yourCD.addTrack(input.nextLine());
        }
        CDlist[cdnum] = yourCD;


        if (rear == front) {
            result = false;
        } else {
            if (CDlist[rear] != null) {
                rear--;
            } else {
                CDlist[rear] = yourCD;
            }
            result = true;
        }
        return result;    

    }

重要的是

更改 Main.java

public class Main {

public static void main(String[] args) {


   System.out.println("Would you like to add a new CD");
 CDList list=new CDList(2);  // Two is the No of CDs you can prompt the user to enter by reading the input and pass it to the constructor
    for (int i=0; i < 2 ; i++) {
        list.add();
    }   
    list.SortArtist();
    list.Display();

}

}

预期输出

Would you like to add a new CD
Please enter the Artist name and the CD title: 
Ram
Charan
Enter the number of songs: 
1
Enter the name of the songs: 
song1
Please enter the Artist name and the CD title: 
Ram
Racha
Enter the number of songs: 
2
Enter the name of the songs: 
song1
song2
Artist:     Ram
CD Title:   Racha
Track #     Song Title
  1     
  2     song1
  3     song2
于 2013-03-14T05:37:22.027 回答