当前位置: 代码迷 >> 综合 >> for循环(countinue、break、九九乘法表、菱形、数字金字塔、1-100求和等)
  详细解决方案

for循环(countinue、break、九九乘法表、菱形、数字金字塔、1-100求和等)

热度:19   发布时间:2023-12-03 02:14:25.0
package cn.wzt;public class F1 {public static void main(String[] args) {//for计数循环语句 break终止循环 continue继续循环
//        for (int i=0;i<5;i++){
//            if (true) continue;
//            System.out.println(i);
//        }for (int i=0;i<20;i++){if (i%2!=0)continue;//退出这次循环,立即开始下次循环System.out.println(i);if (i>8){break;//退出循环语句(终止循环)}}//九九乘法表for (int i=1; i<10;i++){for (int j=1;j<=i;j++){System.out.printf("%d*%d=%2d  ",j,i,j*i);}System.out.println();}//求和1+2+3+...+100int sum=0;for (int i=1;i<=100;i++){sum+=i;}System.out.printf("1+2+3+...+100=%d%n",sum);//菱形int s=15;for (int i=1;i<=15;i+=2){--s;for (int i1=0;i1<s;i1++)System.out.printf(" ");for (int j=1;j<=i;j++)System.out.print("*");System.out.println();}for (int i=13;i>0;i-=2){++s;for (int i1=0;i1<s;i1++)System.out.printf(" ");for (int j=1;j<=i;j++)System.out.print("*");System.out.println();}//数字金字塔int z=15;for (int i=1;i<=15;i+=2){--z;for (int i1=0;i1<z;i1++)System.out.printf(" ");int zz=(i-1)/2;for (int j=1;j<=zz;j++)System.out.printf("%d",j);System.out.printf("%d",zz+1);for (int j=zz;j>0;j--)System.out.printf("%d",j);System.out.println();}//公鸡五块钱一只,母鸡三块钱一只,小鸡三只一块钱。现有一百块钱,要买100只鸡,问有几种方案for (int g=0;g<=100;g++){for (int m=0;m<=100;m++){for (int x=0;x<=100;x++){if (g+m+x==100&&g*5+m*3+x/3==100&&x%3==0){System.out.printf("公鸡%d只,母鸡%d只,小鸡%d只。%n",g,m,x);}}}}}
}

  相关解决方案