黑马程序员Java零基础视频教程笔记-判断和循环

文章目录

  • 一、流程控制语句
  • 二、if 的第一种格式
  • 三、if 的第二种格式
  • 四、if 的第三种格式
  • 五、switch语句和练习
  • 六、switch的扩展知识点和练习
  • 七、for循环格式和练习
  • 八、for循环练习-累加思想和统计思想
  • 九、循环语句-while
  • 十、两道力扣算法题和do...while循环
  • 附:更多内容

一、流程控制语句

1. 顺序结构
顺序结构语句是Java程序默认的执行流程,按照代码的先后顺序,从上到下依次执行
2. 样例

package com.itheima.orderdemo;
public class OrderDemo{public static void main(String[] args){System.out.println("努力做主人喜欢的事");System.out.println("大小姐驾到!通通闪开!");System.out.println("凛冬已至,故乡的梅花开了吗");System.out.println("心怀不惧,方能翱翔于天际");}
}

二、if 的第一种格式

1. 格式

if (关系表达式){语句体;
}

2. 举例

if (酒量 > 2){System.out.println("小伙子,不错!");
}

3. 执行流程

① 首先计算关系表达式的值
② 如果关系表达式的值为true就执行语句体
③ 如果关系表达式的值为false就不执行语句体
④ 继续执行后面的其他语句

4. 样例

