2016-05-13 71 views
-2

我對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循環?

+1

代碼在哪裏?閱讀:[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) –

+2

發佈您已有的代碼的相關部分並解釋您的具體問題。 –

回答

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; 

然後在循環中,您可以運行它喜歡你:

else if(Integer.parseInt(response) == (3)){ 
    for(int i = 0; i < arrLaptop.length; i++) { 
    arrLaptop[i].displayInfo(); 
    } 
}//else if 3 

我建議,如果你想更好地瞭解您看看一些基本的面向對象編程教程。

+0

我想知道我應該把代碼放在哪裏。但我不能複製和粘貼我的代碼,因爲它很長,網站不允許我。 – NewbiewinCoding

+0

您需要在每行代碼之前放置四個空格以使其成爲代碼塊。看[編輯幫助](http://stackoverflow.com/editing-help)查看格式化信息。至於放置for循環的位置,取決於您的代碼。 – YamSMit

+0

完成。 // 2short。 – NewbiewinCoding