2014-09-12 92 views
2

我是Java編程的完全新手,我想在運行時動態創建Java對象,我已經檢查了表單並嘗試了一些代碼,但似乎沒有任何工作。在循環中動態創建對象

這裏是我的代碼..所有的幫助是非常讚賞:)

import java.util.Scanner; 

    public class Main{ 
    public static void main(String[] args){ 
    String carName; 
    String carType; 
    String engineType; 
    int limit; 

    Scanner in = new Scanner(System.in); 
    System.out.print("Enter the number of Cars you want to add - "); 
    limit = in.nextInt(); 

    for(int i = 0; i <limit; i++){ 

    Cars cars[i] = new Cars(); 

    System.out.print("Enter the number of Car Name - "); 
    carName = in.nextLine(); 

    System.out.print("Enter the number of Car Type - "); 
    carType = in.nextLine(); 

    System.out.print("Enter the Engine Type - "); 
    engineType = in.nextLine(); 

    cars[i].setCarName(carName); 
    cars[i].setCarType(carType); 
    cars[i].setEngineeSize(engineType); 
    String a = cars[i].getCarName(); 
    String b = cars[i].getCarType(); 
    String c = cars[i].getEngineeSize(); 
    System.out.println(a,b,c); 

    } 
    } 
    } 

汽車類看起來是這樣的..

public class Cars{ 
    public String carName; 
    public String carType; 
    public String engineeSize; 

    public void Cars(){ 
    System.out.println("The Cars constructor was created ! :-) "); 
    } 

    public void setCarName(String cn){ 
    this.carName = cn; 
    } 

    public void setCarType(String ct){ 
    this.carType = ct; 

    } 

    public void setEngineeSize(String es){ 
    this.engineeSize = es; 

    } 

    public String getCarName(){ 
    return this.carName; 
    } 



    public String getCarType(){ 
    return this.carType; 
    } 

    public String getEngineeSize(){ 
    return this.engineeSize; 
    } 


    } 

回答

0

你在正確的軌道上,但有一些錯誤和不必要的位。

汽車類

你的汽車類主要是罰款,(雖然在我看來Car會更有意義),但是你的構造並沒有什麼意義,你有public void Cars()void的意思是「這個方法沒有返回」,但要返回一個Cars對象,這意味着你的構造需要成爲:

public Cars() 
{ 
    System.out.println("The Cars constructor was created ! :-) "); 
} 

主類

你是非常接近的位置爲好,你的首要問題是創建cars陣列limit時間:

for(int i = 0; i < limit; i++) 
{ 
    Cars cars[i] = new Cars(); 
    //Other code 
} 

陣列需要for環路外進行。

下面是全部修訂的Main班,評論應該很好地解釋我做了什麼以及爲什麼。

import java.util.Scanner; 

public class Main{ 

public static void main(String[] args){ 
    //The strings here were unnecessary 
    int limit; 

    Scanner in = new Scanner(System.in); 

    System.out.print("Enter the number of Cars you want to add - "); 
    limit = in.nextInt(); 
    in.nextLine(); //nextInt leaves a newLine, this will clear it, it's a little strange, but it makes sense seeing as integers can't have newlines at the end 

    //Make an array of Cars, the length of this array is limit 
    Cars[] cars = new Cars[limit]; 

    //Iterate over array cars 
    for(int i = 0; i < limit; i++) 
    { 
     //Read all the properties into strings 
     System.out.println("Enter the number of Car Name - "); 
     String carName = in.nextLine(); 

     System.out.println("Enter the number of Car Type - "); 
     String carType = in.nextLine(); 

     System.out.println("Enter the Engine Type - "); 
     String engineType = in.nextLine(); 

     //Set the object at current position to be a new Cars 
     cars[i] = new Cars(); 

     //Adjust the properties of the Cars at this position 
     cars[i].setCarName(carName); 
     cars[i].setCarType(carType); 
     cars[i].setEngineeSize(engineType); 

     //We still have the variables from the scanner, so we don;t need to read them from the Cars object 
     System.out.println(carName+carType+engineType); 
    } 
    in.close(); //We don't need the scanner anymore 
    } 
} 

打完這一點,並意識到問題是兩歲:)