package com.itheima.ifdemo;
import java.util.Scanner;
public class IfDemo1{public static void main(String[] args){//需求:键盘录入女婿酒量,如果大于2斤,老丈人给出回应,反之不回应//if格式//if(关系表达式){//语句体;//}//分析://1.键盘录入女婿的酒量Scanner sc = new Scanner(System.in);System.out.println("请输入女婿的酒量");int wine = sc.nextInt();//2.对酒量进行判断if(wine > 2){System.out.println("小伙子,不错呦!!");}}
}
package com.itheima.ifdemo;/*if(关系表达式){语句体;}if的注意点:1.大括号的开头可以另起一行书写,但是建议写在第一行的末尾2.在语句体中,如果只有一句代码,大括号可以省略不写,个人建议,大括号还是不要省略3.如果对一个布尔类型的变量进行判断,不要用==号,直接把变量写在小括号即可
*/
public class IfDemo2{public static void main(String[] args){/* int number = 20;if(number >= 10)//int a = 100;//1.定义变量a 2.给变量a赋值为100System.out.println("number大于等于10");*/boolean flag = true;if(flag){System.out.println("flag的值为true");}}
}

5. 练习

package com.itheima.test;
public class Test1{public static void main(String[] args){//小红:如果你这次考试全班第一,我就做你女朋友//分析://1.定义变量记录小明的名次int ranking = 2;//2.对小明的名次进行判断if(ranking == 1){System.out.println("小红成为了小明的女朋友");}}
}
package com.itheima.test;
public class Test2{public static void main(String[] args){//汽车无人驾驶会涉及到大量的判断//当汽车行驶的时候遇到了红绿灯,就会进行判断//如果红灯亮,就停止//如果黄灯亮,就减速//如果绿灯亮,就行驶//1.定义三个变量表示灯的状态//true 亮 false 灭boolean isLightGreen = false;boolean isLightYellow = false;boolean isLightRed = true;//2.判断//红灯亮,就停止//黄灯亮,就减速//绿灯亮,就行驶if(isLightGreen){System.out.println("GoGoGo!!!");}if(isLightYellow){System.out.println("slow!!!");}        if(isLightRed){System.out.println("stop!!!");}}
}

三、if 的第二种格式

1. 格式

if (关系表达式){语句体1;
} else {语句体2;
}

2. 执行流程

① 首先计算关系表达式的值
② 如果关系表达式的值为true就执行语句体1
③ 如果关系表达式的值为false就执行语句体2
④ 继续执行后面的其他语句

3. 练习

package com.itheima.test;
public class Test3{public static void main(String[] args){/*需求:键盘录入一个整数,表示身上的钱如果大于等于100块,就是网红餐厅否则,就吃经济实惠的沙县小吃*///分析://1.键盘录入一个整数,表示身上的钱Scanner sc = new Scanner(System.in);System.out.println("请输入身上的钱");int money = sc.nextInt();//2.对钱进行判断(二选一)if(money >=100){System.out.println("吃网红餐厅");}else{System.out.println("吃经济实惠的沙县小吃");}}
}
package com.itheima.test;
public class Test4{public static void main(String[] args){/*在实际开发中,如果要根据两种不同的情况来执行不同的代码,就需要用到if的第二种格式。需求:假设,用户在超市实际购买,商品总价为:600元键盘录入一个整数表示用户实际支付的钱如果付款大于等于600,表示付款成功,否则付款失败*///分析//1.键盘录入一个整数表示用户实际支付的钱Scanner sc = new Scanner(System.in);System.out.println("录入一个整数表示实际支付的钱");int money = sc.nextInt();//2.判断if(money >= 600){System.out.println("付款成功");}else{System.out.println("付款失败");}}
}
package com.itheima.test;
public class Test5{public static void main(String[] args){/*在实际开发中,电影院选座也会使用到if判断假设某影院售卖了100张票,票的序号为1~100其中奇数票号坐左侧,偶数票号坐右侧键盘录入一个整数表示电影票的票号根据不同情况,给出不同的提示如果票号为奇数,那么打印坐左边如果票号为偶数,那么打印坐右边*///分析://1.键盘录入一个整数表示电影的票号Scanner sc = new Scanner(System.in);System.out.println("请输入一个票号");int ticket = sc.nextInt();//只有当ticket在0~100之间,才是真实有效的票if(ticket >= 0 && ticket <= 100){//if的嵌套//2.判断票号是奇数还是偶数if(ticket % 2 == 1){System.out.println("坐左边");}else{System.out.println("坐右边");}}}
}

四、if 的第三种格式

1. 格式

if (关系表达式1){语句体1;
}else if(关系表达式2){语句体2;
}……
else{语句体 n + 1}

2. 执行流程

① 首先计算关系表达式1的值
② 如果为true就执行语句体1;
③ 如果为true就执行语句体2;
④……
⑤ 如果所有关系表达式结果都为false,就执行语句n+1

3. 练习

package com.itheima.test;
public class Test6{public static void main(String[] args){/*根据不同的分数送不同的礼物如果是95~100分,送自行车一辆如果是90~94分,游乐场玩一天如果是80~89分,送变形金刚一个如果是80分以下,揍一顿*///分析://1.键盘录入小明的考试成绩Scanner sc = new Scanner(System.in);System.out.println("请录入一个整数表示小明的成绩");int score = sc.nextInt();//对异常数据进行判断//0~100合理数据if(score > 0 && score <= 100){//2.根据不同的考试成绩,给出不同的奖励if(score >= 95 && score <= 100){System.out.println("送自行车一辆");}else if(score >= 90 && score <= 94){System.out.println("游乐场玩一天");}else if(score >= 80 && score <= 89){System.out.println("送变形金刚一个");}else{System.out.println("揍一顿");}}else{System.out.println("当前录入的成绩不合法");}}
}
package com.itheima.test;
public class Test7{public static void main(String[] args){/*在实际开发中,多种情况判断时,会用到if的第三种形式需求:商场都会有VIP的会员制,根据不同的会员会有不同的折扣假设商品总价为1000键盘录入会员级别,并计算出实际支付的钱会员1级:打9折会员2级:打8折会员3级:打7折非会员:不打折,要打也是打骨折*///分析://1.定义变量记录总价int price = 1000;//2.键盘录入会员的级别Scanner sc = new Scanner(System.in);System.out.println("请录入会员的级别");int vip = sc.nextInt();//3.根据级别来计算实际要支付的钱if(vip == 1){System.out.println("实际支付的钱为:"+(price * 0.9));         }else if(vip == 2){System.out.println("实际支付的钱为:"+(price * 0.8));}else if(vip == 2){System.out.println("实际支付的钱为:"+(price * 0.7));}else{System.out.println("实际支付的钱为:"+price);}}
}
package com.itheima.test;
public class Test8{public static void main(String[] args){//汽车无人驾驶会涉及到大量的判断//当汽车行驶的时候遇到了红绿灯,就会进行判断//如果红灯亮,就停止//如果黄灯亮,就减速//如果绿灯亮,就行驶//1.定义三个变量表示灯的状态//true 亮 false 灭boolean isLightGreen = false;boolean isLightYellow = false;boolean isLightRed = true;//2.判断//红灯亮,就停止//黄灯亮,就减速//绿灯亮,就行驶if(isLightGreen){System.out.println("GoGoGo!!!");}else if(isLightYellow){System.out.println("slow!!!");}else if(isLightRed){System.out.println("stop!!!");}}
}

五、switch语句和练习

1. 格式

switch(表达式){case1:语句体1;break;case2:语句体2;break……default:语句体n+1;break;
}

2. 格式说明

① 表达式:(将要匹配的值)取值为byte、short、int、char
JDK5以后可以是枚举,JDK7以后可以是String
② case:后面跟的是要和表达式进行比较的值(被匹配的值)
③ break:表示中断,结束的意思,用来结束switch语句
④ default:表示所有情况都不匹配的时候,就执行该处的内容,和if语句的else相似
⑤ case后面的值只能是字面量,不能是变量
⑥ case给出的值不允许重复

3. 执行流程

① 首先计算表达式的值
② 依次和case后面的值进行比较,如果有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结束
③ 如果所有的case后面的值和表达式的值都不匹配,就会执行default里面的语句体,然后结束整个switch语句

4. 样例

package com.itheima.switchdemo;
public class SwitchDemo1{public static void main(String[] args){//兰州拉面、武汉热干面、北京炸酱面、陕西油泼面//1.定义变量记录我心里想吃的面String noodles = "海鲜龙虾面";//2.拿着这个面利用switch跟四种面匹配switch(noodles){case "兰州拉面":System.out.println("吃兰州拉面");break;case "武汉热干面":System.out.println("吃武汉热干面");break;case "北京炸酱面":System.out.println("吃北京炸酱面");break;case "陕西油泼面":System.out.println("吃陕西油泼面");break;default:System.out.println("吃方便面");break;}}
}

5. 练习

package com.itheima.test;
import java.util.Scanner;
public class Test9{public static void main(String[] args){/*需求:键盘录入星期数,显示今天的减肥活动周一:跑步周二:游泳周三:慢走周四:动感单车周五:拳击周六:爬山周日:好好吃一顿*///分析://1.键盘录入星期数Scanner sc = new Scanner(System.in)System.out.println("请输入星期");int week = sc.nextInt();//2.利用switch对星期进行匹配switch(week){case 1:System.out.println("跑步");break;case 2:System.out.println("游泳");break;case 3:System.out.println("慢走");break;case 4:System.out.println("动感单车");break;case 5:System.out.println("拳击");break;case 6:System.out.println("爬山");break;case 7:System.out.println("好好吃一顿");break;default:System.out.println("没有这个星期");break;}}
}

六、switch的扩展知识点和练习

1. default的位置和省略

package com.itheima.switchdemo;
/*default的位置和省略1.位置:default不一定写在最下面,我们可以写在任意位置,只不过习惯会写在最下面2.省略:default可以省略,语法不会有问题,但是不建议省略
*/
public class SwitchDemo2{public static void main(String[] args){int number = 1;switch(number){case 1:System.out.println("number的值为1");break;case 10:System.out.println("number的值为10");break;case 20:System.out.println("number的值为20");break;default:System.out.println("number的值不是1,10或者20");break;}}
}

2. case穿透

package com.itheima.switchdemo;
/*case穿透:就是语句体中没有写break导致的执行流程:首先还是会拿着小括号中表达式的值跟下面每一个case进行匹配如果匹配上了,就会执行对应的语句体,如果此时发现了break,那么结束整个switch语句如果没有发现break,那么程序会继续执行下一个case语句体,一直到break或者右大括号为止使用场景:如果多个case的语句重复了,那么我们考虑利用case穿透去简化代码*/
public class SwitchDemo3{public static void main(String[] args){int number = 1;switch(number){case 1:System.out.println("number的值为1");//break;case 10:System.out.println("number的值为10");//break;case 20:System.out.println("number的值为20");//break;default:System.out.println("number的值不是1,10或者20");//break;}}
}

3. switch新特性

package com.itheima.switchdemo;
/*switch新特性JDK12
*/
public class SwitchDemo4{public static void main(String[] args){//需求://1 2 3 一 二 三/*int number = 1;switch(number){case 1:System.out.println("一");break;case 2:System.out.println("二");break;case 3:System.out.println("三");break;default:System.out.println("没有这种选项");break; }*//*int number = 1;switch(number){case 1 -> {System.out.println("一");}case 2 -> {System.out.println("二");}case 3 -> {System.out.println("三");}default -> {System.out.println("没有这种选项");}}*/int number = 1;switch(number){case 1 -> System.out.println("一");case 2 -> System.out.println("二");case 3 -> System.out.println("三");default -> System.out.println("没有这种选项");}                }
}

4. switch和if第三种格式各自的使用场景

package com.itheima.switchdemo;
/*switch和if第三种格式各自的使用场景if的第三种格式:一般用于对范围的判断switch:把有限个数据一一列举出来,让我们选其一
*/
public class SwitchDemo5{public static void main(String[] args){int score = 100;if(score >= 90 && score <= 100){System.out.println("送自行车");}}
}

5. 练习

package com.itheima.test;
import java.util.Scanner;
public class Test10{public static void main(String[] args){/*需求:键盘录入星期数,输出工作日、休息日(1-5)工作日,(6-7)休息日*///分析://1.键盘录入星期数Scanner sc = new Scanner(System.in);System.out.println("请输入一个整数表示星期");int week = sc.nextInt();//2.利用switch语句进行选择switch(week){case 1,2,3,4,5 -> System.out.println("工作日");case 6,7 -> System.out.println("休息日");default -> System.out.println("没有这个星期");}}
}
package com.itheima.test;
import java.util.Scanner;
public class Test11{public static void main(String[] args){/*在实际开发中,如果我们需要在多种情况下选择其中一个,就可以使用switch语句当我们拨打了某些服务电话时,一般都会有按键选择假设现在我们拨打了一个机票预定电话电话中语音提示:1机票查询2机票预定3机票改签4退出服务其他按键也是退出服务,请使用switch模拟该业务逻辑*///分析://1.键盘录入一个整数表示我们的选择Scanner sc = new Scanner(System.in);System.out.println("请输入您的选择");int choose = sc.nextInt();//2.根据选择执行不同的代码switch(choose){case 1 -> System.out.println("机票查询");case 2 -> System.out.println("机票预定");case 3 -> System.out.println("机票改签");//case 4 -> System.out.println("退出服务");default -> System.out.println("退出服务");}}
}

七、for循环格式和练习

1. for循环

① 格式

for(初始化语句;条件判断语句;条件控制语句){循环体语句;
}

② 样例

for(int i = 1;i <= 10;i++){System.out.println("HelloWorld");
}

2. 执行流程

① 执行初始化语句
② 执行条件判断语句,看其结果是true还是false
如果是false,循环结束
如果是true,执行循环体语句
③ 执行条件控制语句
④ 回到②继续执行条件判断语句

3. 样例

package com.itheima.loopdemo;
public class ForDemo1{public static void main(){//1.需求:打印5次HelloWorld//分析//i 1~5/*for(初始化语句;条件判断语句;条件控制语句){循环体;}*/for(int i = 1;i <= 5;i++){System.out.println("HelloWorld");}}
}

4. 练习

package com.itheima.test;
import java.util.Scanner;
public class Test12{public static void main(String[] args){//需求1:打印1-5//分析://开始条件:1//结束条件:5for(int i = 1;i <= 5;i++){//第一次循环:i=1//第二次循环:i=2//i:1 2 3 4 5 System.out.println(i);}//需求2:打印5-1//分析://开始条件:5//结束条件:1for(int i = 5;i >=1;i--){//第一次循环:i=5//第二次循环:i=4//i:1 2 3 4 5 System.out.println(i);}}
}
package com.itheima.test;
import java.util.Scanner;
public class Test13{public static void main(String[] args){/*在实际开发中,需要重复执行的代码,会选择循环实现比如:玩游戏的时候,如果网不好那么会经常断线重连那么断线重连这个业务逻辑就需要重复执行假设现在公司要求,断线重连的业务逻辑最多只写5次备注:断线重连业务逻辑可以用输出语句替代*///分析//1.因为我们需要重复执行某段代码,所以需要用循环解决//循环的次数:5次//开始条件:1//结束条件:5for(int i = 1;i <=5;i++){System.out.println("第" + i + "次执行断线重连的业务逻辑");}}
}

八、for循环练习-累加思想和统计思想

练习

package com.itheima.test;
import java.util.Scanner;
public class Test14{public static void main(String[] args){/*需求:在实际开发中,如果要获取一个范围中的每一个数据时,也会用到循环比如:求1-5之间的和*///分析://1.循环1~5得到里面的每一个数字//开始条件:1//结束条件:5//用来进行累加int sum = 0;for(int i = 1;i <= 100;i++){//扩展小点//1.求和的变量不能定义在循环的里面,因为变量只在所属的大括号中有效//2.如果我们把变量定义在循环的里面,那么当前变量只能在本次循环中有效//当本次循环结束后,变量就会从内存中消失//当第二次循环开始的时候,又会重新定义一个新的变量                  //结论:如果以后我们要写累加求和的变量,可以把变量定义在循环的外面//int sum = 0;//System.out.println(i);//可以把得到的每一个数字累加到变量sum当中sum = sum + i; //sum += i;//System.out.println(sum);}//当循环结束之后,表示已经把1~5累加到变量sum当中了System.out.println(sum);}
}
package com.itheima.test;
import java.util.Scanner;
public class Test15{public static void main(String[] args){/*需求:在实际开发中,如果要获取一个范围中的每一个数据时,会用到循环。但是此时,大多数情况下,不会获取所有的数据,而是获取其中符合要求的数据。此时就需要循环和其他语句结合使用了。比如:求1-100之间的偶数和*///分析://1.获取1~100之间的每个数int sum = 0;for(int i = 1;i <= 100;i++){//2.累加求和(先判断,再求和)if(i % 2 == 0){sum = sum + i;}}//打印sumSystem.out.println(sum);}
}
package com.itheima.test;
import java.util.Scanner;
public class Test16{public static void main(String[] args){/*需求:键盘录入两个数字,表示一个范围统计这个范围中既能被3整除,又能被5整除数字有多少*///分析://1.键盘录入两个数字Scanner sc = new Scanner(System.in);System.out.println("请录入一个数字表示范围的开始");int start = sc.nextInt();System.out.println("请输入一个数字表示范围的结束");int end = sc.nextInt();//统计变量//简单理解:统计符合要求的数字的个数//2.利用循环获取这个范围中的每一个数字//开始条件:start//结束条件:endfor(int i = start;i <= end;i++){//3.对每一个数字进行判断,统计有多少个满足要求的数字if(i % 3 == 0 && i % 5 == 0){//System.out.println(i);count++;}}System.out.println(count);}
}

九、循环语句-while

1. 格式

初始化语句;
while(条件判断语句){循环体语句;条件控制语句;
}

2. 执行流程

① 初始化语句只执行一次
② 判断语句为true,循环继续
③ 判断语句为false,循环结束

3. 样例

package com.itheima.loopdemo;
public class WhileDemo1{public static void main(String[] args){//需求:利用while循环打印1~100//分析://开始条件:1//结束条件:100int i = 1;while(i<=100){System.out.println(i);i++;}}
}

4. for和while的对比

相同点:运行规则都是一样的
区别:
① for循环中,控制循环的变量,因为归属for循环的语法结构中,在for循环结束后,就不能再次被访问到了
② while循环中,控制循环的变量,对于while循环来说不归属其语法结构中,在while循环结束后,该变量还可以继续使用
③ for循环中:知道循环次数或者循环范围
④ while循环:不知道循环次数和范围,只知道循环的结束条件

5. 练习

package com.itheima.text;
public class test17{public static void main(String[] args){/*需求:世界最高山峰是珠穆朗玛峰(8844.43米),假如我有一张足够大的纸,它的厚度是0.1毫米请问,我折叠多少次,可以折成珠穆朗玛峰的高度?*///分析:折叠纸张,每一次折叠纸张的厚度都是原先的两倍//double a = 0.1;//a = a * 2// a *= 2//1.定义一个变量用来记录山峰的高度double height = 8844430;//2.定义一个变量用来记录纸张的初始厚度double paper = 0.1;//3.定义一个变量用来统计次数int count = 0;//4.虚幻折叠纸张,只要纸张的厚度小于山峰的高度,那么循环就继续//每折叠一次,统计次数就++//选择while理由:此时我们不知道循环的次数也不知道循环的范围,只知道循环的结束条件,所以用whilewhile(paper < height){//折叠纸张paper = paper * 2;//折叠一次,++一次count++;}//当循环结束后,count记录的值就是折叠的次数System.out.println(count);}
}

十、两道力扣算法题和do…while循环

1. 两道力扣算法体

package com.itheima.text;
public class test18{public static void main(String[] args){/*需求:给你一个整数 x 如果x是一个回文整数,打印true,否则,返回false解释:回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数例如:121是回文,而123不是*///核心思路:把数字倒过来跟原来的数字进行比较//1.定义数字int x = 12345;//定义一个临时变量用于记录x原来的值,用于最后进行比较int temp = x;//记录倒过来之后的结果int num = 0;//2.利用循环开始 while(x != 0){//从右往左获取每一位数字int ge = x % 10;//修改一下x记录的值x = x / 10;//把当前获取到的数字拼接到最右边num = num * 10 + ge;}//3.打印numSystem.out.println(num);//54321System.out.println(x);//0//4.比较System.out.print(num == temp);//1.定义数字int x = 12;//2.获取个位int ge = x % 10;//2//获取十位int shi = x / 10 % 10;//1//拼接int result = ge * 10 +shi;}
}
package com.itheima.text;
public class test19{public static void main(String[] args){/*需求:给定两个整数,被除数和除数(都是正数,且不超过int的范围)将两数相除,要求不使用乘法,除法和%运算符得到商和余数分析:被除数 / 除数 = 商 ... 余数int a = 100;int b = 10;100 - 10 = 9090 - 10 = 8080 - 10 = 7070 - 10 = 60...10 - 10 = 0(余数)*///1.定义变量记录被除数int dividend = 100;//2.定义变量记录除数int divisor = 3;//3.定义一个变量用来统计相减了多少次int count = 0;//循环 while//在循环中,不断的用被除数-除数//只要被除数是大于等于除数的,那么就一直循环while(dividend >= divisor){dividend = dividend - divisor;//只要减一次,那么统计变量就自增一次count++;}//当循环结束之后dividend变量记录的就是余数System.out.println("余数为:"+dividend);//当循环结束之后,count记录的就是商System.out.println("商为:"+count);}
}

2. do…while循环
① 格式

初始化语句
do{循环体语句;条件控制语句;
}while(条件判断语句);

② 执行流程:先执行后判断

附:更多内容

序号文章目录直达链接
1Java入门https://want595.blog.csdn.net/article/details/130318855
2Java基础概念https://want595.blog.csdn.net/article/details/130337370
3运算符https://want595.blog.csdn.net/article/details/130358604
4判断和循环https://want595.blog.csdn.net/article/details/130367081
5数组https://want595.blog.csdn.net/article/details/130399064
6方法https://want595.blog.csdn.net/article/details/130407188
7面向对象https://want595.blog.csdn.net/article/details/130421064
8字符串https://want595.blog.csdn.net/article/details/130432165
9ArrayListhttps://want595.blog.csdn.net/article/details/130447360


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部