2
Hello, I’m doing exercises on constructors in Java but I’m having a question.
I have two classes
Main java.
public class Main {
public static void main(String[] args) {
Duck[] d = new Duck[5];
d[0] = new Duck();
d[1] = new Duck();
d[2] = new Duck();
d[3] = new Duck();
d[4] = new Duck();
}
}
Duck.java
public class Duck {
private int amountOfDucks;
public Duck() {
amountOfDucks++;
if(amountOfDucks >= 1 && amountOfDucks <= 3) {
System.out.println("Ha poucos patos na lagoa. Quack!");
}
else if (amountOfDucks >= 4 && amountOfDucks <= 6) {
System.out.println("Ha alguns patos na lagoa. " +
"Quack! Quack!");
}
else {
System.out.println("Ha muitos patos na lagoa!! " +
"Quack! Quack! Quack! Quack...");
}
System.out.println("Quantidade de Patos: "
+ amountOfDucks + "\n");
}
}
I have the way out
There are few ducks in the pond. Quack!
Quantity of Ducks: 1There are few ducks in the pond. Quack!
Quantity of Ducks: 1There are few ducks in the pond. Quack!
Quantity of Ducks: 1There are few ducks in the pond. Quack!
Quantity of Ducks: 1There are few ducks in the pond. Quack!
Quantity of Ducks: 1
How do I get an out with the number of ducks amountOfDucks
increasing and not just 1?
That’s right, thank you!
– Bruno Neuman