循环结构 :while

循环四要素:
  1.初始化条件
  2.循环条件
  3.循环体
  4.迭代条件

格式:

  1.初始化条件
  while(2.循环条件){
    3.循环体
    4.迭代条件
  }

public class WhileTest{

    public static void main(String[] args){

        //需求 :求100以内的偶数,偶数的个数,偶数的总和
int count = 0; //偶数的个数
int sum = 0; //偶数的总和
int i = 1;//初始化条件
while(i <= 100){//循环条件
//循环体
if(i % 2 == 0){
count++;
sum += i;
System.out.println(i);
}
//迭代条件
i++;
} System.out.println("count=" + count + " sum=" + sum); }
}
/*

    从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入为0时结束程序。

*/
import java.util.Scanner;
public class NumberCountTest{ public static void main(String[] args){ Scanner sc = new Scanner(System.in);
int positiveNumber = 0; //正数
int negativeNumber = 0; //负数 while(true){
//读取数据
int number = sc.nextInt();
//判断
if(number > 0){//正数
positiveNumber++;
}else if(number < 0){//负数
negativeNumber++;
}else{//退出循环
break;
}
} System.out.println("正数的个数" + positiveNumber + " 负数的个数" + negativeNumber);
}
}

最新文章

  1. 提交本地项目到github服务器
  2. 【Swift学习】Swift编程之旅---Subscripts下标(十六)
  3. css font-weight原理
  4. iOS 键盘隐藏
  5. bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp
  6. Hibernate 的HQL语句,初级
  7. WINDOWS BITLOCK
  8. mac复制粘贴剪切
  9. 2017/4/27-Gradle的配置与Spring的下载
  10. 【BBED】BBED模拟并修复ORA-08102错误
  11. 记录在vue中使用jsx时踩过的坑
  12. 学习ELk之----02. Elastic Search操作入门
  13. springboot配置SSL自签名证书
  14. 文件系统的描述信息-/etc/fstab
  15. [转帖]NUMA架构的CPU -- 你真的用好了么?
  16. elasticsearch 单机多实例
  17. 6-7 树的层次遍历 uva122
  18. android根据图片路径显示图片
  19. INSTALL_FAILED_INVALID_APK
  20. LAMP结合discuz论坛的配置

热门文章

  1. java Class类使用
  2. MyEclipse使用教程:使用Workbench和Perspectives
  3. vue项目中打包background背景路径问题
  4. puppet使用rsync模块
  5. js原生高逼格插件
  6. javascript之大文件分段上传、断点续传(一)
  7. ASP.net 能写一个上传整个文件夹的东东(msdn)
  8. ACM 求全排列(字典序、邻位对换、递增进位制数,递减进位制数)
  9. 一本通例题-生日蛋糕——题解&lt;超强深搜剪枝,从无限到有限&gt;
  10. 阿里云CentOS 7.3安装Redis详细步骤