【判断和循环

编程入门 行业动态 更新时间:2024-10-11 21:28:20

【判断和循环

【判断和循环

顺序结构

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

package com.hh.demo.OrderDemo;
public class OrderDemo {public static void main(String[] args) {System.out.println("a");System.out.println("b");System.out.println("c");System.out.println("d");}
}
//a
//b
//c
//d

分支结构

if语句

第一种格式

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

执行流程
1.首先计算关系表达式的值
2.如果关系表达式的值为true就执行语句体
3.如果关系表达式的值为false就不执行语句体
4.继续执行后面的其他语句
注意
1.大括号开头可以另起一行书写,但建议写在第一行末尾
2.在语句体中,如果只有一句代码,大括号可以省略不写.建议不要省略
3.如果对一句布尔类型的变量进行判断,不要用==号,直接将变量写在小括号中即可
练习
键盘录入一个人酒量,如果大于2斤,给出回应,反之不回应

import java.util.Scanner;
public class IfDemo1 {public static void main(String[] args) {//键盘录入一个人酒量,如果大于2斤,给出回应,反之不回应Scanner sc = new Scanner(System.in);System.out.println("请输入酒量");int sum = sc.nextInt();if(sum > 2){System.out.println("给出回应");}}
}

练习:考试奖励
小红;如果你考全班第一,我就会做你女朋友

package com.hh.demo.test;
public class test1 {public static void main(String[] args) {//小红;如果你考全班第一,我就会做你女朋友//定义变量记录小明名词int ranking = 1;if(ranking == 1){System.out.println("小红成为小明的女朋友");}}
}

练习:自动驾驶
当前灯为红灯则停止,为绿灯则出发

package com.hh.demo.test;
public class test2 {public static void main(String[] args) {boolean isLightRed = true;boolean isLightGreen = false;boolean isLightYellow = false;if(isLightRed){System.out.println("stop");}if(isLightGreen){System.out.println("go");}if(isLightYellow){System.out.println("slow");}}
}

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

第二种格式

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

执行流程
1.首先计算关系表达式的值
2.如果关系表达式的值为true就执行语句体1
3.如果关系表达式的值为false就执行语句体2
4.继续执行后面的其他语句

练习
键盘录入一个整数,表示身上的钱.如果大于等于100,就吃网红餐厅;否则,就吃沙县小吃

package com.hh.demo.Ifdemo;
import java.util.Scanner;
public class ifDemo2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入一个整数");int money = sc.nextInt();if(money >= 100){System.out.println("吃网红餐厅");}else{System.out.println("吃沙县小吃");}}
}

练习:商品付款
假设用户在超市实际购买商品总价为600元,键盘录入整数表示用户实际支付的钱,如果付款金额大于等于600,表示付款成功;否则付款失败.

package com.hh.demo.test;
import java.util.Scanner;
public class test3 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入用户付款金额");int money = sc.nextInt();if(money >= 600) {System.out.println("付款成功");}else{System.out.println("付款失败");}}
}

练习:影院选座
票号1-100,如果买到奇数票号坐左侧,如果是偶数票号坐右侧

