这可能有点复杂,但我会尽力解释
好吧,让我先把我的CDList类
* MAIN: *现在这是主程序问的问题 主程序是测试CDList功能。您可以对5个电话进行硬编码 要添加,然后add()将提示用户输入。你的主程序应该调用所有 CDlist公共方法,并产生标记输出清楚地解释了什么是第2页 输出试图证明。 这就是我到目前为止的主要内容..只是为了测试CDList中的add方法......
输出: 该程序的输出是一个完整的捕获测试会话,主要是 程序产生标签说“测试删除:”(或其他),然后显示 结果。将结果清除给评分者。你要1)添加5张CD(似乎有道理 数据,每个至少3个轨道)到CDList,2)显示按艺术家排序的数据,3) 按标题排序后显示列表,4)在删除其中一个之前和之后显示列表 列表中的CD。要获得完全信用,必须清楚地标记输出和CD列表 显示必须整洁,易于阅读。
编辑5:我的CDList CLASS
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;
}
}
输出IM错误:
是否要添加新CD请输入艺术家姓名和CD title:eminem recovery输入歌曲数量:3输入名称 歌曲:太空束缚不怕你喜欢的方式请你说谎 输入艺术家姓名和CD标题:micheal jackson惊悚片 输入歌曲数量:3输入歌曲名称:击败它 惊悚宝贝是我的请输入艺术家姓名和CD标题: sean paul tomahawk technique输入歌曲数量:3输入 歌曲名称:她不介意把它放在你身上请输入 艺术家姓名和CD标题:Manafest Glory输入数字 歌曲:3输入歌曲的名称:你在哪里失控的梦想 请输入艺术家姓名和CD标题:kj-52合作 输入歌曲数量:3输入歌曲名称:亲爱的苗条 你在哪里
线程“main”中的异常java.lang.NullPointerException at cd.CDList.SortArtist(CDList.java:86)在cd.Main.main(Main.java:17) Java结果:1
这是第86行: if(CDlist [i] .getArtist()。compareTo(CDlist [i + 1] .getArtist())&lt; 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 CLASS
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:Tracklist类
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]);
}
}
}
答案 0 :(得分:0)
我认为这是错误的
int songNum = input.nextByte();
您需要使用nextInt()
我的代码中没有找到主要方法或任何静态阻止。
你还没有打电话给他添加方法
CD[i]= new CDList(5);
CD[i].add() or call add() method in the CD constructor
添加
您的完整工作代码:
像这样更改SortArtist
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 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