2016-11-13 51 views
0

代碼:打印在同一行的兩個值Java

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在同一行上。

+0

你看過'System.out'中的其他函數嗎? – SLaks

+0

在單個'println()'調用中串聯字符串的情況如何? –

回答

0

使用System.out.print()印刷cordinate X,然後使用System.out.println()打印cordinateý

0

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

它都會打印出一行。

+0

謝謝你的工作 –