解决有趣的模式问题可以增强对循环的理解。它们是必不可少的,因为它们有助于建立对特定编程语言的坚实基础。有各种各样的模式,包括基于数字、基于星号和基于字母的模式。本文将指导您使用java中的嵌套for循环来解决一个小屋星型模式。
打印小屋星形图案的java程序由于我们要使用嵌套for循环来解决问题,因此有必要讨论一下它的语法。 
语法for ( initial expression; conditional expression; increment/decrement expression ){   for ( initial expression; conditional expression; increment/decrement expression ) {      // code to be executed   }}
初始表达式 - 循环开始时执行一次。
条件表达式 - 代码将在条件表达式为真时执行。
递增/递减表达式 - 递增/递减循环变量。
pattern 的中文翻译为:模式方法将整个模式分为两部分。第一部分是一个上三角形状,第二部分是一个下矩形部分。
声明并初始化一个整数“n”,指定上下部分的行数。
声明并初始化空格和星星的初始计数。
现在,为上三角部分定义一个嵌套的 for 循环。外部 for 循环将运行到“n”,第一个内部循环将运行到空格计数并打印空格。打印后将空格数减 1。
第二个内部的for循环将运行直到星星计数,并打印星星。打印后将星星计数增加2。
再次创建另一个嵌套的for循环。外部for循环将运行到'n',第一个内部循环将打印左侧矩形形状,第二个内部循环将打印空格,最后一个内部循环将打印右侧矩形形状。
示例public class hut {   public static void main(string[] args) {      // count of upper triangle row and lower rectangle row       int n = 5;       int spc = n-1;       // initial count of space      int str = 1;       // initial count of star      // upper triangular shape      for(int i = 1; i <= n; i++) {         for(int j = 1; j <= spc; j++) {            // for space            system.out.print(\t);          }         spc--;         for(int k = 1; k <= str; k++) {             // for star            system.out.print(*\t);           }         str += 2;         system.out.println();          // to move the cursor to next line      }      // lower rectangular shape      for (int i = 0; i < n; i++) {         // for left rectangular shape         for (int j = 0; j < n/2; j++) {             system.out.print(*\t);         }         // for space         for (int j = 0; j < 5; j++) {            system.out.print(\t);         }         // for right rectangular shape         for (int j = 0; j < n/2; j++) {            system.out.print(*\t);         }         system.out.println();          // to move the cursor to next line      }   }}
输出				*				*	*	*			*	*	*	*	*		*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*	*						*	*	*	*						*	*	*	*						*	*	*	*						*	*	*	*						*	*	
结论在这篇文章中,我们讨论了小屋星形模式的解决方案。我们借助嵌套 for 循环解决了这个特殊问题。这将帮助您解码模式问题的逻辑,并使您能够自己解决其他模式。
以上就是打印小屋星形图案的程序的详细内容。
   
 
   