while循环结构

循环结构

while循环

 

 

package base.struct;

public class WhileDemo1 {
  public static void main(String[] args) {
      //输出1-100
      int i=0;
      while (i<100){
          //i=i+1
          i++;
          System.out.println(i);

      }
  }
}
package base.struct;

public class WhileDemo2 {
  public static void main(String[] args) {
      //死循环,尽量避免这种操作
      while (true){
          //等待客户端连接
          //定时检查
          ///其他需要等待的东西
      }
  }
}
package base.struct;

public class WhileDemo3 {
  public static void main(String[] args) {
      //计算1+2+3+。。。。+100=?
      //初始值为0
      int i=0;
      //总和
      int sum=0;
      while(i<=100){
          sum=sum+i;
          i++;
      }
      System.out.println(sum);
  }
}
 
posted @ 2022-05-13 16:15  怎样的人生  阅读(33)  评论(0)    收藏  举报