1
ユーザー定義量オブジェクトを格納するユーザー定義デューケを作成しようとしています。アイテムオブジェクトを作成し、そのオブジェクトを両端キューに格納するよりも値またはアイテム番号と価格を指定します。これまでのところ、私のメインは単品で構成されています。アイテムを設定するたびに、以前に保存されたアイテムの値が最新のものと書き込まれます。どのように私はそれらをすべて保存するために不確定な量のアイテムを作成することができますか?あなたが1で行われているし、あなたのキューに追加した後、または場所のいずれかでデキューに入れるオブジェクトを無期限に作成する
item1= new Item();
を追加する必要があります
メイン
import java.*;
import java.util.*;
public class MyDequeApp {
public static void main(String[] args)
{
//variables
String userinNum;
double userinPrice;
int queOp;
//creating new Item
Item item1= new Item();
//creating new Scanner
Scanner scan1=new Scanner(System.in);
//user input number of elements in the deque
System.out.println("Enter the number of elements in the que");
int queElm=scan1.nextInt();
MyDeque theQueue=new MyDeque(queElm);
//do/while so while user selects 1-7 they stay in the switch/case
do
{
//switch/case menu
System.out.println("1. Insert to front");
System.out.println("2. Insert to rear");
System.out.println("3. Remove from front");
System.out.println("4. Remove from rear");
System.out.println("5. Peek front");
System.out.println("6. Peek rear");
System.out.println("7. Display deque");
System.out.println("Anything else to Quit");
//user input the case number
queOp=scan1.nextInt();
scan1.nextLine();
switch(queOp)
{
//insert to front
case 1:
System.out.println("Enter an item number");
userinNum=scan1.nextLine();
item1.setNum(userinNum);
System.out.println("Enter a price");
userinPrice=scan1.nextDouble();
scan1.nextLine();
item1.setPrice(userinPrice);
System.out.println(item1.toString());
theQueue.insertFront(item1);
break;
//insert to rear
case 2:
System.out.println("Enter an item numbeR");
userinNum=scan1.nextLine();
item1.setNum(userinNum);
System.out.println("Enter a pricE");
userinPrice=scan1.nextDouble();
scan1.nextLine();
item1.setPrice(userinPrice);
System.out.println(item1.toString());
theQueue.insertFront(item1);
theQueue.insertRear(item1);
break;
//remove from front
case 3:
theQueue.removeFront();
break;
//remove from rear
case 4:
theQueue.removeRear();
break;
//peek front
case 5:
System.out.println(theQueue.peekFront());
break;
//peek rear
case 6:
System.out.println(theQueue.peekRear());
break;
//display the deque
case 7:
System.out.println(theQueue.toString());
break;
//quit
default:
break;
}
}while(queOp>0 && queOp<8);
}
}
まだ少し不明です。 item2、item3、item4などを宣言する際に追加することを意味しますか?またはアイテム(変数)を使用していますか?あなたは精緻化できますか? – Dan
またはおそらく増分ですか? – Dan
あなたのコードにすでに書かれている方法だけですが、その部分をループ内に移動してループが新しい項目を作成する必要があります。混乱している部分は、item1は実際にはループを通過するたびに異なる項目になりますが、ループ内で同じ名前を持ち、キュー内の別のオブジェクトであるということです。 –