让火箭彼此相邻展示

时间:2015-10-08 00:03:19

标签: java

是的,这是一个家庭作业问题。但是,我不认为使用堆栈交换解决作业问题是一个问题。这是真正的程序员所做的,不是他们。

我有这个程序可以将两个火箭飞船打印到控制台。火箭垂直堆放。我希望他们并排出现。有没有办法在不改变所有静态方法的情况下做到这一点?

public class TwoRockets {

    public static void main (String[] args) {       
        appearrocket();
        appearrocket();     
    }

    public static void appearrocket() {         
        cone();
        box();
        label();
        box();
        cone();
    }       

    public static void box() {          
        System.out.println("+------+");
        System.out.println("|      |");
        System.out.println("|      |");
        System.out.println("+------+");         
    }

    public static void cone() {         
        System.out.println("   /\\");
        System.out.println("  /  \\");
        System.out.println(" /    \\");
    }

    public static void label() {            
        System.out.println("| VIVA |");
        System.out.println("|MEXICO|");
    }
}

2 个答案:

答案 0 :(得分:0)

您必须修改静态方法。 通过使用连接操作符,您可以并排获得两个火箭。 注释掉一个// showsrocket();

package com.stackover.ex;


public class TwoRockets {

    public static void main (String[] args) {

        appearrocket();     
        //appearrocket();

    }



    public static void appearrocket() {

        cone();
        box();
        label();
        box();
        cone();     
    }



    public static void box() {

        System.out.println("+------+\t"+"+------+");
        System.out.println("|      |\t"+"|      |");
        System.out.println("|      |\t"+"|      |");
        System.out.println("+------+\t"+"+------+");

    }

    public static void cone() {

        System.out.println("   /\\\t\t"+"   /\\");
        System.out.println("  /  \\\t\t"+"  /  \\");
        System.out.println(" /    \\\t\t"+" /    \\");      
    }

    public static void label() {

        System.out.println("| VIVA |\t"+"| VIVA |");
        System.out.println("|MEXICO|\t"+"|MEXICO|");
    }
}

答案 1 :(得分:0)

通用版本,只需更改变量副本的值即可处理任意数量的并列火箭:

public class TwoRockets {
  public static void main(String[] args) {
    final int copies = 3;
    printCap(copies);
    printBox(copies);
    printUS(copies);
    printBox(copies);
    printCap(copies);
}

public static void printCap(int times) {
for (int i = 0; i < times; i++) {
    System.out.print("   /\\   ");
    System.out.print("\t");
}

System.out.println();

for (int i = 0; i < times; i++) {
    System.out.print("  /  \\  ");
    System.out.print("\t");
}

System.out.println();

}

private static void printBox(int times) {
for (int i = 0; i < times; i++) {
    System.out.print("+------+");
    System.out.print("\t");
}
System.out.println();

for (int i = 0; i < times; i++) {
    System.out.print("|      |");
    System.out.print("\t");
}
System.out.println();

for (int i = 0; i < times; i++) {
    System.out.print("|      |");
    System.out.print("\t");
}
System.out.println();

for (int i = 0; i < times; i++) {
    System.out.print("+------+");
    System.out.print("\t");
}

System.out.println();

}

private static void printUS(int times) {
for (int i = 0; i < times; i++) {
    System.out.print("|United|");
    System.out.print("\t");
}
System.out.println();

for (int i = 0; i < times; i++) {
    System.out.print("|States|");
    System.out.print("\t");
}
System.out.println();

}

}