问:在哪些场景下我们需要用到循环结构?
答:在多次重复做同一件事的情况需要使用循环结构,能提高效率
1.循环定义:
(1)循环结构特点:1.循环条件(初始值,结束值)
2.循环操作(重复执行的事)
(2)循环结构和非循环结构对比
package cn.csc.test;
public class Demo6 {
public static void main(String[] args) {
//使用while循环结构实现循环操作【代码量少,效率高】
int i =1;
while (i<=5){
System.out.println("第"+i+"遍我是Night");
i++;
}
//使用非循环结构实现循环操作【代码量多,效率低】
System.out.println("我是Night");
System.out.println("我是Night");
System.out.println("我是Night");
System.out.println("我是Night");
System.out.println("我是Night");
}
}
2.while循环结构
while循环语法:
while(循环条件){
循环操作
}
实战1:
package cn.csc.test;
public class Demo7 {
public static void main(String[] args) {
//1.确定循环条件(有始有终
// 初始值:1 结束值:50
int i = 1;
//2.套用while语法结构
while (i<=50){
//3.确定循环操作
System.out.println("打印"+i+"试卷");
//4.循环推出语句
i++;
}
}
}
实战2:
package cn.csc.test;
import java.util.Scanner;
public class Demo9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
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("完成学习任务!");
}
}
3.do-while循环结构
问:什么场景需要用到do-while循环?
答: do-while循环 需要执行一次do,接着执行while循环
do-while循环语法:
do{
循环操作
}while(循环条件);
do-while实战:
package cn.csc.test;
import java.util.Scanner;
public class Demo15 {
public static void main(String[] args) {
String answer = " ";
do{
System.out.println("上机编程");
System.out.println("是否合格?(y/n):");
Scanner scanner = new Scanner(System.in);
answer = scanner.next();
}while(answer.equals("n"));
System.out.println("合格,通过测试");
}
}
do-while和while区别:
do-while:先执行再判断 while:先判断再执行
4.for循环结构
问:为什么用for循环?
答:代码量少,效率高
for循环语法:
for( 参数初始化;条件判断;更新循环变量){
循环操作
}
for循环实战:
package cn.csc.test;
import java.util.Scanner;
public class Demo16 {
public static void main(String[] args) {
//1.定义变量
//成绩
int score = 0;
//和
double sum = 0.0;
//平均分
double avg = 0.0;
//2.for循环结构
for (int i=1;i<6;i++){
//3.控制台循环展示
System.out.println("请输入5门功课中第"+i+"门课的成绩");
//4.循环获取用户端键盘的值(double)
Scanner scanner = new Scanner(System.in);
score = scanner.nextInt();
//5.求和就是将for循环遍历的所有值进行相加操作 score1+score2...+score5
sum = sum +score;
}
//6.输出和的结果
System.out.println("和:"+sum);
//7.求平均值
avg = sum/5;
//8.输出平均值
System.out.println("平均分:"+avg);
}
}
拓展:多重for循环 (建议:最多用三重,不要超出三重)
实战案例:99乘法表
package cn.csc.test;
public class Demo3 {
public static void main(String[] args) {
//使用双重for循环 循环99乘法表
//1 for循环 循环 行
for (int i=1;i<=9;i++) {
//2.for循环 循环 列
for (int j=1;j<=i;j++){
System.out.print(i+"*"+j+"="+i*j+"\t");
}
System.out.println("\n");
}
}
}
5.break跳出循环
定义:循环结构中遇到break语句,将会跳出其所在的循环,执行该循环结构后的第一条语句、
实战:
package cn.csc.test;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
//1.定义变量
//1.1定义每门课的成绩变量 score
//1.2定义和的变量 sum
//1.3第一平均分的变量 avg
double score = 0;
double sum = 0;
double avg = 0;
boolean flag = false;//状态位,默认为false
//2.获取学生姓名并循环录入学生成绩
System.out.println("请输入学生的姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
for (int i=1;i<=5;i++){
System.out.print("请输入第"+i+"门的成绩:");
score =scanner.nextDouble();
//3.判断如果成绩为负数,则退出
if (score<0){
flag = true;
break;
}
sum = sum+score;
}
if (flag){
System.out.println("抱歉,录入失败");
}else{
//4.获取平均分
avg = sum/5;
System.out.println(name+"的平均为"+avg);
}
}
}
6.continue跳出循环
continue 定义:跳过循环体中剩余的语句而执行下一次循环
实战:
package cn.csc.test;
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
//1.定义变量
int total = 0;//总人数
double score =0;//成绩
int num = 0;//成绩大于等于80的人数
//2.导入Scanner,输出字符串
System.out.println("请输入班级的总人数:");
Scanner scanner = new Scanner(System.in);
total = scanner.nextInt();
//3.循环录入
for (int i=1;i<=total;i++){
System.out.print("第"+i+"同学成绩:");
score =scanner.nextDouble();
if (score<80){
continue;
}
num++;
}
System.out.println("80分以上学生的人数是:"+num);
//4.求分数大于等于80的人数比例
double rate = ((double) num/total)*100;
System.out.println("80分以上学生所占比例是:"+rate+"%");
}
}
break和continue的区别
使用场景:break常用于switch结构和循环结构;continue常用于循环结构
作用:break终止某个循环,跳转到循环外的下一条语句;continue跳出本次循环,继续下个循环
下期预告:下一篇文章将介绍关于Java数组的基础知识
本人才疏学浅,文中如有出现错误请多多谅解