使用用户输入的Java镜像程序



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

首先,我为尺寸使用了静态值。现在,我需要使用用户输入进行尺寸。

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

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

也需要创建一个名为printspaces()的方法,该方法为打印和使用它打印空格的空间采用一个参数。

创建一个名为printDots()的方法,该方法为打印和使用它打印点数的点进行了一个参数。

我需要删除哪个代码才能添加printDots和printspaces?

谢谢

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("|");
        }
    }
}

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

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

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

尝试时为我工作。

希望这会有所帮助。

最新更新