打印指数增长的星形图案的Java程序
编写一个Java程序,使用for循环打印指数增长的星形图案。它使用嵌套的for循环,其中第一个for循环从零到给定的行进行迭代。然后,Java嵌套循环从1到i的幂值进行迭代,每次迭代都会打印出指数级增长的星形图案。
package ShapePrograms;
import java.util.Scanner;
public class ExponentiallyIncrease1 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter Exponentially Increased Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Exponentially Increased Rectangle Star Pattern");
for (int i = 0; i < rows; i++ )
{
for (int j = 1 ; j <= Math.pow(2, i); j++ )
{
System.out.print("\*");
}
System.out.println();
}
}
在这个Java指数增长的星形图案程序中,我们用一个while循环代替了for循环。
pacakge ShapePrograms;
import java.util.Scanner;
public class ExponentiallyIncrease2 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter Exponentially Increased Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Exponentially Increased Rectangle Star Pattern");
int i = 0, j;
while( i < rows)
{
j = 1 ;
while ( j <= Math.pow(2, i) )
{
System.out.print("\*");
j++;
}
System.out.println();
i++ ;
}
}
Enter Exponentially Increased Pattern Rows = 7
Printing Exponentially Increased Rectangle Star Pattern
*
**
****
********
****************
********************************
****************************************************************
使用do while循环打印指数增加的星形图案的Java程序
pacakge ShapePrograms;
import java.util.Scanner;
public class ExponentiallyIncrease3 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
int i = 0, j;
System.out.print("Enter Exponentially Increased Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Exponentially Increased Rectangle Star Pattern");
do
{
j = 1 ;
do
{
System.out.print("\*");
} while ( ++j <= Math.pow(2, i) );
System.out.println();
} while( ++i < rows) ;
}
Enter Exponentially Increased Pattern Rows = 4
Printing Exponentially Increased Rectangle Star Pattern
*
**
****
********
在这个Java例子中,ExponentiallyIncreasedPattern函数打印了一个给定符号的指数增加图案。
package ShapePrograms;
import java.util.Scanner;
public class ExponentiallyIncrease4 { private static Scanner sc;
public static void main(String\[\] args) {
sc = new Scanner(System.in);
System.out.print("Enter Exponentially Increased Pattern Rows = ");
int rows = sc.nextInt();
System.out.print("Enter Character for Exponentially Increased Pattern = ");
char ch = sc.next().charAt(0);
System.out.println("Printing Exponentially Increased Rectangle Star Pattern");
ExponentiallyIncreasedPattern(rows, ch);
}
public static void ExponentiallyIncreasedPattern(int rows, char ch) {
for (int i = 0; i < rows; i++ )
{
for (int j = 1 ; j <= Math.pow(2, i); j++ )
{
System.out.print(ch);
}
System.out.println();
}
}
Enter Exponentially Increased Pattern Rows = 7
Enter Character for Exponentially Increased Pattern = #
Printing Exponentially Increased Rectangle Star Pattern
#
##
####
########
################
################################
################################################################