Java-for循环练习

输出四行直角三角形

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

输出七行等腰三角形

for (int i = 0; i < 7; i++) {
    for (int j = 0; j < 7; j++) {
        if (j > i)
            System.out.print(" ");
    }

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

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

输出七行棱形

for(int a=1;a<=4;a++){
    for(int b=1;b<=4-a;b++)
        System.out.print(" ");
    for(int c=1;c<=a*2-1;c++)
        System.out.print("*");
    System.out.println();
}
for(int d=3;d>=1;d--){
    for(int b=1;b<=4-d;b++){
        System.out.print(" ");
    }
    for(int c=4-d;c<=2+d;c++){
        System.out.print("*");
    }
    System.out.println();
}

斐波那契数列(for循环实现)

其他的递归和数组以后再讲

a b c c = b+c
1 1 2
int a=1, b=1, c=0;
System.out.println("斐波那契数列前20项为:");
System.out.print(a + "\t" + b + "\t");
for (int i = 1; i <= 18; i++) {
    c = a + b;
    a = b;
    b = c;
    System.out.print(c + "\t");
    if ((i + 2) % 5 == 0)
        System.out.println();
}

冒泡排序

int[] arr = {2, 0, 10, 9, 8, 4, 32, 1, 5, 7};
int invar;
for (int j = 0; j <arr.length-1 ; j++) {
    for (int i = 0; i <arr.length-1-j ; i++) {
        if (arr[i]>arr[i+1]){
            //交换顺序
            invar = arr[i+1];
            arr[i+1] = arr[i];
            arr[i] = invar;
        }
    }
}
System.out.println(Arrays.toString(arr));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一些Java的for循环练习题: 输出四行直角三角形: ```java for (int i = 0; i < 5; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } ``` 输出七行等腰三角形: ```java for (int i = 0; i < 7; i++) { for (int j = 0; j < 7; j++) { if (j > i) System.out.print(" "); } for (int j = 0; j <= i; j++) { System.out.print("*"); } for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(""); } ``` 输出七行棱形: ```java for (int a = 1; a <= 4; a++) { for (int b = 1; b <= 4 - a; b++) { System.out.print(" "); } for (int c = 1; c <= a * 2 - 1; c++) { System.out.print("*"); } System.out.println(); } for (int d = 3; d >= 1; d--) { for (int b = 1; b <= 4 - d; b++) { System.out.print(" "); } for (int c = 4 - d; c <= 2 * d; c++) { System.out.print("*"); } System.out.println(); } ``` 斐波那契数列(for循环实现): ```java int n = 10; // 斐波那契数列的长度 int[] fib = new int[n]; fib = 0; fib = 1; for (int i = 2; i < n; i++) { fib[i = fib[i - 1 + fib[i - 2]; } for (int i = 0; i < n; i++) { System.out.print(fib[i + " "); } ``` 正向乘法表: ```java for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " X " + i + " = " + (i * j) + "\t"); } System.out.println(); } ``` 循环输入周一到周五的成绩,并计算平均分、最高分和最低分: ```java Scanner input = new Scanner(System.in); double sum = 0; int maxScore = Integer.MIN_VALUE; int minScore = Integer.MAX_VALUE; for (int i = 1; i <= 5; i++) { System.out.println("请输入周" + i + "的成绩:"); int score = input.nextInt(); while (score < 0) { System.out.println("成绩不能为负数,请重新输入:"); score = input.nextInt(); } sum += score; maxScore = Math.max(maxScore, score); minScore = Math.min(minScore, score); } double average = sum / 5; System.out.println("这五个数的平均数:" + average); System.out.println("这五个数的最高数:" + maxScore); System.out.println("这五个数的最低数:" + minScore); ```<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java-for循环练习](https://blog.csdn.net/weixin_45297286/article/details/118616109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [JAVA初学者关于for循环的十道练习题](https://blog.csdn.net/m0_62756941/article/details/132035281)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

师兄白泽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值