首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将这两个值打印到同一行java

将这两个值打印到同一行java
EN

Stack Overflow用户
提问于 2016-11-14 02:26:13
回答 2查看 1.1K关注 0票数 0

代码:

代码语言:javascript
运行
复制
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以在同一行上打印两张照片?

代码语言:javascript
运行
复制
int currentX=coordinates.getXCoordinate(); 
int currentY=coordinates.getYCoordinate();

System.out.println(currentX);
System.out.printlncurrentY);

所以我希望x,y在同一条线上,而不是x和y在下一行。

EN

回答 2

Stack Overflow用户

发布于 2016-11-14 02:31:37

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

票数 0
EN

Stack Overflow用户

发布于 2016-11-14 03:03:07

在Java语言中,println的意思是“打印行”。每次使用此命令时,Java都会在控制台中的一个新行上启动。

如果你不想每次都从新的一行开始,你可以使用System.out.print()。然后,Java将在同一行上打印这两个文件。

代码语言:javascript
运行
复制
System.out.print(currentX);
System.out.print(currentY);

或者,您也可以只对这两个变量运行一条println语句:

代码语言:javascript
运行
复制
System.out.println("Current X: " + currentX + "; Current Y: " + currentY);

它将在一行中打印出来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40577258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档