感觉对你有用就给个鼓励吧~~
For循环嵌套练习汇总:
- public class ForForTest {
- public static void main(String[] args) {
- method_1();
- method_2();
- method_3();
- method_4();
- method_5();
- method_6();
- method_7();
-
- }
- //打印矩形
- public static void method_1(){
- for(int i = 0 ;i<5;i++){
- for(int j = 0; j<5;j++){
- System.out.print("*");
- }
- System.out.println();
- }
- }
- //打印倒直角三角形1
- public static void method_2(){
- for(int i = 0 ;i<5;i++){
- for(int j = 0; j<5-i;j++){
- System.out.print("*");
- }
- System.out.println();
- }
- }
- //打印倒直角三角形2
- public static void method_3(){
- for(int i = 0 ;i<5;i++){
- for(int j = i; j<5;j++){
- System.out.print("*");
- }
- System.out.println();
- }
- }
- //打印正直角三角形2
- public static void method_4(){
- for(int i = 0 ;i<5;i++){
- for(int j = 0; j<=i;j++){
- System.out.print("*");
- }
- System.out.println();
- }
- }
- //打印倒三角数字1
- public static void method_5(){
- for(int i = 0 ;i<5;i++){
- for(int j = 5; j>i;j--){
- System.out.print(j);
- }
- System.out.println();
- }
- }
- //打印倒三角数字2
- public static void method_6(){
- for(int i = 0 ;i<5;i++){
- for(int j = 0; j<=i;j++){
- System.out.print(i+1);
- }
- System.out.println();
- }
- }
- //打印等腰三角形
- public static void method_7(){
- for (int i = 0; i < 5; i++) {
- for(int k = 0; k < i; k++){
- System.out.print(" ");
- }
- for (int j = i; j < 5; j++) {
- System.out.print("* ");
- }
- System.out.println();
- }
- }
- }
复制代码 输出效果:
*****
*****
*****
*****
*****
*****
****
***
**
*
*****
****
***
**
*
*
**
***
****
*****
54321
5432
543
54
5
1
22
333
4444
55555
* * * * *
* * * *
* * *
* *
*
感觉对你有用就给个鼓励吧~~
|