代码:
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在下一行。
发布于 2016-11-14 02:31:37
使用System.out.print()打印坐标X,然后使用System.out.println()打印坐标Y
发布于 2016-11-14 03:03:07
在Java语言中,println的意思是“打印行”。每次使用此命令时,Java都会在控制台中的一个新行上启动。
如果你不想每次都从新的一行开始,你可以使用System.out.print()。然后,Java将在同一行上打印这两个文件。
System.out.print(currentX);
System.out.print(currentY);或者,您也可以只对这两个变量运行一条println语句:
System.out.println("Current X: " + currentX + "; Current Y: " + currentY);它将在一行中打印出来。
https://stackoverflow.com/questions/40577258
复制相似问题