我非常非常熟悉Java,我有一个即将到期的项目。
除了在工作中存储和显示信息之外,我还做了其他所有事情。
我该怎么做?
首先,我有4个班级
数字1是名为"的类。电脑" 。它存储所有变量。
select DATEDIFF(second, EventStartTimeStamp_UTC0, EventEndTimeStamp_UTC0) as EventDuration
from allEvent
where DATEDIFF(Minute, EventStartTimeStamp_UTC0, EventEndTimeStamp_UTC0) < 1
数字2是名为&#34;计算机演示&#34; 。它将显示我写的所有文本,并且是主类。但我不知道在哪里放置for循环来显示存储的信息。
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只是一个子类,它在显示代码时会有额外的信息。
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
和
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);
}
}
那么我如何使用for循环显示代码?我应该把for循环放在哪里?
答案 0 :(得分:1)
在不知道数据是什么或者您正在尝试做什么的情况下,要在数组中显示信息,您需要使用循环。
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
编辑:
首先,我要更改您的信息设置,因为您的类包含了您未使用的构造函数,字段和方法。从用户获取信息后,您应该将其放入类构造函数中,然后将新对象添加到数组中。即:
Laptop[] arrLaptop = new Laptop[count];
Laptop newLaptop = new Laptop(monitor, computerID, processorSpeed, ram, hardDisk);
arrLaptop[i] = newLaptop;
然后在你的for循环中你可以运行它:
else if(Integer.parseInt(response) == (3)){
for(int i = 0; i < arrLaptop.length; i++) {
arrLaptop[i].displayInfo();
}
}//else if 3
如果您想要更好地理解,我建议您查看一些基本的面向对象编程教程。