Deplacer mon ocean et poisson
matt2421
Messages postés
17
Statut
Membre
-
KX Messages postés 19031 Statut Modérateur -
KX Messages postés 19031 Statut Modérateur -
salut! Voici trois classes que j'ai cree via Eclipse (Fish, ocean ,oceanlifemain) et je n'arrive pas a deplacer mon ocean et mon poisson de 5++.
besoin d'aide svp! Il y a un peu d'allemand dedans mais c'est pas important!
besoin d'aide svp! Il y a un peu d'allemand dedans mais c'est pas important!
package infpp.oceanlife;
public class Fish {
private int x;
private int y;
private String name;
public Fish(int x, int y, String name) {
this.x = x;
this.y = y;
this.name = name;
}
public int getX() {
return this.x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
*
* Rückgabewert eines Fisches
*
*/
public String toString() {
return "Fish [x=" + x + ", y=" + y + ", name=" + name + "]";
}
}
package infpp.oceanlife;
public class Ocean {
private int width;
private int depth;
private Fish fish;
public Ocean (int width, int depth, Fish fish) {
this.width = width;
this.depth = depth;
this.fish = fish;
}
public int getwidth () {
return this.width;
}
public void setwidth (int width) {
this.width = width;
}
public int getdepth () {
return this.depth;
}
public void setdepth (int depth) {
this.depth = depth;
}
public Fish getfish () {
return this.fish;
}
public void setfish (Fish fish) {
this.fish = fish;
}
public void move () {
int H = fish.getX();
int V = fish.getY();
System.out.println("Die aktuell position von dem Fische " + H + " " + V);
}
public String toString () {
return "Ocean [width = " + width + ", depth = " + depth + ", fish = " + fish + "]";
}
}
package infpp.oceanlife;
public class OceanLifeMain {
public static void main(String[] args) {
int width = 1024;
int depth = 768;
Ocean ocean = new Ocean(width, depth, new Fish(4 , 6 , "nemo"));
for (int i = 0; i < 5; i++) {
ocean.move ();
System.out.println(ocean);
}
}
}
A voir également:
- Deplacer mon ocean et poisson
- Déplacer colonne excel - Guide
- Symbole poisson voiture - Accueil - Maison
- Déplacer barre des taches windows 11 - Guide
- Déplacer dossier onedrive - Guide
- Deplacer icone sur iphone - Guide
Par exemple, si tu veux aller à droite (x+1) tu peux faire :
public void move() { int previousX = fish.getX(); int previousY = fish.getY(); int nextX = previousX + 1; int nextY = previousY; fish.setX(nextX); fish.setY(nextY); System.out.println("Der Fisch bewegt von (x="+previousX+",y="+previousY +") bis (x="+nextX+",y="+nextY+")"); }Remarque : je pense que c'est encore mieux de passer deux paramètres à la méthode move pour savoir de combien il faut se déplacer sur chaque axe.
public void move(int deltaX, int deltaY) { int previousX = fish.getX(); int previousY = fish.getY(); int nextX = previousX + deltaX; int nextY = previousY + deltaY; fish.setX(nextX); fish.setY(nextY); System.out.println("Der Fisch bewegt von (x="+previousX+",y="+previousY +") bis (x="+nextX+",y="+nextY+")"); }Que signifie ce fish.setX (next X); et ne pourrait -on pas l'ecrire de la meme forme que int previousX = fish.getX ()? Du genre int nextX =fish.setX (nextX); ?
Merci encore!!!!!!
public int getX() { return this.x; } public void setX(int x) { this.x = x; }