2014-09-12 3 views
2

Je suis un débutant complet à la programmation Java, je veux créer dynamiquement des objets en Java pendant l'exécution, j'ai vérifié les formulaires et essayé du code mais rien ne semble vraiment fonctionner.créer dynamiquement des objets dans une boucle

voici mon code .. Toute aide est vraiment apprécié :)

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); 

    } 
    } 
    } 

La classe voitures ressemble à ceci ..

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; 
    } 


    } 

Répondre

0

Vous êtes sur la bonne voie, il y a quelques erreurs et des bits inutiles cependant.

La classe Voitures

Votre classe Cars a été la plupart du temps très bien, (mais à mon avis Car aurait été plus logique) mais votre constructeur n'a pas de sens, vous avez eu public void Cars(), void signifie « cette méthode ne retourne rien », mais vous voulez retourner un objet Cars, ce qui signifie votre constructeur doit devenir:

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

votre classe principale

Vous étiez très proche ici aussi, votre principal problème a été crée le tableau carslimit fois:

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

Le tableau doit être fait en dehors de la boucle for.

Voici la classe Main révisée dans son intégralité, les commentaires devraient expliquer assez bien ce que j'ai fait et pourquoi.

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 
    } 
} 

fini de taper ceci et réalisé que la question est de deux ans :)

Questions connexes