0
你好,我想我真的很難與這部分。我正在製作一個程序,它接受來自用戶的名字和姓氏的值,然後對其進行排隊(CASE1)。然後能夠將其出列(CASE2)並最終顯示列表上的所有內容。在IDE上沒有錯誤,但我不能得到我想要的結果,在CASE 2中它引發異常錯誤,意味着傳遞給它的列表爲空。在CASE 3中沒有顯示任何值。我該如何解決?Java隊列,出列隊列和列隊隊列/出隊列表中的所有值
import java.util.*;
import java.util.Iterator;
class Customer2 {
public String lastName;
public String firstName;
public Customer2() {
}
public Customer2(String last, String first) {
this.lastName = last;
this.firstName = first;
}
public String toString() {
return firstName + " " + lastName;
}
}
class HourlyCustomer2 extends Customer2 {
public double hourlyRate;
public HourlyCustomer2(String last, String first) {
super(last, first);
}
}
class Queue1<E> {
private LinkedList<E> list = new LinkedList<E>();
public void enqueue(E item) {
list.addLast(item);
}
public E dequeue() {
// return a Customer2 with null values if empty? (up to you)
return list.remove(0);
}
public E isNotEnd(){
return list.getLast();
}
public boolean hasItems() {
return !list.isEmpty();
}
public boolean isEmpty() {
return list.isEmpty();
}
public Iterator<E> iterator() {
return list.iterator();
}
public E removeFirst() {
return list.removeFirst();
}
public E getFirst() {
return list.getFirst();
}
public int size() {
return list.size();
}
public boolean hasNext() {
return false;
}
public void addItems(Queue1<? extends E> q) {
while (q.hasNext()) list.addLast(q.dequeue());
}
}
public class something {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input1;
String input2;
int choice = 1000;
Queue1<Customer2> empList;
empList = new Queue1<Customer2>();
Queue1<HourlyCustomer2> hList;
hList = new Queue1<HourlyCustomer2>();
do {
System.out.println("================");
System.out.println("Queue Operations Menu");
System.out.println("================");
System.out.println("1,Enquene");
System.out.println("2,Dequeue");
System.out.println("3,View queue");
System.out.println("0, Quit\n");
System.out.println("Enter Choice:");
try {
choice = sc.nextInt();
switch(choice) {
case 1:
System.out.println("\nPlease enter last name: ");
input1 = sc.next();
System.out.println("\nPlease enter first name: ");
input2 = sc.next();
hList.enqueue(new HourlyCustomer2(input1, input2));
empList.addItems(hList);
System.out.println("\n"+(input2 + " " + input1) + " is successful queued");
break;
case 2:
if (empList.isEmpty()) {
System.out.println("The queue is empty!");
}
else
{
System.out.println("\nDequeued customer: " +empList.getFirst());
empList.removeFirst();
}
System.out.println("\nNext customer in queue: " +empList.getFirst()+"\n");
break;
case 3:
System.out.println("\nThe Customer's names are: \n");
Iterator<Customer2> it = empList.iterator();
while (it.hasNext()) {
System.out.println("\nThe customers' names are: \n");
}
break;
case 0:
System.exit(0);
default:
System.out.println("Invalid choice");
}
}
catch(InputMismatchException e) {
System.out.println("Please enter 1-5, 0 to quit");
sc.nextLine();
}
} while(choice != 0);
}
}
你認爲我應該放入我的hasNext()方法嗎? – Rekt
@Rekt剛剛編輯帖子,以幫助完成。 –