0
我對Java很新,我想要做的是在存儲計算機零件商店中使用集合的可用產品的目錄中創建一個模型。我的老師詢問了目錄中每個產品的一個實例。這就是我想出了:現在ArrayList元素不打印
import java.util.*;
public class AvailablesCatalog {
public AvailablesCatalog(List cat1) {
cat1 = new ArrayList();
Motherboard item1 = new Motherboard("MD4652", 1995, "Lenovo", 100.50, "Intel", 32, 5);
CPU item2 = new CPU("MD4652", 1995, "Lenovo", 100.50, 2.9, 6);
Graphics item3 = new Graphics("MD4652", 1995, "Lenovo", 100.50, "AMD", 6);
RAM item4 = new RAM("MD4652", 1995, "Lenovo", 100.50, "DDR2", 4, 1600);
HD item5 = new HD("MD4652", 1995, "Lenovo", 100.50, "SSD", 2.5, 750);
Monitor item6 = new Monitor("MD4652", 1995, "Lenovo", 100.50, "LED", 17.5, "1920x1080", "HDMI");
Keyboard item7 = new Keyboard("MD4652", 1995, "Lenovo", 100.50, "Wireless");
Mouse item8 = new Mouse("MD4652", 1995, "Lenovo", 100.50, "Laser", "Wireless");
Printer item9 = new Printer("MD4652", 1995, "Lenovo", 100.50, "Laser", "Colored");
cat1.add(item1);
cat1.add(item2);
cat1.add(item3);
cat1.add(item4);
cat1.add(item5);
cat1.add(item6);
cat1.add(item7);
cat1.add(item8);
cat1.add(item9);
}
public String toString(List cat1, int i) {
for(i=0; i<cat1.size(); i++) {
System.out.println(cat1.get(i).toString());
}
return "----------------------------------------------------";
}
}
,通過店內的mainApp我使用到打印的目錄,我都存儲在一個變量,名爲AV的AvailablesCatalog對象類型的實例。這是主要的應用程序:
public class mainApp {
public static void main(String[] args){
/* Variables for Menu System and Sub Menu System */
int MainMenu;
String SubMenu;
String ReturnToMenu;
String SubMenuReturnToMenu;
List cat1 = new ArrayList();
AvailablesCatalog av = new AvailablesCatalog(cat1);
/* Displays menu system to console */
System.out.println("..............MENU...............");
System.out.println("..............1 View All Available Products..............");
System.out.println("..............2 View Orders...................");
System.out.println("..............3 View Sales...................");
System.out.println("..............0 Exit...................");
System.out.print("Please select an option: ");
Scanner sc = new Scanner(System.in);
MainMenu = sc.nextInt();
if(MainMenu == 1){
for(int i = 0; i < cat1.size(); i++) {
System.out.println(av.toString(cat1, i));
}
}
else if(MainMenu == 2) {
System.out.println("lol");
}
else if(MainMenu == 3) {
System.out.println("lol3");
}
else if(MainMenu == 4) {
System.exit(0);
}
}
}
一切順利編譯,當我運行mainApp菜單顯示正確。但是當我按1打印可用的產品目錄時,程序只是結束。選項2和3只是佔位符,現在順便說一句。提前致謝。
嘗試插入打印語句以查看if-branch之前cat1內部的內容 – Zavior