Java打印各种图案

1 Java打印各种图案的思路

在Java面试中通常要求检查程序员的逻辑和思维。我们可以用不同的设计打印Java图案程序。要学习打印图案程序,我们必须对Java循环有深入的了解,例如for循环do-while循环。在本部分中,我们将学习如何在Java中打印模式。

我们将Java打印图案程序分为三类:

  • 星形图案
  • 数字图案
  • 字母图案

每当您为图案程序设计逻辑时,请首先在块中绘制该图案,如下图所示。该图呈现了该图案的清晰外观。

每个模式程序都有两个或两个以上的循环。循环数取决于模式或逻辑的复杂性。第一个for循环适用于行,第二个循环适用于列。在打印图案程序中,广泛使用Java for loop。

另外,在上述模式中,行是由表示 i 和 列 被表示为 j。我们看到第一行只打印一个星星。第二行打印两个星星,依此类推。该彩色块打印空间。

让我们为模式创建逻辑,上面给出。在以下代码段中,我们从0开始的行和列值。我们也可以从1开始的值,这是您的选择。

for(int i=0; i<row; i++)   
{   
for(int j=0; j<=i; j++)   
{   
System.out.print("* ");   
}   
System.out.println(); 

在上面的代码段中,第一个for循环用于行,第二个for循环用于列。

让我们逐步查看代码的执行情况,其中n = 4(我们要打印的行数)。

迭代1:

For i=0, 0<4 (true)
For j=0, j<=0 (true)

第一个print语句在第一行打印一个星号,第二个println语句打印空格并将光标扔到下一行。

*  

现在i和j的值增加到1。

迭代2:

For i=1, 1<4 (true)
For j=1, 1<=1 (true)

第一个print语句在第二行打印两个星号,第二个println语句打印空格并将光标移到下一行。

*  
* *  

现在i和j的值增加到2。

迭代3:

For i=2, 2<4 (true)
For j=2, 2<=2 (true)

第一个print语句在第三行打印三个星号,第二个println语句打印空格并将光标移到下一行。

*  
* *  
* * *  

现在i和j的值增加到3。

迭代4:

For i=3, 3<4 (true)
For j=3, 3<=3 (true)

第一个print语句在第四行打印四个星号,第二个println语句打印空格并将光标移到下一行。

*  
* *  
* * *  
* * * *  

现在i和j的值增加到4。

For i=4, 4<4 (false)

当i的值等于行数时,程序的执行将终止。

2 Java打印星形图案

2.1 Java打印直角三角形

/**
 * 一点教程网: http://www.yiidian.com
 */
public class RightTrianglePattern   
{   
public static void main(String args[])   
{   
//i for rows and j for columns      
//row denotes the number of rows you want to print  
int i, j, row=6;   
//outer loop for rows  
for(i=0; i<row; i++)   
{   
//inner loop for columns  
for(j=0; j<=i; j++)   
{   
//prints stars   
System.out.print("* ");   
}   
//throws the cursor in a new line after printing each line  
System.out.println();   
}   
}   
}  

输出结果为:

2.2 Java打印左三角形

/**
 * 一点教程网: http://www.yiidian.com
 */
public class LeftTrianglePattern  
{    
public static void main(String args[])   
{    
//i for rows and j for columns      
//row denotes the number of rows you want to print  
int i, j, row = 6;       
//Outer loop work for rows  
for (i=0; i<row; i++)   
{  
//inner loop work for space      
for (j=2*(row-i); j>=0; j--)         
{  
//prints space between two stars      
System.out.print(" ");   
}   
//inner loop for columns  
for (j=0; j<=i; j++ )   
{   
//prints star      
System.out.print("* ");   
}   
//throws the cursor in a new line after printing each line  
System.out.println();   
}   
}   
}  

输出结果为:

2.3 Java打印金字塔星形

/**
 * 一点教程网: http://www.yiidian.com
 */
public class PyramidPattern  
{    
public static void main(String args[])   
{    
//i for rows and j for columns      
//row denotes the number of rows you want to print  
int i, j, row = 6;       
//Outer loop work for rows  
for (i=0; i<row; i++)   
{  
//inner loop work for space      
for (j=row-i; j>1; j--)   
{  
//prints space between two stars  
System.out.print(" ");   
}   
//inner loop for columns  
for (j=0; j<=i; j++ )   
{   
//prints star      
System.out.print("* ");   
}   
//throws the cursor in a new line after printing each line  
System.out.println();   
}   
}   
}  

输出结果为:

2.4 Java打印菱形图案

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.Scanner;  
public class DiamondPattern  
{  
public static void main(String args[])  
{  
int row, i, j, space = 1;  
System.out.print("Enter the number of rows you want to print: ");  
Scanner sc = new Scanner(System.in);  
row = sc.nextInt();  
space = row - 1;  
for (j = 1; j<= row; j++)  
{  
for (i = 1; i<= space; i++)  
{  
System.out.print(" ");  
}  
space--;  
for (i = 1; i <= 2 * j - 1; i++)  
{  
System.out.print("*");  
}  
System.out.println("");  
}  
space = 1;  
for (j = 1; j<= row - 1; j++)  
{  
for (i = 1; i<= space; i++)  
{  
System.out.print(" ");  
}  
space++;  
for (i = 1; i<= 2 * (row - j) - 1; i++)  
{  
System.out.print("*");  
}  
System.out.println("");  
}  
}  
}  

输出结果为:

2.5 Java打印向下三角形星形

/**
 * 一点教程网: http://www.yiidian.com
 */
public class DownwardTrianglePattern  
{  
public static void main(String[] args)  
{  
int rows=7;      
//inner loop  
for (int i= rows-1; i>=0 ; i--)  
{  
//outer loop  
for (int j=0; j<=i; j++)  
{  
//prints star and space  
System.out.print("*" + " ");  
}  
//throws the cursor in the next line after printing each line  
System.out.println();  
}  
}  
}  

输出结果为:

2.6 Java打印镜像的直角三角形

/**
 * 一点教程网: http://www.yiidian.com
 */
public class MirroredRightTrianglePattern   
{  
public static void main(String[] args)  
{  
int n=7;  
//inner loop  
for (int i= 0; i<= n; i++)  
{  
//outer loop  
for (int j=1; j<=n-i; j++)  
{  
System.out.print(" ");  
}  
for (int k=0;k<=i;k++)  
{  
System.out.print("*");  
}   
System.out.println("");  
}  
}  
}  

输出结果为:

2.7 Java打印反金字塔星形

/**
 * 一点教程网: http://www.yiidian.com
 */
public class ReversePyramidPattern  
{  
public static void main(String[] args)  
{  
int rows=8;  
for (int i= 0; i<= rows-1; i++)  
{  
for (int j=0; j<=i; j++)  
{  
System.out.print(" ");  
}  
for (int k=0; k<=rows-1-i; k++)  
{  
System.out.print("*" + " ");  
}  
System.out.println();  
}  
}  
}  

输出结果为:

2.8 Java打印右下镜星形

/**
 * 一点教程网: http://www.yiidian.com
 */
public class RightDownMirrorPattern  
{  
public static void main(String args[])  
{  
int row=7;  
for (int i= row; i>= 1; i--)  
{  
for (int j=row; j>i;j--)  
{  
System.out.print(" ");  
}  
for (int k=1;k<=i;k++)  
{  
System.out.print("*");  
}  
System.out.println("");  
}  
}  
}  

输出结果为:

2.9 Java打印右帕斯卡三角

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.Scanner;  
public class RightPascalTrianglePattern  
{  
public static void main(String[] args)  
{  
int i, j, rows;  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter the number of rows you want to print: ");  
rows = sc.nextInt();          
for (i= 0; i<= rows-1; i++)  
{  
for (j=0; j<=i; j++)   
{  
System.out.print("*"+ " ");  
}   
System.out.println("");   
}   
for (i=rows-1; i>=0; i--)  
{  
for(j=0; j <= i-1;j++)  
{  
System.out.print("*"+ " ");  
}  
System.out.println("");  
}  
}  
}

输出结果为:

2.10 Java打印左帕斯卡三角

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.Scanner;  
public class LeftPascalTrianglePattern  
{  
public static void main(String[] args)  
{  
int i, j, k, rows;  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter the number of rows you want to print: ");  
rows = sc.nextInt();          
for (i= 1; i<= rows ; i++)  
{  
for (j=i; j <rows ;j++)              
{  
System.out.print(" ");  
}  
for (k=1; k<=i;k++)   
{  
System.out.print("*");   
}   
System.out.println("");   
}   
for (i=rows; i>=1; i--)  
{  
for(j=i; j<=rows;j++)  
{  
System.out.print(" ");  
}  
for(k=1; k<i ;k++)   
{  
System.out.print("*");  
}  
System.out.println("");  
}  
sc.close();  
}  
} 

输出结果为:

2.11 Java打印沙漏星形

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.Scanner;  
public class SandglassPattern  
{  
public static void main(String[] args)  
{  
int i, j, k, n;  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter the number of rows you want to print: ");  
n = sc.nextInt();              
for (i= 0; i<= n-1 ; i++)  
{  
for (j=0; j<i; j++)  
{  
System.out.print(" ");  
}  
for (k=i; k<=n-1; k++)   
{   
System.out.print("*" + " ");   
}   
System.out.println("");   
}   
for (i= n-1; i>= 0; i--)  
{  
for (j=0; j<i; j++)  
{  
System.out.print(" ");  
}  
for (k=i; k<=n-1; k++)  
{  
System.out.print("*" + " ");  
}  
System.out.println("");  
}  
sc.close();  
}  
}  

输出结果为:

2.12 Java打印字母星形

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.*;  
public class AlphabetPattern  
{  
public static void main(String[] args)  
{  
int i, j, n=8;  
// Outer for loop for number of lines  
for (i = 0; i<=n; i++)   
{  
// Inner for loop for logic execution  
for (j = 0; j<= n / 2; j++)   
{  
// prints two vertical lines  
if ((j == 0 || j == n / 2) && i != 0 ||  
// print first line of alphabet  
i == 0  && j != n / 2 ||   
// prints middle line  
i == n / 2)   
System.out.print("*");  
else  
System.out.print(" ");  
}  
System.out.println();  
}  
}  
}  

输出结果为:

2.13 Java打印三角星型

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.Scanner;  
public class TrianglePattern  
{  
public static void main(String[] args)  
{  
int i, j, k, rows=9;  
for (i=1; i<= rows ; i++)  
{  
for (j = i; j < rows ; j++)   
{  
System.out.print(" ");  
}     
for (k = 1; k <= (2*i -1) ;k++)   
{  
if(k==1 || i == rows || k==(2*i-1))   
{  
System.out.print("*");  
}  
else   
{  
System.out.print(" ");  
}  
}  
System.out.println("");  
}  
}  
}  

输出结果为:

2.14 Java打印下三角图案

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.Scanner;  
public class DownTrianglePattern  
{  
public static void main(String[] args)  
{  
int i, j, k, rows=9;     
for (i=rows; i>= 1 ; i--)  
{  
for (j = i; j<rows ; j++)   
{  
System.out.print(" ");  
}     
for (k = 1; k <= (2*i -1) ;k++)   
{  
if( k==1 || i == rows || k==(2*i-1))   
{  
System.out.print("*");  
}  
else   
{  
System.out.print(" ");  
}  
}  
System.out.println("");  
}  
}  
}  

输出结果为:

2.15 Java打印钻石星型

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.*;  
public class DiamondPattern  
{  
public static void main(String[] args)  
{  
Scanner sc = new Scanner(System.in);  
System.out.println("Enter the number of rows you want to print: ");  
int rows = sc.nextInt();      
for (i=1; i<= rows ; i++)   
{  
for (j = rows; j > i ; j--)   
{  
System.out.print(" ");  
}  
System.out.print("*");  
for (k = 1; k < 2*(i -1) ;k++)   
{   
System.out.print(" ");   
}  
if( i==1)   
{   
System.out.println("");  
}  
else  
{  
System.out.println("*");   
}  
}   
for (i=rows-1; i>= 1 ; i--)  
{  
for (int j = rows; j > i ; j--)   
{  
System.out.print(" ");  
}  
System.out.print("*");  
for (int k = 1; k < 2*(i -1) ;k++)   
{  
System.out.print(" ");  
}  
if(i==1)  
System.out.println("");  
else  
System.out.println("*");  
}  
}  
}

输出结果为:

3 Java打印数字图案

3.1 图案1

/**
 * 一点教程网: http://www.yiidian.com
 */
public class Pattern1  
{  
public static void main(String args[])   
{   
int i, j,number, n=7;   
//loop for rows  
for(i=0; i<n; i++)  
{   
number=1;   
//loop for columns  
for(j=0; j<=i; j++)  
{   
//prints num  
System.out.print(number+ " ");   
//incrementing the value of number   
number++;   
}   
//throws the cursor at the next line after printing each row  
System.out.println();   
}   
}   
}  

输出结果为:

3.2 图案2

/**
 * 一点教程网: http://www.yiidian.com
 */
public class Pattern2  
{              
public static void main(String[] args)   
{  
int i, j, k = 1;  
//inner loop  
for (i = 1; i <= 7; i++)   
{  
//outer loop  
for (j = 1; j< i + 1; j++)   
{  
//prints the value of k  
System.out.print(k++ + " ");  
}  
//throws the cursor at the next line  
System.out.println();  
}  
}  
}  

输出结果为:

3.3 图案3

/**
 * 一点教程网: http://www.yiidian.com
 */
public class Pattern3  
{              
public static void main(String[] args)   
{  
int n = 8;    //n is the number of rows you want to print  
for (int i = 0; i < n; i++)   
{  
int number = 1;  
System.out.printf("%" + (n - i) * 2 + "s", "");  
for (int j = 0; j <= i; j++)   
{  
System.out.printf("%4d", number);  
number = number * (i - j) / (j + 1);  
}  
System.out.println();  
}  
}  
} 

输出结果为:

3.4 图案4

/**
 * 一点教程网: http://www.yiidian.com
 */
public class Pattern4  
{              
public static void main(String[] args)   
{  
for (int i = 1; i <= 4; i++)  
{  
int n = 8;  
for (int j = 1; j<= n - i; j++)   
{   
System.out.print(" ");   
}   
for (int k = i; k >= 1; k--)  
{  
System.out.print(k);  
}  
for (int l = 2; l <= i; l++)   
{  
System.out.print(l);   
}   
System.out.println();   
}   
for (int i = 3; i >= 1; i--)  
{  
int n = 10;  
for (int j = 0; j<= n - i; j++)   
{  
System.out.print(" ");   
}   
for (int k = i; k >= 1; k--)  
{  
System.out.print(k);  
}  
for (int l = 2; l <= i; l++)  
{  
System.out.print(l);  
}  
System.out.println();  
}  
}  
}

输出结果为:

3.5 图案5

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.*;  
public class Pattern5  
{              
public static void main(String[] args)   
{  
int i, j, rows;  
Scanner sc = new Scanner(System.in);    
System.out.print("Enter the number of rows you want to print: ");      
 rows = sc.nextInt();           
for (i = 1; i <= rows; i++)   
{  
for (j = 1; j <= i; j++)  
{  
System.out.print(i+" ");  
}  
System.out.println();  
}           
}  
}  

输出结果为:

4 Java打印字母图案

4.1 Java打印直角三角形字母图案

/**
 * 一点教程网: http://www.yiidian.com
 */
public class RightAlphabaticPattern  
{              
public static void main(String[] args)  
{  
int alphabet = 65; //ASCII value of capital A is 65  
//inner loop for rows  
for (int i = 0; i <= 8; i++)  
{  
//outer loop for columns      
for (int j = 0; j <= i; j++)  
{  
//adds the value of j in the ASCII value of A and prints the corresponding alphabet  
System.out.print((char) (alphabet + j) + " ");   
}  
System.out.println();  
}  
}  
}  

输出结果为:

4.2 Java打印重复字母图案

/**
 * 一点教程网: http://www.yiidian.com
 */
public class RepeatingPattern  
{              
public static void main(String[] args)  
{  
int letter = 65; //ASCII value of capital A is 65  
//inner loop for rwos  
for (int i = 0; i<= 9; i++)  
{  
//outer loop for columns  
for (int j = 0; j <= i; j++)  
{  
//prints the character  
System.out.print((char) letter + " ");  
}  
letter++;  
System.out.println();  
}  
}  
}  

输出结果为:

4.3 Java打印K形字母图案

public class KshapePattern  
{  
public static void main(String[] args)  
{  
for (int i = 8; i >= 0; i--)  
{  
int alphabet = 65;  
for (int j = 0; j <= i; j++)  
{  
System.out.print((char) (alphabet + j) + " ");  
}  
System.out.println();  
}  
for (int i = 0; i<= 8; i++)  
{  
int alphabet = 65;  
for (int j = 0; j <= i; j++)  
{  
System.out.print((char) (alphabet + j) + " ");  
}  
System.out.println();  
}  
}  
}  

输出结果为:

4.4 Java打印三角形字符图案

public class TriangleCharacterPattern  
{              
public static void main(String[] args)  
{  
for (int i = 0; i <= 8; i++)   
{  
int alphabet = 65;   
for (int j = 8; j > i; j--)  
{  
System.out.print(" ");  
}  
for (int k = 0; k <= i; k++)  
{  
System.out.print((char) (alphabet + k) + " ");  
}  
System.out.println();  
}  
}  
}  

输出结果为:

4.5 Java打印菱形字母图案

/**
 * 一点教程网: http://www.yiidian.com
 */
import java.util.Scanner;  
public class DiamondCharacterPattern  
{  
public static void main(String[] args)   
{  
char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };  
int alphabet _number = 0;  
String[] diamond = new String[26]; // array of strings  
System.out.print("Enter a Character between A to Z : ");  
Scanner reader = new Scanner(System.in);  
try   
{  
char user_ alphabet = reader.next("[A-Z]").charAt(0);  
// search for letter number in the array letter  
for (int i = 0; i < alphabet.length; i++)   
{  
if (letter[i] == user_ alphabet)   
{  
alphabet _number = i;  
break;  
}  
}  
//construct diamond  
for (int i = 0; i <= alphabet _number; i++)   
{  
diamond[i] = "";  
//add initial spaces  
for (int j = 0; j < alphabet _number - i; j++)   
{  
diamond[i] += " ";  
}  
// add alphabet  
diamond[i] += alphabet  
//add space between letters  
if (alphabet[i] != 'A')   
{  
for (int j = 0; j < 2 * i - 1; j++)   
{   
diamond[i] += " ";   
}   
// add alphabet  
diamond[i] += alphabet[i];   
}   
// Draw the first part of the diamond   
System.out.println(diamond[i]);   
}   
for (int i = alphabet _number - 1; i >= 0; i--)  
{  
// Draw the second part of the diamond  
// prints the diamondArray in the reverse order  
System.out.println(diamond[i]);  
}  
}  
catch (Exception e)   
{  
e.printStackTrace();  
}  
finally   
{  
reader.close();  
}  
}  
} 

输出结果为:

热门文章

优秀文章