嗨我真的需要我的程序帮助,我已经搜索了高和低的解决方案,但似乎无法找到我在找什么。以前的输入被新输入覆盖
我正在制作一个程序,用户添加一个桌面,为其输入各种信息,然后将其添加到数组列表中。
下面的代码:
简介:
Scanner scan = new Scanner(System.in);
String input;
boolean looper = true;
DecimalFormat f = new DecimalFormat("#.00");
ArrayList<Desktop> desktopList = new ArrayList<>();
ArrayList<Laptop> laptopList = new ArrayList<>();
while (looper) {
System.out.println("");
System.out.println("******************* Artificial Intelligence Co. *************************");
System.out.println("1. Add Information for new Desktop");
System.out.println("2. Add Information for new Laptop");
System.out.println("3. Display all computer information");
System.out.println("4. Quit");
System.out.println("5. Credits");
System.out.println("*************************************************************************");
switch语句和案例1:
switch (input) {
case "1":
System.out.println("");
System.out.println("=========================================================================");
System.out.println("Information for new Desktop");
System.out.println("=========================================================================");
Desktop xx = new Desktop();
boolean loop = true;
while (loop) {
System.out.print("What is the Computer ID: ");
xx.setComputerID(scan.nextLine().toUpperCase());
if ((xx.getComputerID().startsWith("D")) && (xx.getComputerID().length() == 4)) {
loop = false;
} else {
System.out.println("Computer ID should start with a letter \"D\". and have 4 characters.");
System.out.println("");
}
}
loop = true;
while (loop) {
System.out.print("What is the Processor Speed: ");
xx.setCPUspeed(scan.nextLine().toUpperCase());
try {
if (StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 2))) ||
StringisDouble(xx.getCPUspeed().substring(0, (xx.getCPUspeed().length() - 3)))) { //checks the value before GHZ or HZ if its a double
if (xx.getCPUspeed().endsWith("GHZ") || xx.getCPUspeed().endsWith("HZ")) {
loop = false;
} else {
System.out.println("CPU Speed input should end with \"GHZ\" or \"HZ\".");
System.out.println("");
}
} else {
System.out.println("CPU Speed input should contain a decimal or number followed by a \"GHZ\" or a \"HZ\".");
System.out.println("");
}
} catch (StringIndexOutOfBoundsException e) {
System.out.println("CPU Speed input should contain a decimal or number followed by a \"GHZ\" or a \"HZ\".");
System.out.println("");
}
}
loop = true;
while (loop) {
System.out.print("What is the RAM: ");
xx.setRAM(scan.nextLine().toUpperCase());
try {
if (StringisInteger(xx.getRAM().substring(0, (xx.getRAM().length() - 2)))) { //checks the value if it is numeric and ending with GB or MB
if (xx.getRAM().endsWith("GB") || xx.getRAM().endsWith("MB")) {
loop = false;
} else {
System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
System.out.println("");
}
} else {
System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
System.out.println("");
}
} catch (StringIndexOutOfBoundsException e) {
System.out.println("RAM input should have a numeric value and end with \"GB\" or \"MB\".");
System.out.println("");
}
}
loop = true;
while (loop) {
System.out.print("What is the Harddisk size: ");
xx.setHarddisk(scan.nextLine().toUpperCase());
try {
if (StringisInteger(xx.getHarddisk().substring(0, (xx.getHarddisk().length() - 2)))) { //checks the value if it is numeric and ending with GB or MB
if (xx.getHarddisk().endsWith("GB") || xx.getHarddisk().endsWith("TB")) {
loop = false;
} else {
System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
System.out.println("");
}
} else {
System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
System.out.println("");
}
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Harddisk input should have a numeric value and end with \"GB\" or \"TB\".");
System.out.println("");
}
}
loop = true;
while (loop) {
System.out.print("What is the Monitor: ");
xx.setMonitor(scan.nextLine().toUpperCase());
if (xx.getMonitor().equals("CRT") || xx.getMonitor().equals("LCD")) {
loop = false;
} else {
System.out.println("Please enter in CRT or LCD only.");
System.out.println("");
}
}
loop = true;
while (loop) {
try {
System.out.print("What is the price: $");
xx.setPrice(Double.parseDouble(scan.nextLine()));
loop = false;
} catch (NumberFormatException e) {
System.out.println("Price input should be numeric.");
System.out.println("");
}
}
desktopList.add(xx);
System.out.println("Information successfully added.");
System.out.println("");
System.out.println("");
break;
案例3,在用户获取看到他/她进入什么:
case "3":
int DesktopCounter = 1;
int LaptopCounter = 1;
System.out.println("");
if (desktopList.isEmpty()) {
System.out.println("No desktop added!");
System.out.println("");
} else {
for (int i = 0; i < desktopList.size(); i++) {
System.out.println("");
System.out.println("Desktop " + DesktopCounter);
System.out.println("Computer ID: " + desktopList.get(i).getComputerID());
System.out.println("Processor Speed: " + desktopList.get(i).getCPUspeed());
System.out.println("RAM: " + desktopList.get(i).getRAM());
System.out.println("Harddisk:" + desktopList.get(i).getHarddisk());
System.out.println("Monitor: " + desktopList.get(i).getMonitor());
System.out.println("Price: $" + f.format(desktopList.get(i).getPrice()));
DesktopCounter++;
}
}
break;
桌面级:
public class Desktop extends Computer //Child class of Computer
{
private static String Monitor;
public Desktop()
{
ComputerID = "-- No ID specified --";
CPUspeed = "-- No processor speed specified --";
RAM = "-- No RAM specified-";
Harddisk = "-- No Harddisk size specified --";
Monitor = "-- No Monitor specified --";
Price = 0.0;
}
//Setters and Getters
public String getMonitor()
{
return Monitor;
}
public void setMonitor(String monitor)
{
Monitor = monitor;
}
}
计算机类:
- 计算机ID:
public class Computer //Parent class { protected static String ComputerID; protected static String CPUspeed; protected static String RAM; protected static String Harddisk; protected static double Price; public Computer() //Initializer { ComputerID = "-- No ID specified --"; CPUspeed = "-- No processor speed specified --"; RAM = "-- No amount RAM specified-"; Harddisk = "-- No Harddisk size specified --"; Price = 0.0; } public Computer(String computerID, String cpuspeed, String ram, String harddisk, double price) { ComputerID = computerID; CPUspeed = cpuspeed; RAM = ram; Harddisk = harddisk; Price = price; } //Getters and Setters public String getComputerID() { return ComputerID; } public void setComputerID(String computerID) { ComputerID = computerID; } public String getCPUspeed() { return CPUspeed; } public void setCPUspeed(String cpuspeed) { CPUspeed = cpuspeed; } public String getRAM() { return RAM; } public void setRAM(String ram) { RAM = ram; } public String getHarddisk() { return Harddisk; } public void setHarddisk(String harddisk) { Harddisk = harddisk; } public double getPrice() { return Price; } public void setPrice(double price) { Price = price; } //End of getters and setters
}
现在说,如果我添加使用情况1以下信息桌面中输入:D001
- 处理器速度:3.2GHZ
- RAM:512MB
- 硬盘:80GB
- 显示器:CRT
,然后继续添加其他桌面这些:
- 计算机ID:D123
- 处理器速度: 4.4GHZ
- RAM:8GB
- 硬盘: 1TB
- 显示器:LCD
当我显示使用外壳3码块中的信息时,其输出:
- 桌面1
- 计算机ID:D123
- 处理器速度:4 。4GHZ
- 内存:8GB
- 硬盘:1TB
- 显示器:液晶
- 桌面2
- 计算机ID:D123
- 处理器速度:4.4GHZ
- RAM:8GB
- 硬盘:1TB
- 显示器:液晶
当权,桌面1应该显示自己的独特属性。
我将不胜感激任何帮助。
编辑:我通过让我的变量非静态来解决这个问题。经过额外的代码
你可以把完整的代码,而不是代码片段,这样很容易找到变量/引用正在初始化和循环打破。 –