如何打印以下图案

对于给定的输入,我需要打印模式。例如对于 input = 6 我必须打印:


MMMMMMSDDDDDD

MMMMMSSSDDDDD

MMMMSSSSSDDDD

MMMSSSSSSSDDD

MMSSSSSSSSSDD

MSSSSSSSSSSSD

CSSSSSSSSSSSK

CCSSSSSSSSSKK

CCCSSSSSSSKKK

CCCCSSSSSKKKK

CCCCCSSSKKKKK

CCCCCCSKKKKKK 

我已经尝试过,但不能比这更进一步


public class tgk {


public static void main(String[] args)  {


    Scanner sc = new Scanner(System.in);


    int size = sc.nextInt();


    int half = ((2*size)+1)/2;

     for (int i = 0; i < size ; i++) 

     { 

         for (int j = size; j > i; j--) 

         {

         System.out.print("M");

          }

     for (int k = half+1 ; k > half - i; k--) 

     {

         System.out.print("S");

     }







    System.out.println();

 }



     for(int i = size; i > 0; i--)

     {

         for (int j = size; j >= i; j--) {

             System.out.print("C");

         }

         for (int k = 0; k < (i * 2 - 1); k++) {

             System.out.print("S");

         }




         System.out.println();

     }

}


}

如果输入 = 3 它应该是


MMMSDDD

MMSSSDD

MSSSSSD

CSSSSSK

CCSSSKK

CCCSKKK



ibeautiful
浏览 174回答 2
2回答

撒科打诨

您可以使用两组for循环打印图案的每一半。假设input变量包含问题的大小int input = 3;for (int i = 0; i < input; i++) {&nbsp; &nbsp; for (int j = 0; j < input - i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print('M');&nbsp; &nbsp; }&nbsp; &nbsp; for (int j = 0; j < 2 * i + 1; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print('S');&nbsp; &nbsp; }&nbsp; &nbsp; for (int j = 0; j < input - i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print('D');&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}for (int i = input - 1; i >= 0; i--) {&nbsp; &nbsp; for (int j = 0; j < input - i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print('C');&nbsp; &nbsp; }&nbsp; &nbsp; for (int j = 0; j < 2 * i + 1; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print('S');&nbsp; &nbsp; }&nbsp; &nbsp; for (int j = 0; j < input - i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print('K');&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}将打印input = 3:MMMSDDDMMSSSDDMSSSSSDCSSSSSKCCSSSKKCCCSKKK和input = 6:MMMMMMSDDDDDDMMMMMSSSDDDDDMMMMSSSSSDDDDMMMSSSSSSSDDDMMSSSSSSSSSDDMSSSSSSSSSSSDCSSSSSSSSSSSKCCSSSSSSSSSKKCCCSSSSSSSKKKCCCCSSSSSKKKKCCCCCSSSKKKKKCCCCCCSKKKKKK

小唯快跑啊

我不知道为什么,但我真的希望它只使用一组 for 循环:&nbsp; &nbsp; int number = 8;&nbsp; &nbsp; for (int i = 0; i < number * 2; i++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < (number * 2) + 1; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i <&nbsp; number && j+i < number ?&nbsp; &nbsp; 'M' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i <&nbsp; number && j-i > number ?&nbsp; &nbsp; 'D' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i <&nbsp; number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?&nbsp; &nbsp; 'S' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i >= number && i-j >= number ?&nbsp; &nbsp;'C' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i >= number && j+i >= number*3 ? 'K' :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'S'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }所以对于8(就像在代码中一样)它打印:MMMMMMMMSDDDDDDDDMMMMMMMSSSDDDDDDDMMMMMMSSSSSDDDDDDMMMMMSSSSSSSDDDDDMMMMSSSSSSSSSDDDDMMMSSSSSSSSSSSDDDMMSSSSSSSSSSSSSDDMSSSSSSSSSSSSSSSDCSSSSSSSSSSSSSSSKCCSSSSSSSSSSSSSKKCCCSSSSSSSSSSSKKKCCCCSSSSSSSSSKKKKCCCCCSSSSSSSKKKKKCCCCCCSSSSSKKKKKKCCCCCCCSSSKKKKKKKCCCCCCCCSKKKKKKKK...或为3:MMMSDDDMMSSSDDMSSSSSDCSSSSSKCCSSSKKCCCSKKK
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java