public class While02 {

    public static void main(String[] args) {

        /**
* while(循环条件){
* 循环体(循环操作)
* }
* 循环条件 必须是一个boolean类型的值!
*
* 当满足了循环条件,会执行循环体,直到不满足循环条件是退出!
*/
System.out.println("大家辛苦了1");
System.out.println("大家辛苦了2");
System.out.println("大家辛苦了3");
System.out.println("大家辛苦了4");
System.out.println("大家辛苦了5");
System.out.println("**************************************");
// 定义一个变量 用来保存 循环的次数
int num = 0;
while (num <= 10000) {
System.out.println("大家辛苦了" + num);
// 自身+1
num++;
}
System.out.println(num); } }
public class WhileDemo03 {

    public static void main(String[] args) {

        /**
* 循环打印50份试卷
* 分析:
* 循环条件:count<=50 小于等于50次
* 循环体: 打印试卷
*/
int count = 1;
while (count <= 50) {
System.out.println("正在打印第" + count + "份试卷");
// 迭代变量
count++;
}
}
}
public class WhileDemo04 {

    /**
* 老师每天检查张浩的学习任务是否合格。
* 如果不合格,则继续进行。
老师给张浩安排的每天的学习任务为:
上午阅读教材,学习理论部分,
下午上机编程,掌握代码部分
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入是否合格?(y/n)");
String answer = scanner.next();
// 循环条件 -==》是否合格? 如果不合格 一直学习!
while (answer.equalsIgnoreCase("n")) {
System.out.println("阅读教材,学习理论部分");
System.out.println("上机编程,掌握代码部分");
System.out.println("再次输入是否合格?(y/n)");
answer = scanner.next();
}
System.out.println("完成了 学习任务!");
} }
public class WhileDemo05 {

    /**
* 2012年培养学员25万人,每年增长25%。
* 请问按此增长速度
* 到哪一年培训学员人数将达到100万人?
*/
public static void main(String[] args) { int year = 2012;
double person = 25;
while (person <= 100) {
person *= (1 + 0.25); // person*=(1+0.25);
year++;
}
System.out.println(year + "培训学员人数将达到100万人");
}
}
public class WhileDemo06 {

