içine koymak için belirsiz miktarda nesne oluşturma Kullanıcı tanımlı miktar nesneleri depolayan kullanıcı tanımlı bir deque oluşturmaya çalışıyorum. Bir nesne nesnesi yaratıyorum ve nesneyi bir deque deposunda saklamak yerine, değerleri veya item # ve fiyatını veriyorum. Şimdiye kadar benim ana tekil bir maddeden oluşur. Öğeyi her ayarladığımda, önceden depolanmış öğe değerlerini en son ile yazar. Hepsini depolamak için belirsiz miktarda öğe oluşturmaya nasıl gidebilirim?Bir deque
Ana
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);
}
}
Hala biraz belirsizim. Demek istediğiniz madde2, item3, item 4 vb. veya bir Öğe (değişken) ile? Detaylandırır mısın? – Dan
veya muhtemelen artıyor mu? – Dan
kodunuzda zaten nasıl yazıldığını, ancak bu parçayı da döngü içerisine taşımanız gerekir, böylece döngü de yeni öğeyi yaratır. Buradaki kafa karıştırıcı kısım, öğe1'in döngü boyunca her seferinde farklı öğeler olacağı, ancak kuyruğunuzda farklı bir nesne iken, döngüde aynı adı taşıyacağıdır. –