如何在Java中以这种模式打印不同的字符串?

时间:2019-01-20 12:39:36

标签: java

我正在尝试用Java编写一个程序,该程序将为输入的字符串提供如下模式

C O M P U T E R
O             E
M             T
P             U
U             P
T             M
E             O
R E T U P M O C

这是我的程序代码

import java.util.Scanner;
class pandapattern
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a word : ");
        String s=sc.nextLine();
        System.out.println();
        int l=s.length();
        for(int i=0;i<+l;i++)
        {
            System.out.print(s.charAt(i)+" ");
        }
        char[][] frwd = new char[l][1];
        char[][] bcwd = new char[l][1];
        for(int f=1;f<l;f++)
        {
            frwd[f][0]=s.charAt(f);
        }
        for(int b=l-2;b>=0;b--)
        {
            bcwd[b][0]=s.charAt(b);
        }
        for(int p=1;p<l;p++)
        {
            System.out.print("\n"+frwd[p][0]);
        }
        for(int p1=l-1;p1>=0;p1--)
        {
            System.out.print(bcwd[p1][0]+" ");
        }
    }
}

我得到了这种模式:

C O M P U T E R
O
M
P
U
T
E
R  E T U P M O C

我如何打印出整个图案?

请帮我弄清楚。

2 个答案:

答案 0 :(得分:1)

您仅需要 一个 char[]数组,而不是更多。 诀窍是解决问题“逐行”

参见下文:

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter word for Panda Pattern: ");
        String word = scanner.nextLine();
        String wspace = " ";
        //convert user input from String to char[]
        char[] wordLetters = word.toCharArray();
        //define array's length, for ease of reference
        int length = wordLetters.length;

        //initially print the sentence in a horizontal line
        for (char wordLetter : wordLetters) {
            System.out.print(wordLetter + wspace);
        }
        //insert new line to start printing for the pattern
        System.out.print("\n");

        /*A for loop that will print the left-most letters vertically
         We start the loop from 1, because the first letter was already printed*/
        for(int i=1; i<length; i++){
            System.out.print(wordLetters[i]);
            /*now we have an inner loop that will print the spaces and the
             rest of the letters in reverse order*/
            for(int j=1; j<length; j++){
                //conditional for IF we are at final line
                if(i == length-1 && j != length-1)
                    System.out.print(wspace + wordLetters[i-j]);
                //conditional for printing right-most letters
                else if(j == length-1) {
                    System.out.print(wspace + wordLetters[j-i]+"\n");
                }
                //THIS WILL PRINT 2 WHITE-SPACES.
                else
                    System.out.print(wspace + wspace);
            }
        }
    }
  

为什么我不需要第二个数组?

由于此模式仅需一个单词,然后反向打印,这意味着仍需要处理相同数量的字母,因此任何其他数组的长度都将相同。

Ergo,我们可以完全省略创建新数组的步骤!

为什么不操纵for-loopsarrays赋予我们的权力?

答案 1 :(得分:0)

首先,对于此任务,您需要一维数组frwdbcwr

第二,以与bcwd相同的方式填充数组frwd

正确地为任务重写了方法的一部分:

    int length = s.length();
    //printing first line
    for (int i = 0; i < +length; i++) {
        System.out.print(s.charAt(i) + " ");
    }
    System.out.println();

    //filling arrays
    char[] frwd = new char[length];
    char[] bcwd = new char[length];
    for (int f = 1; f < length; f++) {
        frwd[f] = s.charAt(f);
    }
    for (int b = 0; b < length; b++) {
        bcwd[b] = s.charAt(length-1 - b);
    }

    for (int p = 1; p < length-1; p++) {
        System.out.print(frwd[p]);

        //filling spaces to line by length of input string
        for (int p3 = 1; p3 < frwd.length-1; p3++) {
            System.out.print(" " + " ");
        }

        System.out.print(" " + bcwd[p]);
        System.out.println();
    }

    for (int p = 0; p <= length - 1; p++) {
        System.out.print(bcwd[p] + " ");
    }
    System.out.println();

您可以只使用输入字符串,而无需额外的char数组。只需以直线和反向循环从字符串(s.charAt(i)中获取字符即可。