    /**
* 编程实现:计算100以内(包括100)的偶数之和
设置断点并调试程序,观察每一次循环中变量值的变化
*/
public static void main(String[] args) { int num = 0;
int sum = 0;// 记录所有的偶数和
while (num <= 100) {
if (num % 2 == 0) { // 偶数
sum += num; // sum=sum+num
}
num++;
}
System.out.println("所有的偶数和:" + sum);
}
}
public class WhileDemo07 {
public static void main(String[] args) {
System.out.println("欢迎进入MyShopping管理系统");
System.out.println("***************************");
System.out.println("1.帽子 \t 2.网球鞋 \t 3.球拍");
Scanner scanner = new Scanner(System.in);
System.out.println("请您输入购买物品的编号");
int choose = scanner.nextInt(); // 获取用户的选择
System.out.println("请您输入购买的数量");
int count = scanner.nextInt();// 获取用户购买的数量
// 定义一个总消费
double money = 0;
switch (choose) {
case 1:
System.out.println("您购买的是 1.帽子 \t ¥50\t总金额:" + (count * 50));
money += (count * 50);
break;
case 2:
System.out.println("您购买的是 2.网球鞋 \t ¥30\t总金额:" + (count * 30));
money += (count * 30);
break;
case 3:
System.out.println("您购买的是 3.球拍 \t ¥5\t总金额:" + (count * 5));
money += (count * 5);
break;
}
System.out.println("是否继续购物?(y/n)");
String answer = scanner.next(); while (answer.equalsIgnoreCase("y")) { // 循环购物操作
System.out.println("***************************");
System.out.println("1.帽子 \t 2.网球鞋 \t 3.球拍");
System.out.println("请您输入购买物品的编号");
choose = scanner.nextInt(); // 获取用户的选择
System.out.println("请您输入购买的数量");
count = scanner.nextInt();// 获取用户购买的数量
switch (choose) {
case 1:
System.out.println("您购买的是 1.帽子 \t ¥50\t总金额:" + (count * 50));
money += (count * 50);
break;
case 2:
System.out.println("您购买的是 2.网球鞋 \t ¥30\t总金额:" + (count * 30));
money += (count * 30);
break;
case 3:
System.out.println("您购买的是 3.球拍 \t ¥5\t总金额:" + (count * 5));
money += (count * 5);
break;
}
System.out.println("是否继续购物?(y/n)");
answer = scanner.next();
}
System.out.println("您本次消费是:" + money);
System.out.println("请您输入支付金额:");
double pay = scanner.nextDouble();
while (pay < money) { // 说明金额不正确
System.out.println("金额不正确!请重新支付!");
pay = scanner.nextDouble();
}
System.out.println("找零:" + (pay - money));
}
}
public class DoWhileDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double money = 0;
String answer = "";
do { // 先执行 再判断
System.out.println("欢迎进入MyShopping管理系统");
System.out.println("***************************");
System.out.println("1.帽子 \t 2.网球鞋 \t 3.球拍");
System.out.println("请您输入购买物品的编号");
int choose = scanner.nextInt(); // 获取用户的选择
System.out.println("请您输入购买的数量");
int count = scanner.nextInt();// 获取用户购买的数量
switch (choose) {
case 1:
System.out.println("您购买的是 1.帽子 \t ¥50\t总金额:" + (count * 50));
money += (count * 50);
break;
case 2:
System.out.println("您购买的是 2.网球鞋 \t ¥30\t总金额:" + (count * 30));
money += (count * 30);
break;
case 3:
System.out.println("您购买的是 3.球拍 \t ¥5\t总金额:" + (count * 5));
money += (count * 5);
break;
}
System.out.println("是否继续购物?(y/n)");
answer = scanner.next();
} while (answer.equalsIgnoreCase("y")); System.out.println("您本次消费是:" + money);
System.out.println("请您输入支付金额:");
double pay = scanner.nextDouble();
while (pay < money) { // 说明金额不正确
System.out.println("金额不正确!请重新支付!");
pay = scanner.nextDouble();
}
System.out.println("找零:" + (pay - money));
}
}
public class DoWhileDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double money = 0; // 记录总消费
String answer = "";
// 定义一个变量 来记录消费清单
String bill = "";
do { // 先执行 再判断
System.out.println("欢迎进入MyShopping管理系统");
System.out.println("***************************");
System.out.println("1.帽子 \t 2.网球鞋 \t 3.球拍");
System.out.println("请您输入购买物品的编号");
int choose = scanner.nextInt(); // 获取用户的选择
System.out.println("请您输入购买的数量");
int count = scanner.nextInt();// 获取用户购买的数量
switch (choose) {
case 1:
String x = "您购买的是 1.帽子 \t ¥50\t总金额:" + (count * 50);
System.out.println(x);
bill += x + "\n";
money += (count * 50);
break;
case 2:
String x2 = "您购买的是 2.网球鞋 \t ¥30\t总金额:" + (count * 30);
System.out.println(x2);
bill += x2 + "\n";
money += (count * 30);
break;
case 3:
String x3 = "您购买的是 3.球拍 \t ¥5\t总金额:" + (count * 5);
System.out.println(x3);
bill += x3 + "\n";
money += (count * 5);
break;
}
System.out.println("是否继续购物?(y/n)");
answer = scanner.next();
} while (answer.equalsIgnoreCase("y")); System.out.println("您本次消费是:" + money);
System.out.println("请您输入支付金额:");
double pay = scanner.nextDouble();
while (pay < money) { // 说明金额不正确
System.out.println("金额不正确!请重新支付!");
pay = scanner.nextDouble();
}
System.out.println("是否需要消费清单:(y/n)");
if (scanner.next().equalsIgnoreCase("y")) {
System.out.println(bill);
}
System.out.println("找零:" + (pay - money));
}
}
public class WhileDemo08 {

