Java day03

news/2024/9/25 16:13:27/文章来源:https://www.cnblogs.com/lsb021214/p/18431559

java 03

1、if判断语句

1、定义语句1

选择结构:if选择语句switch选择语句if选择语句:语句定义格式1:if(关系表达式){语句体;}

注意

注意事项:1、if小括号中的语句,可以很复杂,但是最终的结果一定是boolean类型2、只有当if语句体中的语句只有一行的时候,大括号才可以省略,建议永远不要省略大括号3、小括号后面可以添加分号,相当于if语句拥有一个空的语句体

代码示例

public class IfDemo1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入江川的性别:");String gender = sc.next();//A//B//A.equals(B)if("男".equals(gender));{System.out.println("去男厕所");System.out.println("洗手");}}
}

2、定义语句2

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

注意

注意:if-else语句中,只会执行其中某一个语句体,不会同时都执行!

代码示例

public class IfDemo2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入江川的性别:");String gender = sc.next();//A//B//A.equals(B)if("nan".equals(gender)){System.out.println("去男厕所");System.out.println("洗手");}else {System.out.println("去女厕所");System.out.println("洗手");}}
}

3、定义语句3

if语句的第三种格式:if(关系表达式1){语句体1;}else if(关系表达式2){语句体2;}...{语句体n;}else{语句体n+1;}

代码示例

/*
需求:1、通过把学生考试成绩分等级来引出if语句的第三种格式90-100 优秀80-90  好70-80  良60-70  及格60一下   不及格
*/public class IfDemo3 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入江川的考试成绩:");int score = sc.nextInt();if(score>=90 && score<=100){System.out.println("优秀");}else if(score>=80 && score<90){System.out.println("好");}else if(score>=70 && score<80){System.out.println("良");}else if(score>=60 && score<70){System.out.println("及格");}else if(score>=0 && score<60){System.out.println("不及格");}else {System.out.println("成绩有误!");}}
}

4、练习

//获取三个数据中的最大值
public class IfTest1 {public static void main(String[] args) {int a = 4;int b = 9;int c = 6;if(a<b){if(b<c){System.out.println("c是最大值");}else {System.out.println("b是最大值");}}else {if(a>c){System.out.println("a是最大值");}else {System.out.println("c是最大值");}}}
}

2、switch语句

switch选择语句:语句定义格式:switch(表达式){case 常量值1:表达式1;break;case 常量值2:表达式2;break;...default:表达式n;break;}执行流程:严格按照下面的流程执行。1、先计算表达式中的值2、然后拿着计算出来的值自上而下匹配所有的case,当某一个case后面的常量值与计算出来的值一样的时候,执行其中的语句体,遇到break,结束整个switch语句.3、当所有的case都不匹配的时候,执行default中的语句体,当遇到break的时候,结束整个switch语句.注意:1、表达式的取值:byte,short,int,char,枚举,String2、case后面只能跟常量,不能是变量3、break能不能不写?能不写,但是会发生switch穿透!4、default语句能不能不写?可以不写,但是为了程序的严谨性,最好加上5、default语句是不是一定要放在所有case之后呢?不一定,可以放在switch语句中的任意位置

代码示例

public class SwitchDemo {public static void main(String[] args) {//假设我们所带的金额,正好是购买一瓶饮料的价格,多了或少了都买不了,只有正好才可以消费//可乐 3,脉动 5,红牛 7,ad钙奶 4System.out.println("请输入您带的金额:");Scanner sc = new Scanner(System.in);int price = sc.nextInt();switch (price) {case 3:System.out.println("欢迎购买一瓶 可乐!");break;case 4:System.out.println("欢迎购买一瓶 ad钙奶!");break;case 5:System.out.println("欢迎购买一瓶 脉动!");break;case 7:System.out.println("欢迎购买一瓶 红牛!");break;default:System.out.println("没有您所带金额的饮料!!");break;}}
}

练习

/*输入月份输出季节:春季 3-5夏季 6-8秋季 9-11冬季 12-2*/public class SwitchDemo2 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入需要查看季节的月份:");int season = sc.nextInt();switch (season){case 12:case 1:case 2:System.out.println("冬季");break;case 3:case 4:case 5:System.out.println("春季");break;case 6:case 7:case 8:System.out.println("夏季");break;case 9:case 10:case 11:System.out.println("秋季");break;default:System.out.println("没有该月份。。。");break;}}
}

