我必須編寫一個程序來創建一個字符串隊列。它要求用戶輸入一個數字n,他必須在隊列中輸入n個名字。然後,直到隊列是空的,該程序Java:從隊列中刪除指定數量的元素
- 顯示隊列
- 的頂部的名稱詢問要刪除的數名的用戶。
- 刪除名的數量指定
- 顯示的名稱(一個或多個)中刪除
該程序只能使用該加載(),刪除(),的isEmpty()和元件()方法。 以下是我想出迄今:
package lesson1;
import java.util.*;
public class MyClass1{
public static void main(String[] args) {
Queue <String> strings= new LinkedList<String>();
Scanner input= new Scanner(System.in);
System.out.println("Please enter the number of names, n.");
int n= input.nextInt();
System.out.println("Please enter " +n+ " names");
for(int i=0;i<n; i++){
strings.add(input.next());
}
System.out.println("\nDisplaying the names:\n");
for(String object: strings){
System.out.println(object);
}
while(!strings.isEmpty()){
System.out.println("The name in front of the queue is: " + strings.element());
System.out.println("Please enter the number of names to be deleted:");
int del= input.nextInt();
for(int i=0; i<del; i++){
System.out.println("Name removed:"+strings.remove(i));
}
}
}
}
的問題是,它是用於印刷被刪除,因爲刪除名稱假()是一個布爾方法。我怎樣才能解決這個問題?
這是一個功課題嗎? – Khalos
remove返回已移除的元素。它不返回布爾值。 – Eran
@Eran這是返回_false_名稱刪除: – Tia