【问题标题】:Java Mirror program with user input带有用户输入的 Java Mirror 程序
【发布时间】:2017-09-28 22:05:22
【问题描述】:

我做了一个简单的镜像程序,现在我被要求修改它。

起初我使用静态值来调整大小。现在我需要使用用户输入来调整大小。

到目前为止,这就是我所拥有的,但我不确定该去哪里。 如果有人可以提供帮助,那就太好了。

我得到的用户输入应该用于大小。

我还需要创建一个名为 printspaces() 的方法,它接受一个用于打印多少个空格的参数并使用它来打印空格。

创建一个名为 printdots() 的方法,该方法接受一个用于打印多少点的参数并使用它来打印这些点。

添加打印点和打印空间需要删除哪些代码?

谢谢

package stackoverflow;

import java.util.Scanner;

public class Mirror_2 {
    public static void main(String[] args) {
        line(0);
        top(0);
        bottom(0);
        line(0);
        int SIZE;

        Scanner Console = new Scanner(System.in);
        System.out.print("Please enter Size: ");
        int SIZE1 = Console.nextInt();
        System.out.println("You entered integer " + SIZE1);

    }

    public static void line(int SIZE) {
        // To change the lines at the bottom and top
        System.out.print("#");
        for (int i = 1; i <= SIZE * 4; i++) {
            System.out.print("=");
        }
        System.out.println("#");
    }

    public static void top(int SIZE) {
        // To change the top portion of the ASCII Art
        for (int line = 1; line <= SIZE; line++) {
            System.out.print("|");

            for (int space = 1; space <= (line * -2 + SIZE * 2); space++) {
                System.out.print(" ");
            }

            System.out.print("<>");

            for (int dot = 1; dot <= (line * 4 - 4); dot++) {
                System.out.print(".");
            }

            System.out.print("<>");

            for (int space = 1; space <= line * -2 + SIZE * 2; space++) {
                System.out.print(" ");
            }

            System.out.println("|");
        }
    }

    public static void bottom(int SIZE) {
        // To change the bottom portion of the ASCII Art
        for (int line = SIZE; line >= 1; line--) {
            System.out.print("|");

            for (int space = 1; space <= line * -2 + SIZE * 2; space++) {
                System.out.print(" ");
            }

            System.out.print("<>");

            for (int dot = 1; dot <= line * 4 - 4; dot++) {
                System.out.print(".");
            }

            System.out.print("<>");

            for (int space = 1; space <= line * -2 + SIZE * 2; space++) {
                System.out.print(" ");
            }

            System.out.println("|");
        }
    }
}

【问题讨论】:

    标签: java mirror


    【解决方案1】:

    我认为您只需要调用您的三个方法并传递用户输入:

        System.out.println("You entered integer " + SIZE1);
    
        // Add these three lines    
        line(SIZE1);
        top(SIZE1);
        bottom(SIZE1);
    }
    

    至于printspaces()printdots 方法,您已经获得了创建点和空格的代码。只需使用此名称创建新方法,并将当前打印空格和点的所有代码移动到适当的方法中,然后在您当前打印它们的代码中调用它们。

    当我尝试时为我工作。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多