package com.hh.demo.test;
import java.util.Scanner;
public class test4 {public static void main(String[] args) {//票号1-100,如果买到奇数票号坐左侧,如果是偶数票号坐右侧Scanner sc = new Scanner(System.in);System.out.println("请输入购买的票号");int number = sc.nextInt();//只有在1-100的票才有效if (number >= 1 && number <= 100) {if (number % 2 == 0) {System.out.println("顾客坐右侧");} else {System.out.println("顾客坐左侧");}}}
}

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

第三种格式

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

练习:太简单了懒得写
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

switch语句

switch(表达式){case 值1:语句体1;break;case 值2:语句体2;break;...default:语句体n+1;break;
}

格式说明
1.表达式:(将要匹配的值)取值为byte.short.int.char.JDK 5以后可以是枚举,JDK7以后是String
2.case:后面跟的是和表达式进行比较的值(被匹配的值)
3.default:如果所有情况都不匹配时,就执行该处的内容,和If语句的else相似
4.case后面的值只能是字面量,不能是变量
5.case给出的值不允许重复

练习

package com.hh.demo.switchDemo;
public class SwitchDemo1 {public static void main(String[] args) {String wantEat = "热干面";switch(wantEat){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;}}
}

练习:运动计划
键盘输入星期数,显示当天的减肥活动

package com.hh.demo.test;
import java.util.Scanner;
public class test5 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入星期几");int day = sc.nextInt();switch(day){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的位置和省略
位置:default不一定写在最下面,可以写在任意位置,只不过习惯会写在最下面
省略:default可以省略,与发布会有问题,但不建议省略
2.case穿透(向下穿透)
语句体当中没有写break,程序会继续执行下一个xase的语句体,一直到有break或大括号为止
3.JDK12之后的新的写法

package com.hh.demo.test;
public class test6 {public static void main(String[] args) {int number = 1;switch(number){case 1 -> System.out.println("yi");case 2 -> System.out.println("er");case 3 -> System.out.println("san");default -> System.out.println("没有匹配项");}}
}

4.switch和if第三种格式的使用场景
练习:休息日和工作日
键盘录入星期数,输出工作日.休息日

package com.hh.demo.test;
import java.util.Scanner;
public class test6 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入星期号");int day = sc.nextInt();switch(day){case 1,2,3,4,5 -> System.out.println("工作日");case 6,7 -> System.out.println("休息日");default -> System.out.println("没有这一天");}}
}

练习:打电话时的按键选择

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

循环结构

for循环

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

练习:打印5次helloworld

package com.hh.demo.forDemo;
public class ForDemo1 {public static void main(String[] args) {for (int i = 1; i <= 5; i++) {System.out.println("helloworld ");}}
}

练习:输出数字1-5和5-1

package com.hh.demo.test;
public class test6 {public static void main(String[] args) {for(int i = 1; i <= 5; i++){System.out.println(i);}for(int j = 5; j >= 1; j--){System.out.println(j);}}
}

练习:输出数字1-5的和

package com.hh.demo.test;
public class test7 {public static void main(String[] args) {//输出数字1-5的和int sum = 0;for(int i = 1; i <=5 ; i++){sum += i;}System.out.println(sum);}
}

练习:求1-100之间的偶数和

package com.hh.demo.test;
public class test8 {public static void main(String[] args) {int sum = 0;for(int i = 1; i <=100; i++){if(i % 2 == 0)sum += i;}System.out.println(sum);}
}

练习:键盘录入两个数字,表示一个范围,统计这个范围内既能被3整除,又能被5整除的数字有多少个

package com.hh.demo.test;
import java.util.Scanner;
public class test9 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入两个数字");int lift = sc.nextInt();int right = sc.nextInt();int member = 0;for(int i = lift; i <= right; i++){if(i % 3 == 0 && i % 5 == 0){member++;}}System.out.println(member);}
}

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

while 循环

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

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

练习:已知珠穆朗玛峰的高度时8844430毫米,假如有一张足够大的纸,它的厚度是0.1毫米.请问我折叠多少次,可以遮城珠穆朗玛峰的高度

package com.hh.demo.test;
public class test10 {public static void main(String[] args) {double i = 0.1;int time = 0;while(i<8844430){time++;i *= 2;}System.out.println(time);}
}

[Leecode原题]给你一个整数,如果x是一个回文整数打印true,否则打印false
回文数是指正序和倒叙读都是一样的整数 eg:121
提示:把原来的数倒过来比一下就可以

package com.hh.demo.test;
public class test11 {public static void main(String[] args) {int x = 123;int temp = x;int a = 0;while(temp>=10){a += temp % 10;a *= 10;temp /= 10;}a += temp;System.out.println(a == x);}
}

[Leecode原题]给定两个整数,被除数.除数(都是正数,且不超过int范围)将两数相除,要求不使用乘法,除法,%运算符,得到商和余数

package com.hh.demo.test;
public class test12 {public static void main(String[] args) {int dividend = 100;int divider = 10;int num = 0;while(dividend >= divider){dividend -= divider;num++;}System.out.println(num);System.out.println(dividend);}
}

更多推荐

【判断和循环

本文发布于:2024-02-06 02:15:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1745570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!