我对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) –
发布您已有的代码的相关部分并解释您的具体问题。 –