编写一个 Java 程序,使用 for 循环在每个行模式中打印重复的字符或字母。
package Alphabets;
import java.util.Scanner;
public class RepeatedCharPat1 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Repeated Character Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Repeated Character/Alphabets Pattern");
int alphabet = 65;
for (int i = 0 ; i < rows; i++ )
{
for (int j = 0 ; j <= i; j++ )
{
System.out.print((char)alphabet + " ");
}
alphabet++;
System.out.println();
}
}
}

这个示例程序使用 while 循环显示了每行重复字符的右三角形模式。
package Alphabets;
import java.util.Scanner;
public class RepeatedCharPat2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Repeated Character Pattern Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Repeated Character/Alphabets Pattern");
int i = 0, j, alphabet = 65;
while(i < rows )
{
j = 0 ;
while( j <= i )
{
System.out.print((char)alphabet + " ");
j++;
}
alphabet++;
System.out.println();
i++;
}
}
}
Enter Repeated Character Pattern Rows = 8
Printing Repeated Character/Alphabets Pattern
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G
H H H H H H H H
下面显示的程序将使用 do while 循环打印重复的字符或字母模式。
package Alphabets;
import java.util.Scanner;
public class RepeatedCharPat3 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.print("Enter Rows = ");
int rows = sc.nextInt();
System.out.println("Printing Repeated Character/Alphabets Pattern");
int i = 0, j, alphabet = 65;
do
{
j = 0 ;
do
{
System.out.print((char)alphabet + " ");
} while( ++j <= i ) ;
alphabet++;
System.out.println();
} while(++i < rows );
}
}
Enter Rows = 12
Printing Repeated Character/Alphabets Pattern
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G
H H H H H H H H
I I I I I I I I I
J J J J J J J J J J
K K K K K K K K K K K
L L L L L L L L L L L L