奇数长度对角线图案

时间:2017-02-25 14:27:27

标签: java string diagonal

我编写了一个程序来对角打印给定的字符串(如x)

输入:PROGRAM

预期产出:

P     M
 R   A
  O R
   G
  O R
 R   A
P     M

我的输出:

P
       M
 R
      A
  O
     R
       G
    R
   O
     A
  R
      M
 P

我的Java课程:

import java.util.*;

class Codechef
{

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        char[] word = new char[100];
        word = s.toCharArray();
        int count=0;
        int k = s.length()-1;

        for (int i=0;i<s.length();i++){
            for (int j=0;j<s.length();j++){
               if(!(i==k))
                   System.out.println(word[i]);

               for(int x=0;x<s.length()-count;x++)
                   System.out.print(" ");

               System.out.println(word[k]);
               k--;
               count++;

               break; 
            }

            for(int j=0;j<count;j++)
                System.out.print(" ");    
        }
    }

}

3 个答案:

答案 0 :(得分:0)

您的逻辑仅适用于水平线的上半部分。 完成以下计划:

import java.util.*;

class Codechef
{

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        char[] word = new char[100];
        word = s.toCharArray();
        int k = s.length()-1;
        int x ;

        for (int i=0;i<s.length();i++){
               if((i<k))
               {
                   for (x=0; x<i; x++)
                       System.out.print(" ");
                   System.out.print(word[i]);
                   for ( x=0; x<k-i-1; x++)
                       System.out.print(" ");
                   System.out.println(word[k]);
                   k--;

               }

               else if(i == k){
                   for ( x=0; x<i; x++)
                       System.out.print(" ");
                   System.out.println(word[k]);
                   k--;

               }
               else{
                   for(x = k; x >= 0 ; x--)
                       System.out.print(" ");
                   System.out.print(word[k]);
                   for (x=0; x<i-k-1; x++)
                       System.out.print(" ");
                   System.out.println(word[i]);
                   k--;

               }

            }

    }

}

按照你的说法提供输出。

答案 1 :(得分:0)

BlackArrow提出了一种更简单的打印图案的方法,我们可以将图案存储在二维数组中,然后优雅地打印。代码如下:-

public static void printDiagonalPattern(String str) {
    int len = str.length();
    //if length is even, then pattern is not possible.
    if (len % 2 == 0) {
        return;
    } else {

        char[][] matrix = new char[len][len];
        char[] givenString = str.toCharArray();
        //prepare elements for first diagonal i.e. up to down.
        for (int i = 0, j=0; i<len && j< len; i++, j++) {
            matrix[i][j] = givenString[i];
        }
        //prepare elements for second diagonal i.e. down to up.
        for (int i = len-1, j=0; i>=0 && j< len; i--, j++) {
            matrix[i][j] = givenString[j];
        }
        for(int i = 0; i<len; i++) {
            for (int j = 0; j<len; j++) {
                System.out.print(matrix[i][j]);
            }
            System.out.println("");
        }
    }
}

答案 2 :(得分:0)

浏览以下程序,

import java.util.Scanner;

public class PrintWelcome {

    public static void main(String[] args) {

        Scanner sc= new Scanner(System.in);
        System.out.println("enter odd string only :");
        String s=sc.nextLine();
        char c[]=s.toCharArray();
        int len=c.length;
        if((len&1)==0)
            System.out.println("string is prime: ");
        else{
            System.out.println("String is : "+s.trim()+", length : "+len);
            System.out.println("String center letter is : "+c[len/2]);
            int i, j=0,k,n=c.length/2,m=n+1;
            int last=c.length-1;
            int l=c.length-1;
            //upper portion 
            for(i=0; i<=c.length/2; i++)
            {   
                //if(n>=0)
                //upper center spaces
                for(j=c.length/2;j>i; j--)
                {
                    System.out.print(" ");//upper center spaces
                }//upper left array values
                for(k=i;k<=i;k++)
                {
                    System.out.print(c[n]);//upper left array values
                    n--;
                }
                //}
                //upper right part spaces
                if(m<=c.length-1)
                {
                    for(j=1; j<i*2; j++)
                    {
                        System.out.print(" ");//up
                    }//upper right values
                    for(k=m;k<=i+(c.length/2);k++)
                    {
                        System.out.print(c[m]);
                        m++;
                    }
                }
                System.out.println();
            }

            //below pattern
            for (int x = 0; x <=c.length/2; x++) {
                //below right spaces
                for(int y=1;y<=x;y++)
                {
                    System.out.print(" ");
                }
                //below left values
                for(int z=x;z<=x;z++)
                {
                    System.out.print(c[z]);
                }
                //below center space            
                for(int p=j-2;p>=x*2;p--)
                {
                    System.out.print(" ");

                }
                //below right values
                if(last>c.length/2)
                {
                    for(int r=last;r<=l;r++)
                    {
                        System.out.print(c[r]);
                        l--;
                        last--;
                    }
                }
                System.out.println();
            }
            }
    }

}