打印以下代码

时间:2016-06-18 19:12:05

标签: java

我一直试图打印一个有直角三角形及其镜像的图案,我无法找出镜像部分。

模式:

A       A
AB     BA
ABC   CBA
ABCD DCBA
ABCDEDCBA

模式I我已经能够打印:

A         A 
AB       AB
ABC     ABC
ABCD   ABCD
ABCDE ABCDE 

以下是代码:

public static void main(String args[])
{
    int i,j,k,l;
    System.out.print("Enter the numbers of rows you want to print: ");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    for (i=1;i<=n;i++)
    {
        int ch = 65,ch1 = 65;
        for (j=1;j<=i;j++)
        {
            System.out.print((char)ch);
            ch++;
        }
        for (k=n;k>=i+1;k--)
        {
            System.out.print("  ");
        }

        for (l=1;l<=i;l++)
        {
            System.out.print((char)ch1);
            ch1++;
        }
        System.out.println("");
    }
};

3 个答案:

答案 0 :(得分:0)

    int i,j,k,l;
    System.out.print("Enter the numbers of rows you want to print: \n");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    for (i=1;i<=n;i++)
    {
        int ch = 65,ch1 = 65;
        for (j=1;j<=i;j++)
        {
            System.out.print((char)ch);
            ch++;
        }

        for (k=2*(n+1-i)-3;k>0;k--)
        {
            System.out.print(" ");
        }

        for (l=1;l<=i;l++)
        {
            System.out.print((char)(ch1-l+i));
        }
        System.out.println("");
    }   
}

答案 1 :(得分:0)

For your specific problem, you should take advantage of a StringBuilder as not only does it allow you to append characters, but it also allows you to reverse the string, which is perfect for your mirroring scenario. Leaving the spaces as the only problem to handle, which Arrays.fill() may be able to help with

In your code you've provided the value of 'A' (65) here: int ch = 65,ch1 = 65; to use when printing. However for loops also work with char, meaning you can integrate this logic into the structure

E.g. if the loop initialization was 'A' this would be the same as starting at 'A'. The increment would then update this to 'B', 'C' etc as it loops assuming you were using ++. There are tutorials available to better explain this

By using variable names that convey what you are trying to do, it makes it easier to condense / optimise your code as you're writing it. Hopefully the SSCCE at the end of this answer can highlight the benefits of this, such as this line: int numberOfSpacesNeeded = numberOfRows - rowIndex;


SSCCE:

public class MirrorPrinting {

    public static void main(String[] args) {
        System.out.print("Enter the numbers of rows you want to print: ");
        Scanner scan = new Scanner(System.in);
        int numberOfRows = scan.nextInt();

        StringBuilder currentRow = new StringBuilder();
        for(int rowIndex = 1; rowIndex <= numberOfRows; rowIndex++){
            for(char currentCharacter = 'A'; currentCharacter < rowIndex + 'A'; currentCharacter++){
                currentRow.append(currentCharacter);
            }

            int numberOfSpacesNeeded = numberOfRows - rowIndex;
            char[] spaces = new char[numberOfSpacesNeeded];
            Arrays.fill(spaces, ' ');

            System.out.println(currentRow.toString() + new String(spaces) + currentRow.reverse().toString());

            //Reset StringBuilder
            currentRow = new StringBuilder();
        }
    }
}

答案 2 :(得分:0)

Comments to your code:

  • i,j,k,l; What is this? Did you forget the type, i.e. int i,j,k,l;? If so, remove the line, and declare the variables where they are used, e.g. for (int i=1;i<=n;i++).
    You can then also reuse the variable names.

  • If you want ch to be a char, why not make it a char? You should also use 'A' to initialize it, not 65.

  • Why do you need two ch variables? Just use one.

  • Last two points are moot, since you should just calculate the value directly, e.g. (char)('A' + j).

  • Now that you've done that, the last loop should count backwards, to get letters in reverse order. (mirror image part)

  • Since you want middle letter (E in your example) to only print once, that means you need odd number of spaces when not printing E and no spaces when printing the single E. Easiest way to do that, is for last line to be handled separately.

  • No need to add empty string when printing newline, i.e. println("")println().

  • Don't put ; after your method, i.e. };}.

This is the result:

public static void main(String args[]) {
    System.out.print("Enter the numbers of rows you want to print: ");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    // Print lines 1 to n-1
    for (int i = 1; i < n; i++) { // count up (i = 1 .. n-1)
        for (int j = 0; j < i; j++) { // count up (j = 0 .. i-1)
            System.out.print((char)('A' + j));
        }
        for (int j = 1; j < 2 * (n - i); j++) { // odd number of spaces
            System.out.print(' ');
        }
        for (int j = i - 1; j >= 0; j--) { // count down (j = i-1 .. 0)
            System.out.print((char)('A' + j));
        }
        System.out.println();
    }
    // Now print last line
    for (int j = 0; j < n; j++) { // count up (j = 0 .. n-1)
        System.out.print((char)('A' + j));
    }
    for (int j = n - 2; j >= 0; j--) { // count down (j = n-2 .. 0)
        System.out.print((char)('A' + j));
    }
    System.out.println();
}