簡體   English   中英

將兩個值打印在同一行上

[英]printing the both values on the same line 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。

使用System.out.print()打印坐標X,然后使用System.out.println()打印坐標Y

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

並將全部打印在一行上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM