大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java打印金字塔的程序

Java打印金字塔的程序

更新時間:2022-11-25 11:13:57 來源:動力節點 瀏覽1233次

本文旨在給出一個Java實現的圖案打印。

簡單的金字塔圖案

import java.io.*;
// Java code to demonstrate star patterns
public class GeeksForGeeks
{
	// Function to demonstrate printing pattern
	public static void printStars(int n)
	{
		int i, j;
		// outer loop to handle number of rows
		// n in this case
		for(i=0; i<n; i++)
		{
			// inner loop to handle number of columns
			// values changing acc. to outer loop
			for(j=0; j<=i; j++)
			{
				// printing stars
				System.out.print("* ");
			}
			// ending line after each row
			System.out.println();
		}
}
	// Driver Function
	public static void main(String args[])
	{
		int n = 5;
		printStars(n);
	}
}

輸出

*
* *
* * *
* * * *
* * * * *

方法1:使用while循環

// java program to print simple pyramid pattern using while
// loop
import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		int r = 1, c = 0, n = 5;
		// the while loop check the conditions until the
		// condition is false. if it is true then enter in
		// to loop and execute the statements
		while (r <= n) {
			while (c <= r - 1) {
				// printing the required pattern
				System.out.print("* ");
				c++;
			}
			r++;
			c = 0;
			// new line after each row
			System.out.println();
		}
	}
}
// this code is contributed by gangarajula laxmi

輸出

*
* *
* * *
* * * *
* * * * *

方法2:使用遞歸

// Java code to demonstrate star pattern
// using Recursion
import java.io.*;
class GFG {
	// function to print a row
	static void printRow(int num)
	{
		// base case
		if (num == 0)
			return;
		System.out.print("* ");
		// recursively calling printRow()
		printRow(num - 1);
	}
	// function to print the pattern
	static void pattern(int n, int i)
	{
		// base case
		if (n == 0)
			return;
		printRow(i);
		System.out.println();
		// recursively calling pattern()
		pattern(n - 1, i + 1);
	}
	// Driver code
	public static void main(String[] args)
	{
		int n = 5;
		pattern(n, 1);
	}
}
// this code is contributed by Shivesh Kumar Dwivedi

輸出

*
* *
* * *
* * * *
* * * * *

180度旋轉后

import java.io.*;
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
	// Function to demonstrate printing pattern
	public static void printStars(int n)
	{
		int i, j;
		// outer loop to handle number of rows
		// n in this case
		for(i=0; i<n; i++)
		{
			// inner loop to handle number spaces
			// values changing acc. to requirement
			for(j=2*(n-i); j>=0; j--)
			{
				// printing spaces
				System.out.print(" ");
			}			
			// inner loop to handle number of columns
			// values changing acc. to outer loop
			for(j=0; j<=i; j++)
			{
				// printing stars
				System.out.print("* ");
			}			
			// ending line after each row
			System.out.println();
		}
	}
	// Driver Function
	public static void main(String args[])
	{
		int n = 5;
		printStars(n);
	}
}

輸出

           *
         * *
       * * *
     * * * *
   * * * * *

方法3:使用遞歸

// Java code to demonstrate star pattern
//using Recursion
import java.util.*;
public class GeeksForGeeks
{
	// function to print spaces
	static void printSpace(int space)
	{
		// base case
		if (space == 0)
		{
			return;
		}
		System.out.print(" " + " ");
		// recursively calling printSpace()
		printSpace(space - 1);
	}
	// function to print stars
	static void printStars(int star)
	{
		// base case
		if (star == 0)
		{
			return;
		}
		System.out.print("* ");
		// recursively calling printStars()
		printStars(star - 1);
	}
	// function to print the pattern
	static void pattern(int n, int num)
	{
		// base case
		if (n == 0)
		{
			return;
		}
		printSpace(n - 1);
		printStars(num - n + 1);
		System.out.println();
		// recursively calling pattern()
		pattern(n - 1, num);
	}
	// Driver code
	public static void main(String args[])
	{
		int n = 5;
		pattern(n, n);
	}
}
//this code is contributed by Shivesh Kumar Dwivedi

輸出

        *
      * *
    * * *
  * * * *
* * * * *

印刷三角

import java.io.*;
// Java code to demonstrate star pattern
public class GeeksForGeeks
{
	// Function to demonstrate printing pattern
	public static void printTriangle(int n)
	{
		// outer loop to handle number of rows
		// n in this case
		for (int i=0; i<n; i++)
		{
			// inner loop to handle number spaces
			// values changing acc. to requirement
			for (int j=n-i; j>1; j--)
			{
				// printing spaces
				System.out.print(" ");
			}
			// inner loop to handle number of columns
			// values changing acc. to outer loop
			for (int j=0; j<=i; j++ )
			{
				// printing stars
				System.out.print("* ");
			}
			// ending line after each row
			System.out.println();
		}
	}	
	// Driver Function
	public static void main(String args[])
	{
		int n = 5;
		printTriangle(n);
	}
}

輸出

    *
   * *
  * * *
 * * * *
* * * * *

方法4:使用遞歸

// Java code to demonstrate star pattern
// using recursion
import java.util.*;
public class GeeksForGeeks {
	// function to print spaces
	static void printSpace(int space)
	{
		// base case
		if (space == 0)
			return;
		System.out.print(" ");
		// recursively calling printSpace()
		printSpace(space - 1);
	}
	// function to print asterisks
	static void printStar(int asterisk)
	{
		// base case
		if (asterisk == 0)
			return;
		System.out.print("* ");
		// recursively calling printStar()
		printStar(asterisk - 1);
	}
	// function to print the pattern
	static void pattern(int n, int num)
	{
		// base case
		if (n == 0)
			return;
		printSpace(n - 1);
		printStar(num - n + 1);
		System.out.println("");
		// recursively calling pattern()
		pattern(n - 1, num);
	}
	// Driver code
	public static void main(String[] args)
	{
		int n = 5;
		pattern(n, n);
	}
}
// this code is contributed by Shivesh Kumar Dwivedi

輸出

    *
   * *
  * * *
 * * * *
* * * * *

打印金字塔的反面

//MainFunction
public class ReversePyramid
{
	public static void main(String[] args)
	{
		int rows = 6; // Number of Rows we want to print		
		//Printing the pattern
		for (int i = 1; i <= rows; i++)
		{
		for (int j = 1; j < i; j++)
			{
				System.out.print(" ");
			}
		for (int j = i; j <= rows; j++)
			{
				System.out.print(j+" ");
			}
			System.out.println();
		}				
		}	
	}

輸出

1 2 3 4 5 6
 2 3 4 5 6
  3 4 5 6
   4 5 6
    5 6
     6個

鏡像數字圖案

//MainFunction
public class ReversePattern
{
	public static void main(String[] args)
	{
		int rows = 7; // Number of Rows we want to print		
		//Printing the pattern
		for (int i = 1; i <= rows; i++)
		{
		for (int j = 1; j < i; j++)
			{
				System.out.print(" ");
			}
		for (int j = i; j <= rows; j++)
			{
				System.out.print(j+" ");
			}
			System.out.println();
		}
	//Printing the reverse pattern
		for (int i = rows-1; i >= 1; i--)
		{
		for (int j = 1; j < i; j++)
			{
				System.out.print(" ");
			}
		for (int j = i; j <= rows; j++)
			{
				System.out.print(j+" ");
			}
			System.out.println();
		}	
	}
}

輸出

1 2 3 4 5 6 7
 2 3 4 5 6 7
  3 4 5 6 7
   4 5 6 7
    5 6 7
     6 7
      7
     6 7
    5 6 7
   4 5 6 7
  3 4 5 6 7
 2 3 4 5 6 7
1 2 3 4 5 6 7

數字模式

import java.io.*;
// Java code to demonstrate number pattern
public class GeeksForGeeks
{
	// Function to demonstrate printing pattern
	public static void printNums(int n)
	{
		int i, j,num;
		// outer loop to handle number of rows
		// n in this case
		for(i=0; i<n; i++)
		{
			// initialising starting number
			num=1;
			// inner loop to handle number of columns
			// values changing acc. to outer loop
			for(j=0; j<=i; j++)
			{
				// printing num with a space
				System.out.print(num+ " ");
				//incrementing value of num
				num++;
			}
			// ending line after each row
			System.out.println();
		}
	}
	// Driver Function
	public static void main(String args[])
	{
		int n = 5;
		printNums(n);
	}
}

輸出

1個
1 2
1 2 3
1 2 3 4
1 2 3 4 5

方法5:使用 while 循環

// java program to print number pattern using while
// loop
import java.io.*;
class GFG {
	public static void main(String[] args)
	{
		int r = 1, c = 1, n = 5;
		// the while loop check the conditions until the
		// condition is false. if it is true then enter in
		// to loop and execute the statements
		while (r <= n) {
			while (c <= r ) {
				// printing the required pattern
				System.out.print(c+" ");
				c++;
			}
			r++;
			c = 1;
			// new line after each row
			System.out.println();
		}
	}
}

輸出

1個
1 2
1 2 3
1 2 3 4
1 2 3 4 5

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 性a爱片免费视频性 | 色多网站免费视频 | 99精品国产自在现线观看 | 亚洲人成一区二区三区 | 久久精品国产四虎 | 99v视频国产在线观看免费 | 国产精品999在线 | 国产高清在线精品一区免费97 | 欧美99热| 亚洲视频在线观看一区 | 亚洲一区视频 | 久久香蕉国产线看观看式 | 99爱视频99爱在线观看免费 | 欧美金妇欧美乱妇xxxx | 中文字幕日本一区波多野不卡 | 国产不卡视频在线播放 | 性夜黄a爽爽免费视频国产 性夜影院爽黄a爽免费看网站 | 日本在线不卡视频 | 啪啪一级片 | 一级毛片一 | 九九精品国产99精品 | 欧美一级aa天码毛片 | 99网站 | 亚洲欧美日韩中文字幕在线一 | 亚洲精品亚洲一区二区 | 在线播放亚洲 | 日韩综合一区 | 久久久国产高清 | 欧美日韩中文在线视频 | 伊人成影院九九 | 中文字幕亚洲综久久2021 | 九月婷婷天天澡天天添天天爽 | 精品中文字幕不卡在线视频 | 亚洲码在线 | 天天躁日日躁成人字幕aⅴ 天天躁日日躁狠狠躁黑人躁 | 久久香蕉国产线看观看99 | 91福利社在线观看 | 欧美久久久久欧美一区 | 四虎影视免费观看免费观看 | 青青青国产在线 | 天天操夜夜操视频 |