我對Java很新,我有一個即將到期的項目。如何顯示我已保存在數組中的信息?
除了在工作中存儲和顯示信息之外,我還做了其他所有工作。
我該怎麼做?
首先,我有4個班
1號是被命名爲「計算機」之類的。它存儲所有的變量。
public class Computer {
private String computerID;
private String processorSpeed;
private String ram;
private String hardDisk;
public double price;
public Computer(String computerID,String processorSpeed,String ram,String hardDisk){
this.computerID = computerID;
this.processorSpeed = processorSpeed;
this.ram = ram;
this.hardDisk = hardDisk;
}//Constructor
public String getComputerID(){
return computerID;
}//getComputerID
public String getProcessorSpeed(){
return processorSpeed;
}//getProcessorSpeed
public String getRam(){
return ram;
}//getRam
public String getHardDisk(){
return hardDisk;
}//getHardDisk
}//class Computer
2號是名爲「計算機演示」的類。它將顯示我寫的所有文本,並且是主要類。但我不知道在哪裏放置for循環來顯示存儲的信息。
import java.util.Scanner;
public class ComputerDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String response;
int countDesktop = 1;// d1
int countdeskid = 1;//desktop ID
int countdeskpspd = 1;// array deskpspd
int countdeskram = 1;// dekstop ram
int countdeskdisk = 1;// desktop disk
int countdeskmonitor = 1;// desktop monitor
Desktop[] d1 = new Desktop[countDesktop];
int countLaptop = 1; // l1
int countlaptopid = 1 ; // laptop ID
int countlaptoppspd = 1 ; // array deskspspd
int countlaptopram = 1 ; // laptop ram
int countlaptopdisk = 1 ; // laptop disk
int countlaptopweight = 1 ; // laptop weight
Laptop[] l1 = new Laptop[countLaptop] ;
String[] deskid = new String[countdeskid];
String[] deskpspd = new String[countdeskpspd];
String[] deskram = new String[countdeskram];
String[] deskdisk = new String[countdeskdisk];
String[] deskmonitor = new String[countdeskmonitor];
do {
System.out.println("************************ Artificial Intelligence Co. ************************");
System.out.println("Computer Menu:");
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("*****************************************************************************");
System.out.print("Please enter either 1 to 4: ");
response = input.nextLine();
System.out.println("");
try{
if(Integer.parseInt(response) == (1)){
System.out.println("=============================================================================================");
System.out.println("Information for new Desktop");
System.out.println("=============================================================================================");
System.out.print("What is the computer ID : ");
deskid[countdeskid-1] = input.nextLine();
countdeskid++;
System.out.print("What is the Processor Speed : ");
deskpspd[countdeskpspd-1] = input.nextLine();
countdeskpspd++;
System.out.print("What is the RAM : ");
deskram[countdeskram-1] = input.nextLine();
countdeskram++;
System.out.print("What is the Harddisk size : ");
deskdisk[countdeskdisk-1] = input.nextLine();
countdeskdisk++;
System.out.print("What is the Monitor Type : ");
deskmonitor[countdeskmonitor-1] = input.nextLine();
System.out.println("What is the Price : ");
response = input.nextLine();
System.out.println("");
System.out.println("Your information has been added successfully.");
System.out.println("");
String[] laptopid = new String [countlaptopid] ;
String[] laptoppspd = new String [countlaptoppspd] ;
String[] laptopram = new String [countlaptopram] ;
String[] laptopdisk = new String [countlaptopdisk] ;
String[] laptopweight = new String [countlaptopweight] ;
System.out.println("************************ Artificial Intelligence Co. ************************");
System.out.println("Computer Menu:");
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("*****************************************************************************");
System.out.print("Please enter either 1 to 4: ");
response = input.nextLine();
System.out.println("");
if(Integer.parseInt(response) == (2)){
System.out.println("=============================================================================================");
System.out.println("Information for new Laptop");
System.out.println("=============================================================================================");
System.out.print("What is the computer ID : ");
laptopid[countlaptopid-1] = input.nextLine();
countlaptopid++;
System.out.print("What is the Processor Speed : ");
laptoppspd[countlaptoppspd-1] = input.nextLine();
countlaptoppspd++;
System.out.print("What is the RAM : ");
laptopram[countlaptopram-1] = input.nextLine();
countlaptopram++;
System.out.print("What is the Harddisk size : ");
laptopdisk[countlaptopdisk-1] = input.nextLine();
countlaptopdisk++;
System.out.print("What is the weight : ");
laptopweight[countlaptopweight-1] = input.nextLine();
System.out.println("What is the Price : ");
response = input.nextLine();
System.out.println("");
System.out.println("Your information has been added successfully.");
System.out.println("");
}//else if 2
else if(Integer.parseInt(response) == (3)){
}//else if 3
else if(Integer.parseInt(response) == (4)){
break;
}//else if 4
}
}//try
catch(Exception e){
}//catch
}while(true);
}
}//class ComputerDemo
最後2只是一個子類,它在顯示代碼時有額外的信息。
public class Desktop extends Computer {
String monitorType;
public Desktop(String monitor, String computerID,String processorSpeed,String ram,String hardDisk){
super(computerID,processorSpeed,ram,hardDisk);
this.monitorType = monitorType;
}//constructor
public void displayInfo(){
System.out.println("Computer ID : " + getComputerID());
System.out.println("Processor Speed : " + getProcessorSpeed());
System.out.println("RAM : " + getRam());
System.out.println("Harddisk : " + getHardDisk());
System.out.println("Monitor : " + monitorType);
System.out.println("Price : " + price);
}
}
和
public class Laptop extends Computer {
String weight;
public Laptop(String monitor, String computerID,String processorSpeed,String ram,String hardDisk){
super(computerID,processorSpeed,ram,hardDisk);
this.weight = weight;
}//constructor
public void displayInfo(){
System.out.println("Computer ID : " + getComputerID());
System.out.println("Processor Speed : " + getProcessorSpeed());
System.out.println("RAM : " + getRam());
System.out.println("Harddisk : " + getHardDisk());
System.out.println("Weight : " + weight);
System.out.println("Price : " + price);
}
} //Laptop
讓我怎麼顯示使用循環的代碼?我應該在哪裏放置for循環?
代碼在哪裏?閱讀:[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –
發佈您已有的代碼的相關部分並解釋您的具體問題。 –