package HW1;
import java.util.ArrayList;
public class Arrays {
public ArrayList Case() {
ArrayList <Case> cases = new ArrayList<Case>();
Case c1 = new Case("Antec Twelve Hundred V3", 20.20, 8.39, 22.87, "ATX Full Tower", 154.99);
Case c3 = new Case("BitPhenix Phenom M Arctic", 9.80, 12.95, 14.69, "MicroATX Mini Tower", 89.99);
Case c2 = new Case("Corsair 250D", 13.8, 10.9, 11.4, "MiniITX Tower", 99.9);
cases.add(c1);
cases.add(c2);
cases.add(c3);
for (int i = 0; i < cases.size(); i++) {
System.out.println("Name: " + cases.get(i).getName() + "\n" + "Dimensions: " + cases.get(i).getLength() + "' x " + cases.get(i).getWidth() + "' x " + cases.get(i).getHeight() + "' " + "Type: " +cases.get(i).getType() + "\n" + "Price: $" + cases.get(i).getPrice()+ "\n");
}
return cases;
}
public ArrayList Monitor() {
ArrayList <Monitor> monitor = new ArrayList<Monitor>();
Monitor m1 = new Monitor("Asus PB278Q", 27, 549.99);
Monitor m2 = new Monitor("Dell U2412M", 24, 263.99);
Monitor m3 = new Monitor("Samsung S22300H", 21.5, 139.99);
monitor.add(m1);
monitor.add(m2);
monitor.add(m3);
for (int i = 0; i < monitor.size(); i++) {
System.out.println("Name: " + monitor.get(i).getName() + "\n" + "Size: " + monitor.get(i).getSize() + "' " + "\n" + "Price: $" + monitor.get(i).getPrice() + "\n");
}
return monitor;
}
public ArrayList CPU() {
ArrayList <CPU> cpu = new ArrayList<CPU>();
CPU cpu1 = new CPU("AMDFX-8350", 4.0, 188.99);
CPU cpu2 = new CPU("Intel Core i5-3570K", 3.4, 179.99);
CPU cpu3 = new CPU("Intel Core i7-3770K", 3.5, 249.99);
cpu.add(cpu1);
cpu.add(cpu2);
cpu.add(cpu3);
for (int i = 0; i < cpu.size(); i++) {
System.out.println("Name: " + cpu.get(i).getName() + "\n" + "Speed: " + cpu.get(i).getSpeed() + "GHz " + "\n" + "Price: $" + cpu.get(i).getPrice() + "\n");
}
return cpu;
}
public ArrayList VideoCard() {
ArrayList <VideoCard> videoCard = new ArrayList<VideoCard>();
VideoCard vd1 = new VideoCard("Nvidia GeForce GTX 770", 2, 346.99);
VideoCard vd2 = new VideoCard("Nvidia GeForce GTX 670", 2, 333.99);
VideoCard vd3 = new VideoCard("AMD Radeon HD 7950", 3, 399.99);
videoCard.add(vd1);
videoCard.add(vd2);
videoCard.add(vd3);
for (int i = 0; i < videoCard.size(); i++) {
System.out.println("Name: " + videoCard.get(i).getName() + "\n" + "Memory Size: " + videoCard.get(i).getMemorySize() + "GB " + "\n" + "Price: $" + videoCard.get(i).getPrice() + "\n");
}
return videoCard;
}
public ArrayList HardDrive() {
ArrayList <HardDrive> hardDrive = new ArrayList<HardDrive>();
HardDrive hd1 = new HardDrive("Samsung MZ-7PD256BW", 256, 199.99);
HardDrive hd2 = new HardDrive("Corsair CSSD-F240GBGT-BK", 240, 206.99);
HardDrive hd3 = new HardDrive("Crucial CT128M4SSD1", 128, 199.95);
hardDrive.add(hd1);
hardDrive.add(hd2);
hardDrive.add(hd3);
for (int i = 0; i < hardDrive.size(); i++) {
System.out.println("Name: " + hardDrive.get(i).getName() + "\n" + "Memory Size: " + hardDrive.get(i).getSize() + "GB " + "\n" + "Price: $" + hardDrive.get(i).getPrice()+ "\n");
}
return hardDrive;
}
public ArrayList Memory() {
ArrayList <Memory> memory = new ArrayList<Memory>();
Memory me1 = new Memory("Corsair Vengeance", 8, 69.99);
Memory me2 = new Memory("G.Skill Ares Series", 16, 149.99);
Memory me3 = new Memory("Kingston Black Series", 8, 79.99);
memory.add(me1);
memory.add(me2);
memory.add(me3);
for (int i = 0; i < memory.size(); i++) {
System.out.println("Name: " + memory.get(i).getName() + "\n" + "Memory Size: " + memory.get(i).getSize() + "GB " + "\n" + "Price: $" + memory.get(i).getPrice()+"\n");
}
return memory;
}
}
比方說,我想訪問c3
的價格。在同樣的方法,我可以打印它,但我想訪問另一個類的主要方法c3
的價格。這是代碼。假設從菜單中,用戶輸入1
並決定購買第一個選項。我得到它來打印選項,但我不知道如何訪問它的價格。如何從一個類訪問ArrayList的特定部分到另一個類
package HW1;
import java.util.*;
public class Main {
public static void printMenu()
{
System.out.println("Menu");
System.out.println("1: Choose a Case");
System.out.println("2: Choose a Monitor");
System.out.println("3: Choose a CPU");
System.out.println("4: Choose a Video Card");
System.out.println("5: Choose a Hard Drive");
System.out.println("6: Choose a Memory");
System.out.println("7: Delete Current Computer");
System.out.println("8: Display Receipt");
System.out.println("9: Quit program");
}
public static void main(String[] args) {
printMenu();
System.out.println("please select and option");
Arrays arrays = new Arrays();
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
if (choice == 1) {
ArrayList cases = arrays.Case();
System.out.println(cases);
System.out.println("please select a case");
if (choice == 1) {
System.out.println(c1.getPrice());
}
}
}
}
好吧,我考慮了一些建議,並將Case()方法更改爲case1,因爲編譯器認爲它的情況。在你提供的if語句中,我得到2個錯誤,它說Case不能解析爲變量並刪除大小寫,這種情況也是無效的expresion – user3517896
Case是什麼包?你輸入了嗎? – MadProgrammer
我有像Case,Monitor等其他類,就像數組一樣,他們只是返回項目的名稱和價格所有的類都在同一個包中 – user3517896