2016-05-13 73 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