public class Aeroplane {
private String name;
private Coordinates coordinates;
private int speed;
private int totalDistance;
private int repairDistance;
public Aeroplane(String name, int xcoordinate, int ycoordinate, int speed, int totalDistance, int repairDistance) {
this.name= name;
coordinates=new Coordinates(xcoordinate,ycoordinate);
this.speed=speed;
this.totalDistance = totalDistance;
this.repairDistance= repairDistance;
}
public void singleFlight(Destination destination ) {
// Some temp vars to hold
int tempX=0;
int tempY=0;
// Hold current x,y coordinates
int currentX=coordinates.getXCoordinate(); // Get X coordinate from plane coord object
int currentY=coordinates.getYCoordinate(); // Get Y coord
// Hold the Desinationation coordinates -
int destinationX=destination.getXCoordinate();
int destinationY=destination.getYCoordinate();
// Print Start Coordinates here
System.out.println(currentX);
System.out.println(currentY);
// While loop
while(currentX!=destinationX || currentY!=destinationY) // So as long as either the x,y coord of the current
// planes position do not equal the destination coord, then keep going
{
// Get difference btn currentX and destination
tempX=destinationX-currentX;
if (tempX<speed) {
currentX+=speed; // Increment current position by speed
}
else{
currentX+=tempX; // Increment speed by remaining distance.
}
// Same for y coord
tempY=destinationY-currentY;
if (tempY<speed) {
currentY+=speed;
}
else {
currentY+=tempY;
}
// Print current x, y coord here
}
// Print final destionation here
}
public void setname (String name) {
this.name = name;
}
public String getname() {
return name;
}
}
我如何改變的println,使在同一行的兩個印記?
int currentX=coordinates.getXCoordinate();
int currentY=coordinates.getYCoordinate();
System.out.println(currentX);
System.out.printlncurrentY);
所以不是x,然後y在下一行,我想x,y在同一行上。
你看過'System.out'中的其他函數嗎? – SLaks
在單個'println()'調用中串聯字符串的情況如何? –