有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在main中调用方法,以便它们在同一行上输出?

我试图构建一个图像,我希望所有的方法都在同一条线上,但到目前为止,这就是我得到的。我不知道如何让调用的下一个方法出现在前一个方法旁边的同一行上。有人能帮忙吗

public class Diamonds {    
public static void main (String [] args) throws IOException {
    //calling the methods to put the logo together
    triangleB(); //part of top diamond
    triangleA(); //part of top diamond
    triangleC(); //part of top diamond
    triangleD(); //part of top diamond
    triangleB(); //part of bottom left diamond
    drawBox(); //part of bottom left diamond
    triangleD(); //part of bottom left diamond
    triangleC(); //part of bottom right diamond
    drawBox(); //part of bottom right diamond
    triangleA(); //part of bottom right diamond

}

public static void triangleA () throws IOException {
    //creates top right piece of top diamond and piece of bottom right diamond
    Scanner fileInput = new Scanner(new File("config.txt"));

    int size = fileInput.nextInt();
    int y = fileInput.nextInt();

    while (y <= size) {
        for (int x = 0; x <= size; x++) {
            if ( x <= y) System.out.print("*");
            else System.out.print(" ");}
        y++;
        System.out.println();
    }
}

public static void triangleB () throws IOException {
    //creates top left piece of top diamond and piece of bottom left diamond
    Scanner fileInput = new Scanner(new File("config.txt"));

    int size = fileInput.nextInt();
    int y = fileInput.nextInt();

    while (y <= size) {
        for (int x = size; x >= 0; x--) {
            if ( x <= y) System.out.print("*");
            else System.out.print(" ");}
        y++;
        System.out.println();
    }
}

public static void triangleC () throws IOException {
    //creates bottom left piece of top diamond and piece of bottom right diamond
    Scanner fileInput = new Scanner(new File("config.txt"));

    int size = fileInput.nextInt();
    int y = fileInput.nextInt();

    while (y <= size) {
        for (int x = 0; x <= size; x++) {
            if ( x >= y) System.out.print("*");
            else System.out.print(" ");}
        y++;
        System.out.println();
    }
}

public static void triangleD () throws IOException {
    //creates bottom right piece of top diamond and piece of bottom left diamond
    Scanner fileInput = new Scanner(new File("config.txt"));

    int size = fileInput.nextInt();
    int y = fileInput.nextInt();

    while (y <= size) {
        for (int x = size; x >= 0; x--) {
            if ( x >= y) System.out.print("*");
            else System.out.print(" ");}
        y++;
        System.out.println();
    }
}

public static void drawLine () throws IOException {
    //method used to draw lines for the box
    Scanner fileInput = new Scanner(new File("config.txt"));

    int size = fileInput.nextInt();

    for (int x = 0; x < size; x++) {
        System.out.print("**"); //two asterisks instead of one to create a box with longer length than height
    }
    System.out.println();
}

public static void drawBox () throws IOException {
    //method to draw a box for the bottom diamonds
    Scanner fileInput = new Scanner(new File("config.txt"));

    int size = fileInput.nextInt();
    int y = fileInput.nextInt();

    while (y <= size) {
        drawLine();
        y++;
    }
}

}


共 (1) 个答案

  1. # 1 楼答案

    尝试使用printf()而不是println()