for循环语句的练习

这篇文章展示了几个Java程序示例,包括使用boolean参数的比较,简单的算术运算计算器,累加计算,以及通过循环创建正等腰三角形、倒等腰三角形的图形输出,还有乘法口诀表的打印。这些例子涵盖了基本的条件判断、循环和数学运算。
摘要由CSDN通过智能技术生成

boolean 参数的比较

 public class day {
     public static void main(String[] args) {
         boolean bool = compare (8,8);
         System.out.println(bool);
     }
 ​
     public static boolean compare(int a ,int b) {
         /*
         boolean c = false;
         if (a == b){
             c = true;
         }
         return c;
        ============================
         boolean c = false;
         if (a == b){
              c = true;
         }else {
              c = false;
         }
         return c;
          */
         return a==b;
     }
 }

计算器

 public class sum {
     public static void main(String[] args) {
          int sum = getSum(5,6,"-");
         System.out.println(sum);
     }
 ​
     public static int getSum(int a ,int b, String tmp) {
         //定义一个方法  修饰 返回值类型 方法名 参数列表
         // int c = 0;
         if (tmp == "+") {
             return a + b;
         } else if (tmp == "-") {
             return a - b;
         } else if (tmp == "*") {
             return a * b;
         } else if (tmp == "/") {
             return a / b;
         }
         else {
             return 0;
         }
         //return c;//返回值
 ​
     }
 }

累加的实现

 public class day01 {
     public static void main(String[] args) {
         int sum = ary();
         System.out.println(sum);
     }
 ​
     public static int ary() {
         int a = 0 ;
         int b = 1000;
         for (int i = 1; i <= b; i++) {
             a = a + i;
         }
         return a;
     }
 }

正等腰三角形

 public class xingxing {
     public static void main(String[] args) {
         int cnt = 9 ;
         for (int i = 1; i <= cnt; i++) {
 ​
             for (int k = 0; k < cnt-i; k++) {
                 System.out.print(" ");
             }
             for (int j = 0; j < i; j++) {
                 System.out.print("* ");
             }
 ​
             System.out.println("  " + i);
         }
     }
 }
         *   1
        * *   2
       * * *   3
      * * * *   4
     * * * * *   5
    * * * * * *   6
   * * * * * * *   7
  * * * * * * * *   8
 * * * * * * * * *   9

倒等腰三角形

 public class xx {
     public static void main(String[] args) {
         int che = 9;
         for (int i = che; i >= 1 ; i--) {
 ​
             for (int k = 0; k < che-i; k++) {
                 System.out.print(" ");
             }
             for (int j = 0; j < i; j++) {
                 System.out.print("* ");
             }
 ​
             System.out.println("  " + i);
         }
     }
 }
 * * * * * * * * *   9
  * * * * * * * *   8
   * * * * * * *   7
    * * * * * *   6
     * * * * *   5
      * * * *   4
       * * *   3
        * *   2
         *   1

乘法口诀表

 public class chen {
     public static void main(String[] args) {
         int b = 9;
         for (int i = 1; i <= b; i++) {
             for (int j = 1; j <= i; j++) {
                 int a = i*j;
                 System.out.print(j + "*" + i + "=" + a + "  ");
             }
             System.out.println();
         }
     }
 }
 1*1=1  
 1*2=2  2*2=4  
 1*3=3  2*3=6  3*3=9  
 1*4=4  2*4=8  3*4=12  4*4=16  
 1*5=5  2*5=10  3*5=15  4*5=20  5*5=25  
 1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
 1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
 1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
 1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  

挑战 :打印出下面的图案

 ●○○○○○○○○●
 ○●○○○○○○●○
 ○○●○○○○●○○
 ○○○●○○●○○○
 ○○○○●●○○○○
 ○○○○●●○○○○
 ○○○●○○●○○○
 ○○●○○○○●○○
 ○●○○○○○○●○
 ●○○○○○○○○●

答案:

 public class dian {
     public static void main(String[] args) {
         int a = 10; //自由定义
         int c = a/2;
         //第一部分 (上)
         for (int i = 0; i < c; i++) {  //上半部分的长度
             for (int j = 0; j < i; j++) {  //正三角
                 System.out.print("a");
             }
 ​
             System.out.print("●");   //不用递增的符号。
 ​
             for (int k = 1; k < c-i; k++) {
                 System.out.print("b");
             }
             for (int l = 1; l < c-i; l++) {
                 System.out.print("c");
             }
 ​
             System.out.print("●");
             for (int m = 0; m < i; m++) {
                 System.out.print("d");
             }
             System.out.println("  "+i);
 ​
         }
         //第二部分 (下)
         for (int i = c; i > 0 ; i--) {
             for (int j = 1; j < i; j++) {
                 System.out.print("e");
             }
 ​
             System.out.print("●");
 ​
             for (int k = 0; k < c-i; k++) {
                 System.out.print("f");
             }
             for (int l = 0; l < c-i; l++) {
                 System.out.print("j");
             }
 ​
             System.out.print("●");
             for (int m = 1; m < i; m++) {
                 System.out.print("k");
             }
             System.out.println("  " + i);
 ​
         }
     }
 }
 ●bbbbcccc●  0
 a●bbbccc●d  1
 aa●bbcc●dd  2
 aaa●bc●ddd  3
 aaaa●●dddd  4
 eeee●●kkkk  5
 eee●fj●kkk  4
 ee●ffjj●kk  3
 e●fffjjj●k  2
 ●ffffjjjj●  1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值