    /**
* 使用do-while实现:输出摄氏温度与华氏温度的对照表,
* 要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32 循环操作:计算摄氏温度,并输出对照条目
循环条件:
条目<=10 && 摄氏温度 <= 250
*/
public static void main(String[] args) {
int count = 1; // 对照条目
double c = 0; // 摄氏度
double f = 0; // 华氏度
System.out.println("条目\t摄氏度\t华氏度");
do {
// 转换
f = c * 9 / 5.0 + 32;
System.out.println(count + "\t" + c + "\t" + f);
count++;
c += 20;
} while (count <= 10 && c <= 250); } }
public class WhileDemo09 {

    /**
* 实现一个数字的反转
*/
public static void main(String[] args) {
int num = 123456789;
int temp = 1;
System.out.print("反转之后的数字:");
while (num != 0) {
// 依次与10取余
temp = num % 10; //
System.out.print(temp);
num = num / 10;
}
} }
public class ForDemo01 {

    public static void main(String[] args) {
/**
*
* for(表达式1;表达式2;表达式3) {
* 循环体
* }
* 表达式1:初始化变量 int a=0;
* 表达式2:循环条件!满足条件执行循环体操作!
* 表达式3:迭代变量!
*
* 三个表达式都可以省略!但是;不能省略!
*
*
* 执行顺序:
* 表达式1---》表达式2---》循环体---》表达式3--->表达式2---》循环体---》表达式3
*/
for (int i = 1; i <= 100; i++) {
System.out.println("好好学习!" + i);
}
}
}
public class ForDemo02 {
/**
* 循环输入某同学S1结业考试的5门课成绩,并计算平均分
*/
public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println("请您输入姓名:");
// 定义一个变量 来保存总成绩
double sum = 0;
String name = scanner.next();
for (int i = 1; i <= 5; i++) {
System.out.println("请您输入第" + i + "门课程的成绩:");
double score = scanner.nextDouble();
sum += score; // 总成绩
}
System.out.println("平均分是:" + (sum / 5)); } }
public class ForDemo03 {
public static void main(String[] args) {
// 根据用户的输入 产生对应的加法表
Scanner scanner = new Scanner(System.in);
System.out.println("请您输入一个数字:");
int num = scanner.nextInt();
for (int i = 0; i <= num; i++) {
System.out.println(i + "+" + (num - i) + "=" + (num));
} } }
public class ErrorDemo04 {
public static void main(String[] args) {
/**
* int i = 0; // 把初始化变量 局部化!
for (; i <= 5; i++) {
System.out.println(i); }
*/
/**for (;;) { // 死循环
System.out.println(1);
}*/ /**for (int i = 0;; i++) { // 省略了 条件判断
System.out.println(i);
}
*/ for (int i = 0; i < 10;) { // 省略了 条件判断迭代变量
System.out.println(i);
} }
}
public class ForDemo05 {
public static void main(String[] args) {
int sum = 0;
// 求1~100之间不能被3整除的数之和
for (int i = 1; i <= 100; i++) {
// 不能被3整除的数
if (i % 3 != 0) {
sum += i;
}
}
System.out.println("不能被3整除的数之和:" + sum);
} }
public class ForDemo06 {
// 计算年龄大于30的 占比
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 定义年龄的变量
int age = 0;
// 年龄大于30以上的人数
int count = 0;
for (int i = 1; i <= 5; i++) {
System.out.println("请您输入年龄:");
age = scanner.nextInt();
if (age >= 30) {
count++;
}
}
System.out.println("30岁以上的比例是:" + (count / 5.0 * 100) + "%");
System.out.println("30岁以下的比例是:" + ((1 - count / 5.0) * 100) + "%");
} }
public class ForDemo07 {
public static void main(String[] args) { System.out.println("请您输入一个数字:");
Scanner scanner = new Scanner(System.in);
double num = scanner.nextDouble(); DecimalFormat df = new DecimalFormat("0.00"); // 保留两位有效数字
String number = df.format(num); // 把double类型的数据 保留两位有效数字 返回String
System.out.println("保留两位有效数字:" + number); System.out.println("*********************************"); NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2); // 保留两位有效数字
number = nf.format(num);
System.out.println("保留两位有效数字:" + number); } }
public class ForDemo08 {
/**
* 日期的转换
* @throws ParseException
*/
public static void main(String[] args) throws ParseException { Date date = new Date(); // 日期 格式 Mon Mar 06 11:02:55 CST 2017
System.out.println("日期 格式:" + date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年-MM月-dd日 hh:mm:ss");
String time = sdf.format(date); // 把日期格式 转换成 String
System.out.println(time);
time = "2018年-05月-05日 11:11:11";
// 把 String 转换成 日期格式
System.out.println(sdf.parse(time));
} }
public class ForDemo09 {
//break 跳出当前循环结构
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("第" + i + "圈");
if (i == 8) {
System.out.println("坚持不住!!!");
break;
}
}
System.out.println("比赛结束!");
}
}
public class ForDemo10 {
/**
* 循环录入某学生5门课的成绩并计算平均分。
* 如果某分数录入为负,停止录入并提示录入错误
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 用来保存总成绩
double sum = 0;
// 标记 是否计算平均分
boolean flag = true;
for (int i = 1; i <= 5; i++) {
System.out.println("请您输入第" + i + "门课程的成绩:");
double score = scanner.nextDouble();
if (score < 0) { // 分数为负数
System.out.println("您的输入有误!退出系统!");
flag = false;
break;
}
sum += score;
}
if (flag) { // flag==true
System.out.println("平均分是:" + (sum / 5));
} }
}
public class ForDemo11 {

    /**
*1~10之间的整数相加,得到累加值大于20的当前数
*/
public static void main(String[] args) { // 定义变量保存 累加值
int sum = 0;
for (int i = 0; i <= 10; i++) {
sum += i;
if (sum > 20) {
break; // 跳出当前循环结构!
}
}
System.out.println("当前的sum===:" + sum); } }
public class ForDemo12 {

    /**
*循环录入Java课的学生成绩,
*统计分数大于等于80分的学生比例
*
*continue:代表的是:结束本次循环!继续下次的循环! 之后的代码不会执行!
*跳出循环体了吗??没有跳出!代表 循环继续!
*
*break: 跳出当前整个循环体!
*
*return: 跳出当前方法,可以带有返回值!
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 定义变量 保存分数大于80的人数
int sum = 0;
System.out.println("请输入班级的人数:");
int count = scanner.nextInt();
for (int i = 1; i <= count; i++) {
System.out.println("请输入第" + i + "名同学的成绩:");
double score = scanner.nextDouble();
if (score < 80) {
continue;
}
sum++;
return;
}
System.out.println("80分以上的人数:" + sum);
System.out.println("80分以上的人数占比:" + (sum * 1.0 / count) * 100 + "%");
}
}
public class ForDemo13 {

    /**
*验证用户登录 失败次数
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name; // 用户名
String password; // 密码
int i;// 记录用户失败的次数
// 进行三次循环操作
for (i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
name = scanner.next();
System.out.println("请输入密码:");
password = scanner.next();
// 判断用户名和密码是否正确
if ("admin".equals(name) && "admin".equals(password)) {
System.out.println("登录成功!");
break;
} else {
System.out.println("输入错误!您还有" + (2 - i) + "次机会!");
}
}
if (i == 3) { // 3次输入都不对
System.out.println("对不起!您三次都输入错误!");
} }
}
// 三种循环 来实现 100以内的偶数和
public static void main(String[] args) {
// 01.使用while循环
int num = 0;
int sum = 0; // 求和
while (num <= 100) {
// 找到偶数并相加
if (num % 2 == 0) {
sum += num; // sum=sum+num??
}
num++;
}
System.out.println("while偶数和是:" + sum); // 02.使用do while
sum = 0;
num = 0;
do {
if (num % 2 == 0) { // 找到偶数
sum += num;
}
num++;
} while (num <= 100);
System.out.println("dowhile偶数和是:" + sum); // 03.使用for
sum = 0;
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
sum += i;
}
}
System.out.println("for偶数和是:" + sum); }

三种循环获取100以内的偶数和

// a+=b a=a+b 是一致的吗?
int a = 5;
double b = 5;
// a =a + b;
a += b; // 做了强制类型转换 (int) (a + b) 不会编译报错
System.out.println(a);

a+=b与 a=a+b

java07循环结构的更多相关文章

  1. Python学习--04条件控制与循环结构

    Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...

  2. Swift -运算符和循环结构

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #4dbf56 } p.p2 { margin: 0.0px 0. ...

  3. 浅析PHP中for与foreach两个循环结构遍历数组的区别

    遍历一个数组是编程中最常见不过的了,这里跟大家讨论下for和foreach两种方法.用这两种方法执行遍历的场景太多太多了,这里我们只针对以下两个数组作为例子来讨论.所谓管中窥豹,多少能理清一点两者的区 ...

  4. PHP流程控制之循环结构

    计算机程序最擅长的功能之一就是按规定的条件,重复执行某些操作.循环结构可以减少源程序重复书写的工作量,即在给定条件成立时,反复执行某程序段,直到条件不成立为止.给定的条件称为循环条件,反复执行的程序段 ...

  5. python基础之循环结构以及列表

    python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.python IDE的选择 IDE的全称叫做集成 ...

  6. C语言-循环结构及break、continue

    循环结构 --1-- 结构循环 1.1 while循环 1.2 do…while循环 1.3 for循环 --2-- break和continue 2.1 break关键字 2.2 continue关 ...

  7. 黑马程序员——C语言基础 流程控制 选择结构和循环结构

    ---恢复内容开始--- Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)流程控制 1> 顺序结构:默认的流程 ...

  8. Java 第8章 循环结构进阶

    循环结构进阶 什么是二重循环? 二重循环的执行顺序是什么?

  9. luogg_java学习_03_流程控制及循环结构

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 程序流程控制 顺序结构 分支结构:if-else,sw ...

随机推荐

  1. KB006: CSS 框模型( Box module )

    框和布局 在 KB005: CSS 层叠 中已经介绍了 CSS 的重要之处.CSS 可以说是页面表现的基础, CSS 可以控制布局,控制元素的渲染. 布局是讲在电影画面构图中,对环境的布置.人物地位的 ...

  2. Centos7下Intel与AMD双显卡驱动的安装

      前2天,在Nvidia单显卡上成功安装上了NVIdia的驱动,一时兴起,拿出另外的一个HP笔记本也准备装上驱动,悲催的是HP的显卡是AMD的,更加.更加悲催的是还是Intel+AMD的双显卡.网络 ...

  3. bootstrap首页制作

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>我的 ...

  4. 12 Integer to Roman(int转罗马数字Medium)

    题目意思:1-3999转罗马数字 思路:从大往小减 ps:这题有点蛋疼 class Solution { public: string intToRoman(int num) { string a[] ...

  5. ActiveX控件资料

    Visual Studio 2008(c#)开发ActiveX控件及制作CAB包总结(1) 分类: C#2011-05-27 15:50 403人阅读 评论(0) 收藏 举报 c#stringhook ...

  6. sublime text3 下搭建python IDE环境 --Anaconda插件篇

    近来在园区发现大家使用ST3(Sublime text 3)工具进行python学习.然后自己也跟风了一把. 1.ST3下载地址: http://www.sublimetext.com/3 2.安装S ...

  7. iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)

    iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)   使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...

  8. iOS Block 用法 (1)-once again

    Block简介: Block的实际行为和Function很像,最大的差别是在可以存取同一个Scope的变量值.Block实体形式如下: ^(传入参数列){行为主体}; Block实体开头是“^”,接着 ...

  9. MSSQL BACKUP WEBSHELL

    Title:MSSQL BACKUP WEBSHELL -- Long Long Ago. Version:MSSQL 2005 && + ';alter/**/database/** ...

  10. DropzoneJS 可以拖拽上传的js库

    介绍 可以拖拽上传的 js库 网址 http://www.dropzonejs.com/ 同类类库 1.jquery.fileupload   http://blueimp.github.io/jQu ...