循环结构
文章目录
- 为什么要学习循环
- while循环
- dowhile循环
- 偶数之和
- 断点调试
- 购物结算
- 循环的选择
- 类名和全类名
- 摄氏华氏对照表
- for循环
- for执行次序
- 五门功课成绩
- for的特殊写法
- break和continue
- 录入客户信息_continue使代码优雅
- 小数的比较不能用==或!=
为什么要学习循环
在编写代码时,业务需求(项目要求)原地打转,符合条件结束循环。
马拉松 (操场
while循环
public class Demo01 {
public static void main(String[] args) {
/*
while (条件) {
循环的体(内容);
更新条件;
}
1 +到 100,5050
*/
int i = 1, sum = 0;
while (i <= 100) {
sum += i;
i++;
}
System.out.println("和:" + sum);
}
}
dowhile循环
public class Demo02 {
public static void main(String[] args) {
/*
do {
循环的体(内容);
更新条件;
} while(条件);
1 +到 100,5050
*/
int i = 1, sum = 0;
do {
sum += i;
i++;
} while (i <= 100);
System.out.println("和:" + sum);
}
}
偶数之和
学员操作一计算100以内的偶数之和
训练要点
while循环结构
程序调试
需求说明
编程实现: 计算100以内 (包括100) 的偶数之和设置断点并调试程序,观察每一次循环中变量值的变化
public class Demo03 {
public static void main(String[] args) {
int n = 1, sum = 0;
while (n <= 100) {
if (n % 2 == 0) sum += n;
n++;
}
System.out.println("sum: " + sum);
}
}
断点调试
购物结算
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("购物结算");
System.out.println("请选择商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
String jx = "y";
while (jx.equals("y")) {
System.out.print("请输入商品编号:");
int select = scanner.nextInt();
switch (select) {
case 1:
System.out.println("T恤 100元");
break;
case 2:
System.out.println("网球鞋 200元");
break;
case 3:
System.out.println("网球拍 300元");
break;
}
System.out.print("是否继续(y/n):");
jx = scanner.next();
}
System.out.println("程序结束!");
}
}
循环的选择
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("购物结算");
System.out.println("请选择商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
String jx;
do {
System.out.print("请输入商品编号:");
int select = scanner.nextInt();
switch (select) {
case 1:
System.out.println("T恤 100元");
break;
case 2:
System.out.println("网球鞋 200元");
break;
case 3:
System.out.println("网球拍 300元");
break;
}
System.out.print("是否继续(y/n):");
jx = scanner.next();
} while (jx.equals("y"));
System.out.println("程序结束!");
}
}
类名和全类名
摄氏华氏对照表
public class Demo06 {
public static void main(String[] args) {
/*
使用do-while实现:输出 *摄氏温度 与 *华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的 *条目 不超过10条。
转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 3
*/
double huashidu, sheshidu = 0;
int count = 0;
do {
huashidu = sheshidu * 9 / 5.0 + 32;
System.out.println(sheshidu + " vs. " + huashidu);
sheshidu += 20;
count++;
} while (sheshidu <= 250 && count <= 10);
}
}
for循环
public class Demo07 {
public static void main(String[] args) {
/*
1 +到 100,5050
特点:循环次数固定下来的。建议使用for循环。
for (声明初始化循环变量; 条件; 修改循环变量) {
循环体;
}
*/
int sum = 0;
for (int i=0; i<=100; i++) {
sum += i;
}
System.out.println("和:" + sum);
}
}
for执行次序
五门功课成绩
import java.util.Scanner;
public class Demo08 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = scanner.next(); // 记录姓名
int sum = 0; // 记录总成绩
for (int i=1; i<=5; i++) {
System.out.print("请输入5门功课的第" + i + "门的成绩:");
sum += scanner.nextInt();
}
System.out.println(name + "的平均分是:" + sum / 5.0);
}
}
for的特殊写法
public class Demo09 {
public static void main(String[] args) {
for (int i=0,j=6; i<=6; i++,j--) {
System.out.println(i + "+" + j + "=" + 6);
}
}
}
break和continue
import java.util.Scanner;
public class Demo10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
for (int i=1; i<=5; i++) {
System.out.print("成绩:");
int cj = scanner.nextInt();
if (cj < 0) break; // 结束循环
}
System.out.println("最后");
}
}
import java.util.Scanner;
public class Demo11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
for (int i=1; i<=5; i++) {
System.out.print("成绩:");
int cj = scanner.nextInt();
if (cj < 0) continue; // 忽略当次
sum += cj;
}
System.out.println("和:" + sum);
}
}
录入客户信息_continue使代码优雅
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("> 添加客户信息");
for (int i=1; i<=3; i++) {
System.out.print("会员号《4位》:");
int id = scanner.nextInt();
if (id >= 1000 && id <= 9999) { // 满足条件1
System.out.print("生日《mm/dd》:");
String birth = scanner.next();
if (birth.length() == 5) { // 满足条件2
System.out.print("积分:");
int jifeng = scanner.nextInt();
if (jifeng >= 0) { // 满足条件3
System.out.println("会员信息是:\n" + id + "\t" + birth + "\t" + jifeng);
}
}
}
}
System.out.println("程序结束!");
}
}
使用continue
import java.util.Scanner;
public class Demo13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("> 添加客户信息");
for (int i=1; i<=3; i++) {
System.out.print("会员号《4位》:");
int id = scanner.nextInt();
if (id < 1000 || id > 9999) continue; // 过滤
System.out.print("生日《mm/dd》:");
String birth = scanner.next();
if (birth.length() != 5) continue; // 过滤
System.out.print("积分:");
int jifeng = scanner.nextInt();
if (jifeng < 0) continue; // 过滤
System.out.println("会员信息是:\n" + id + "\t" + birth + "\t" + jifeng);
}
System.out.println("程序结束!");
}
}
小数的比较不能用==或!=
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
final double JINDU = 0.00001;
/*
1. 录入一个小数1,小数2,小数3。
判断小数1+小数2是否等于小数3?
小数是模拟出来的数,近似值。无法用== !=进行比较的。
*/
Scanner scanner = new Scanner(System.in);
System.out.print("小数1:");
double d1 = scanner.nextDouble();
System.out.print("小数2:");
double d2 = scanner.nextDouble();
System.out.print("小数3:");
double d3 = scanner.nextDouble();
if (d1 + d2 <= d3+JINDU && d1 + d2 >= d3-JINDU) System.out.println("d1+d2==d3");
else System.out.println("d1+d2!=d3");
System.out.println("程序结束!");
}
}