循环结构
为什么需要循环
理解循环的含义
会使用while循环结构
会使用do-while循环结构
会使用调试解决简单的程序错误
没有使用循环结构:
System.out.println("第1遍写:好好学习,天天向上!");
System.out.println(“第2遍写:好好学习,天天向上!");
System.out.println("第3遍写:好好学习,天天向上!");
System.out.println(“第4遍写:好好学习,天天向上!");
……
System.out.println(“第9999遍写:好好学习,天天向上!");
System.out.println(“第10000遍写:好好学习,天天向上!");
使用while循环
int i = 1;
while ( i <= 10000 ){
System.out.println("第" +i+ "遍写:
好好学习,天天向上!");
i ++;
}
循环结构的特点:
什么是while循环:
while ( 循环条件 ) {
循环操作
}
特点:先判断,再执行
流程图
实现代码
public static void main(String[] args) {
int count = 1;
while(count <= 50){
System.out.println("打印第" +count+ "份试卷");
count++;
}
}
如何用程序描述下面这个故事呢?
老师每天检查张浩的学习任务是否合格,如果不合格,则继续进行。
老师给张浩安排的每天的学习任务为:上午阅读教材,学习理论部分,下午上机编程,掌握代码部分。
分析是否存在重复操作
使用while循环实现
确定循环条件和循环操作
套用while语法写出代码
检查循环是否能够退出
System.out.print("合格了吗?(y/n):");
String answer = input.next();
while(!"y".equals(answer)){
System.out.println("上午阅读教材!");
System.out.println("下午上机编程!\n");
System.out.print("合格了吗?(y/n):");
answer = input.next();
}
System.out.println("完成学习任务!");
程序调试
在编写程序过程中有时也出现错误,但不好发现和定位错误,有没有好的方法呢?
通过代码阅读或者加输出语句查找程序错误
当程序结构越来越复杂时,需要专门的技术来发现和定位错误,就是“程序调试”
问题:
顺序输出1~5这5个数字
int i = 1;
System.out.println("程序调试演示,注意观察i的值:");
while(i < 5){
System.out.println(i);
i++;
}
双击左侧第7行双击
然后启动调试
或者这样 右键空白处找到Debug As进入
单步运行
调试启动后,运行到设置断点的代码行将停住
点击F6键可以单步运行程序,观察程序运行过程
观察变量
单步运行时可以在“变量”视图中看到变量当前的值
发现问题
变量i值变为5时就退出了循环,循环只进行4次
修正代码,重新运行
修改循环条件为: i <= 5
解决问题
do-while循环
如何用程序讲述下面的故事?
特点:先执行,再判断
源码:
do{
System.out.println("上机编写程序!");
System.out.print("合格了吗?(y/n)");
answer = input.next();
System.out.println("");
}while(!"y".equals(answer));
System.out.println("恭喜你通过了测试!");
while循环和do-while循环的区别
小练习:商品价格查询
public static void main(String[] args) {
String name = ""; //商品名称
double price = 0.0; //商品价格
int goodsNo = 0; //商品编号
System.out.println("MyShopping管理系统 > 购物结算\n");
//商品清单
System.out.println("*******************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T 恤 2.网球鞋 3.网球拍");
System.out.println("*******************************************");
Scanner input = new Scanner(System.in);
String answer = "y"; //标识是否继续
while("y".equals(answer)){
System.out.print("请输入商品编号:");
goodsNo = input.nextInt();
switch(goodsNo){
case 1:
name = "T 恤";
price = 245.0;
break;
case 2:
name = "网球鞋";
price = 570.0;
break;
case 3:
name = "网球拍";
price = 320.0;
break;
}
System.out.println(name+ "\t" + "¥" + price +"\n");
System.out.print("是否继续(y/n)");
answer = input.next();
}
System.out.println("程序结束!");
}
}
升级购物结算
public static void main(String[] args) {
String name = ""; //商品名称
double price = 0.0; //商品价格
int goodsNo = 0; //商品编号
int amount = 0; //购买数量
double discount = 0.8; //折扣比例
double total = 0.0; //商品总价
double payment = 0.0; //实付金额
//商品清单
System.out.println("*******************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T 恤 2.网球鞋 3.网球拍");
System.out.println("*******************************************");
Scanner input = new Scanner(System.in);
String answer = "y"; //标识是否继续
while("y".equals(answer)){
System.out.print("\n请输入商品编号:");
goodsNo = input.nextInt();
switch(goodsNo){
case 1:
name = "T 恤";
price = 245;
break;
case 2:
name = "网球鞋";
price = 570;
break;
case 3:
name = "网球拍";
price = 320;
break;
}
System.out.print("请输入购买数量:");
amount = input.nextInt();
System.out.println(name+ "¥" + price +"\t数量 " + amount
+ "\t合计 ¥" + price*amount);
total += price*amount;
System.out.print("是否继续(y/n)");
answer = input.next();
}
System.out.println("\n折扣:" + discount);
System.out.println("应付金额:" +total*discount);
System.out.print("实付金额:");
payment = input.nextDouble();
while(payment - total*discount < 0){
System.out.print("您输入的金额小于应付金额,请重新输入:");
payment = input.nextDouble();
}
System.out.println("找钱:" + (payment - total*discount));
}
MyShopping管理系统
public static void main(String[] args) {
System.out.println("欢迎使用MyShopping管理系统\n");
System.out.println("*******************************");
System.out.println("\t1.客 户 信 息 管 理");
System.out.println("\t2.购 物 结 算");
System.out.println("\t3.真 情 回 馈");
System.out.println("\t4.注 销");
System.out.println("*******************************\n");
int choice; //用户选择
boolean isRight; //输入是否正确
System.out.print("请选择,输入数字:");
Scanner input = new Scanner(System.in);
do{
isRight = true;
choice = input.nextInt();
if(choice == 1){
System.out.println("执行客户信息管理");
}else if(choice == 2){
System.out.println("执行购物结算");
}else if(choice == 3){
System.out.println("执行真情回馈");
}else if(choice == 4){
System.out.println("执行注销");
}else{
System.out.print("输入错误,请重新输入数字:");
isRight = false;
}
}while(!isRight);
System.out.println("\n程序结束");
}