2016-11-13 12 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; 
    } 

} 

どのように私と同じ、ライン上の2枚の印刷物を作るための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

1つの 'println()'呼び出しの中で文字列を連結するのはどうですか? –

答えて

0
印刷コーディネートXの

使用System.out.print()、次いでジャワ、println手段「印刷ライン」でコーディネートY

0

を印刷するSystem.out.println()を使用します。これを使うたびに、Javaはコンソールの新しい行から開始します。

毎回新しい行で開始したくない場合は、System.out.print()を使用してください。 Javaは両方とも同じ行に出力します。

System.out.println("Current X: " + currentX + "; Current Y: " + currentY); 

をそして、それはすべて1行に出力します:

System.out.print(currentX); 
System.out.print(currentY); 

また、あなたはまた、単に両方の変数のために1つのprintlnのステートメントを実行することができます。

+0

ありがとうございました –

関連する問題