3、scanner

键盘录入:程序在运行过程中,用户可以根据自己的需求输入参运算的值

实现录入的步骤:

​ 1、导包

import java.util.Scanner;

​ 2、创建键盘录入对象

Scanner scanner = new Scanner(System.in);

​ 3、调用方法键盘录入

​ 1)输入整数

int i = scanner.nextInt();

​ 2)输入字符串

String i = scanner.next();

代码示例

public class ScannerDemo1 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入您的姓名: ");String name = sc.next();System.out.println("请输入您的年龄: ");int age = sc.nextInt();System.out.println("姓名:" + name + ", 年龄:" + age);}
}

4、循环

1、for循环

for循环:

​ 语句定义格式:
​ for(初始化语句;判断条件语句;控制条件语句){

​ 循环体语句;

​ }

注意事项:

​ 1、初始化条件语句,有且仅执行一遍

​ 2、初始化条件语句可以写在for循环的外面,和定义在for循环内部时比较,作用域不同

​ 3、大括号可以省略,但是省略后只能作用在第一条语句上,建议,永远不要省略

​ 4、判断条件语句能否不写?可以不写,但是会变成死循环

​ 5、控制条件语句也可以不写,但是可能会是死循环

一个最简单的for死循环:

​ for(;😉{

​ ......

​ }

//需求:在控制台中输出10行helloworld
public class ForXunDemo1 {public static void main(String[] args) {System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");System.out.println("helloworld");}
}
//用for循环改进
/*初始化语句:定义一个变量表示范围,每循环一次需要改变一次  int i = 1判断条件语句:10行:范围 1-10 i<=10控制条件语句:i++循环体语句:System.out.println("helloworld");
*/
public class ForXunDemo1 {public static void main(String[] args) {for (int i = 1; i <= 10;i++) {System.out.println("helloworld - " + i);}}
}

练习:

package com.shujia.day03;/*请在控制台输出数据1-10请在控制台输出数据10-1求出1-10之间数据之和求出1-100之间偶数和求出1-100之间奇数和求5的阶乘【自己实现】在控制台输出所有的”水仙花数”,统计”水仙花数”共有多少个*/
public class ForTest {public static void main(String[] args) {//请在控制台输出数据1-10for (int i = 1; i <= 10; i++) {System.out.println(i);}System.out.println("------------------------");//请在控制台输出数据10-1for (int i = 10; i > 0; i--) {System.out.println(i);}System.opublic class ForTest {public static void main(String[] args) {//请在控制台输出数据1-10for (int i = 1; i <= 10; i++) {System.out.println(i);}System.out.println("------------------------");for (int i = 1; i <= 10; i++) {sum += i;}System.out.println("1-10之间的和为:" + sum);System.out.println("------------------------");//求出1-100之间偶数和//求出1-100之间奇数和int ouSum = 0;int jiSum = 0;for (int i = 1; i <= 100; i++) {if (i % 2 == 0) {ouSum += i;} else {jiSum += i;}}System.out.println("1-100之间的偶数和为:" + ouSum);System.out.println("1-100之间的奇数和为:" + jiSum);System.out.println("------------------------");int count = 0;for (int i = 100; i < 1000; i++) {int baiWei = i / 100;int shiWei = i % 100 / 10;int geWei = i % 10;if ((baiWei * baiWei * baiWei + shiWei * shiWei * shiWei + geWei * geWei * geWei) == i) {System.out.println(i);count++;}}System.out.println("水仙花数共计 " + count + " 个");}
}

2、while循环

1、定义

while循环:语句定义格式1:初始化条件语句;while(判断条件语句){循环体语句;控制条件语句;}语句定义格式2:初始化条件语句;do{循环体语句;控制条件语句;}while(判断条件语句)

2、注意

 1、while循环可以和for循环等价转换2、for循环和while循环的使用场景:for循环适合明确的范围内循环  【吃葡萄🍇】当循环次数不明确获取就是要求次数的时候,优先考虑while循环【喝水】

代码示例

public class WhileDemo1 {public static void main(String[] args) {//输出10行hello worldfor (int i = 1; i < 0; i++) {System.out.println("hello world");}System.out.println("---------------------------");int i = 1;while (i < 0){System.out.println("hello world");i++;}System.out.println("---------------------------");int i2 = 1;do{System.out.println("hello world");i2++;}while (i2 < 0);}
}

练习:

package com.shujia.day03;/*我国最高山峰是珠穆朗玛峰:8848m,我现在有一张足够大的纸张,厚度为:0.01m。请问,我折叠多少次,就可以保证厚度不低于珠穆朗玛峰的高度?*/
public class WhileTest1 {public static void main(String[] args) {int high = 884800;int thickness = 1;int count = 0;while (thickness<high){thickness *=2;count++;}System.out.println("共折叠 "+count+"次");
}

5、跳转控制语句 方法

跳转控制语句:break关键字continuereturn

1、break

	break的作用:跳出单层循环跳出多层循环带标签的跳出格式:标签名: 循环语句标签名要符合Java的命名规则break: 打破,打碎,终止使用break的注意事项:1、break的使用需要在特定的场景下使用才有意义2、break只能在switch选择语句或者循环中使用
public class BreakDemo {public static void main(String[] args) {
//        break;//输出1-10,当遇到5的时候,使用breakfor(int i=1;i<=10;i++){if(i==5){break; // 终止整个for循环}System.out.println(i);}System.out.println("-------------------------------------");//需求:当j==5的时候,使用breakjc:for (int i = 1; i <= 10; i++) {lg:for (int j = 1; j <= i; j++) {if(j==5){
//                    break; // 终止最近的整个for循环break jc;}System.out.print(j + "*" + i + "=" + (i * j)+"\t");}System.out.println();}}
}

2、continue

continue的使用场景:在循环语句中离开使用场景的存在是没有意义的continue的作用:单层循环对比break,然后总结两个的区别break  退出当前循环continue  退出本次循环

代码示例

public class ContinueDemo {public static void main(String[] args) {for(int i=1;i<=10;i++){if(i==5){continue; // 结束当次循环,继续下一次循环}System.out.println(i);}}
}

3、return

return关键字不是为了跳转出循环体,更常用的功能是结束一个方法,也就是退出一个方法。跳转到上层调用的方法。
return必须在方法中写,一个方法只会有一个return生效,表示结束整个方法
public class ReturnDemo {
//    return; // return必须在方法中写public static void main(String[] args) {
//        return; // 结束整个方法
//        System.out.println("hello world");
//        return;for(int i=1;i<=10;i++){if(i==5){return;}System.out.println(i);}System.out.println("hello world");}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/803524.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AI自动生成代码注释

在vscode 中安装 TONGYI Lingma

通过 Tampermonkey 实现学习通全自动刷课

本文介绍了如何使用 Tampermonkey 这一流行的用户脚本管理器,通过其脚本库实现学习通的全自动刷课。文章详细讲解了 Tampermonkey 的安装步骤、OCS 脚本的配置方法,以及题库的使用流程,帮助读者高效完成学习任务。在学习过程中,自动化工具能大大提升学习效率。Tampermonkey…

KBU1010-ASEMI单向整流桥KBU1010

KBU1010-ASEMI单向整流桥KBU1010编辑:ll KBU1010-ASEMI单向整流桥KBU1010 型号:KBU1010 品牌:ASEMI 封装:KBU-4 批号:2024+ 类型:单向整流桥 电流(ID):10A 电压(VF):1000V 安装方式:直插式封装 特性:大功率、整流扁桥 产品引线数量:4 产品内部芯片个数:4 产品内部…

Kubernetes中Ingress的原理和配置

Ingress的概念和作用 Ingress是Kubernetes集群中的一个对象,用于将外部流量路由到集群内部的服务。它充当了进入Kubernetes集群的API网关,负责接收外部请求,并将其转发到正确的目标服务上。 Ingress通常通过HTTP和HTTPS提供对服务的访问,并支持基于主机名、路径以及其他HTT…

《如 何 速 通 一 套 题》4.0

A sprial 找规律。直接做。 #include <bits/stdc++.h> #define int long long using namespace std;int t, n;int sqrtll(int n) {int l = 1, r = 1000000, ans = 0;for(; l <= r; ) {int mid = (l + r) >> 1;if(mid * mid >= n) {ans = mid, r = mid - 1;}e…

自定义表格样式

HTML:<div class="table-container"><table style="width: 90%; margin-left: 5%"><tr class="table-title"><th style="width: 33%">科室名称</th><th style="width: 33%">当日登录次…

Bash脚本基本语法

一、Bash脚本以及相关介绍Bash脚本是一种在Unix或Linux操作系统中广泛使用的脚本语言,它允许用户编写一系列命令,这些命令将被Bash(Bourne-Again SHell)解释器执行。Bash脚本可以用于自动化各种任务,比如文件管理、程序执行、系统维护等。 编写Bash脚本的基本步骤包括:创…

【vulhub】Discuz-任意文件删除

【vulhub】Discuz-任意文件删除 0x00漏洞介绍 通过配置个人信息的属性值,导致文件删除。 影响版本Discuz <= 3.40x01 搭建环境 数据库服务器填写db(必须db,不然安装失败),数据库名为discuz,数据库账号密码均为root,管理员密码任意。填写联系方式页面直接点击跳过本步…

2024年开源API工具盘点,覆盖API全生命周期

2024年经济持续低迷,本文整理一些免费的开源工具,旨在帮助企业组织降低工具的支出成本,能用免费的何必用付费的呢(狗头)? 如何高效地管理API的全生命周期——从设计、开发、测试、部署到监控和优化,已经成为每个开发者和技术团队关注的重点。以下工具清单,无论你是刚刚…

Day4 与用户交互 + 格式化运算符 + 基本运算符

今天首先对昨天学的进行了复习,由这个复习可以看出昨天的我是多么的水*-*,今天的话倒是学的挺充实的,因为没有像上节课的jupyter notebook那样的东西(这节课看下来还是挺牛的,但也没到非用不可的时候,继续放着吧)。今天主要学习了三大部分,与用户的交互,格式化占位符,…

ACCESS 关于MSCOMCT2.OCX和MSCOMCTL.OCX报错的解决方案

我在ACCESS中添加了TreeView插件,结果电脑A上打开没问题,电脑B打开时就报MSCOMCT2.OCX和MSCOMCTL.OCX有错. B电脑之前是可以正常使用的,但是加了TreeView插件之后报错,说明是插件引用的问题. 在网上下载了这两个文件,复制到C:\WINDOWS\SYSTEM32目录下. 重新注册了这两个文件: 以…

74hc595

74htc595 功能 8位串行输入 8位串行或并行输出 带3态输出的存储 寄存器带直接清零的移位寄存器100 MHZ(典型) 移出频率 ESD保护H BM ELAJESD22-A114-A超过2000VMM EIAJESD23-A115-A超过200 V说明 74HC/HCT595是高速硅栅CMOS器件, 与低功率肖特基TTLLSTTL引脚兼容。 它们是根…