首页 > 编程语言 > 详细

java流程控制(2)

时间:2021-05-04 23:39:20      阅读:26      评论:0      收藏:0      [点我收藏+]

面向对象

面向对象的本质:以类的方式组织代码,以对象的组织(封装)数据

打印九九乘法表

/*
* 1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
* */
public class ForDemo5 {
   public static void main(String[] args) {
       //1.先打印第一列
       //2.把固定的1再用一个循环包起来
       //3.去掉重复项 i<=j
       //4.调整样式
       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.print(j+"*"+i+"="+(i*j)+"\t");
          }
           System.out.println();
      }
  }
}

break continue 区别

public class BreakDemo {
   public static void main(String[] args) {
       int i =0;
       while (i<100){
           i++;
           System.out.println(i);
           if (i==30){
               break;
          }
      }
       System.out.println(123);
  }
}
public class ContinueDemo {
   public static void main(String[] args) {
       int i = 0;
       while (i<100){
           i++;
           if (i%10==0){
               System.out.println();
               continue;
          }
           System.out.print(i);
      }
       //break在任何循环语句的主体部分,均可用break控制循环的流程
       //break用于强行退出循环,不执行循环中剩余的语句(break也可在switch语句中使用)
       //continue 语句用在循环语句体中,用于终止末次循环过程,即跳出循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
  }
}

打印三角形

public class TestDemo1 {
   public static void main(String[] args) {
       //打印三角形
       for (int i = 1; i <=5; i++) {
           for (int j = 5; j >=i; j--) {
               System.out.print(" ");
          }
           for (int j = 1; j <=i; j++) {
               System.out.print("*");
          }
           for (int j = 1; j <i; j++) {
               System.out.print("*");
          }
           System.out.println();
      }
  }
}

 

java流程控制(2)

原文:https://www.cnblogs.com/liushuaiyu/p/14730320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!