简体   繁体   中英

Java Mirror program with user input

I made a simple mirror program and now I was asked to modify it.

At first I used a static value for the sizing. Now I need to use user input for the sizing.

So far this is what I have but i'm not sure where to go with this. If anyone can help that would be great.

The user input I get is supposed to be used for the size.

Also I need to create a method named printspaces() which takes a parameter for how many spaces to print and use it to print the spaces.

create a method named printdots() which takes a parameter for how many dots to print and use it to print the dots.

What code will I need to remove to add printdots and printspaces?

Thanks

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

I think you just need to call your three methods passing them the user input:

    System.out.println("You entered integer " + SIZE1);

    // Add these three lines    
    line(SIZE1);
    top(SIZE1);
    bottom(SIZE1);
}

As for the printspaces() and printdots methods, you've already got code that creates the dots and the spaces. Just create new methods with this names and move all the code that currently prints the spaces and dots into the appropriate methods and call them in the code where you currently are printing them.

Worked for me when I tried it.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM