2016-11-13 3 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 Y

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

спасибо, что это сработало –

Смежные вопросы