流程控制
顺序结构(代码顺序执行)
在没有其他结构干扰的情况下,任何代码,都是从上到下依次执行
分支结构
在代码中,有的时候,执行某些代码,需要满足一定的条件,就需要使用到分支结构
常用的分支语句,就是 if(){} -- else{} 结构
语法1 : 如果表达式的结果是true,那么就执行它的代码块的内容
if(表达式){
代码块;
}
语法2 : 如果表达式结果为true,那么执行代码块1的内容,如果是false,执行代码块2
(这种结构的分支,都可以转为三元表达式)
if(表达式){
代码块1;
}else{
代码块2;
}
语法3: 如果表达式1的结果是true,执行代码块1,表达式2的结果为true,执行代码块2,.....表达式都不为true,则执行代码块4
if(表达式1){
代码块1;
}else if(表达式2){
代码块2;
}else if(表达式3){
代码块3;
}else{
代码块4;
}
开关语句 switch
语法 : 判断switch后面的变量的值,如果变量的值等于值1,执行代码块1,等于值2,执行代码块2,等于值3,执行代码块3...
都不满足,执行default后 的默认代码块
switch (变量){
case 值1 :
代码块1;
break;
case 值2 :
代码块2;
break;
case 值3 :
代码块3;
break;
....
default:
默认代码块;
}
使用switch注意的问题 :
1,switch后面的括号中,可以用的数据类型 : byte、short、int、char、String,以及对应的包装类,不能使用long或者浮点型
2,case后面的break可以省略,但是会发生case穿透现象,一般不建议省略
3,case后面,只能是常量,不能是变量,而且case后面的值不可重复
4,default可以不用写,但是不满足条件的情况就无法执行
使用switch完成,让用户输入一个数字(1-7),后台输出对应的内容 ,比如,输入1,则输出 ,今天是星期一,输入超过范围的值则返回输入错误
循环结构
循环其实就是可以将一些重复性的代码内容,整理起来,利用特殊的语法,完成重复性的操作,从而让代码变的更加简单、简洁
循环必要条件 :
1,需要一个循环结束条件
2,需要一个控制条件变化的代码块
while循环
语法 : 如果表达式的结果是一个布尔值,如果为true,就会执行代码块的内容
while(表达式){
代码块;
}
public class Demo01 {
public static void main(String[] args) {
//System.out.println(1);
//System.out.println(2);
//System.out.println(3);
//System.out.println(4);
//System.out.println(5);
//System.out.println(6);
//System.out.println(7);
//System.out.println(8);
//System.out.println(9);
//System.out.println(10);
//使用while循环输出1-10
//1, 循环需要结束条件
//2,循环体代码需要变化
int i = 1;
while ( i <= 10){ //循环结束条件
System.out.println(i);
i++; //循环条件发生改变
}
int b = 1;
while (true){
System.out.println(b);
b++; //循环条件发生改变
if (b >10){ //终止循环的条件
break;
}
}
//使用while循环求 1-100的和
//1+2+3+4+5+....+100
int c = 1; //声明循环的初始条件变量
int sum = 0; //声明求和结果的变量
while (c <= 100){
// c = 1,sum = 1 ; c=2 sum= 1+2 ;c =3 sum = 1+2+3 ;c = 100 sum = 1+2+3+4+..+100
sum = sum + c;
c++;
}
System.out.println("1-100的和是:" + sum);
//求1-100之间的偶数的和
//第一种写法
int x = 1;
int sum1 = 0;
while (x <= 100){
//如果x是偶数,才去累加
if (x % 2 == 0){
sum1 += x;
}
x++;
}
System.out.println("1-100之间的偶数和:" + sum1);
//第二种写法
int y = 0;
int sum2 = 0;
while (y <= 100){
sum2 += y;
y += 2;
}
System.out.println("1-100之间的偶数和:" + sum2);
//第三种写法 continue ,continue表示,跳出当次循环,继续下次循环
int z = 0;
int sum3 = 0;
while (z <= 100){
//如果z是奇数,那么就跳出当次循环,继续下次循环
z++;
if (z % 2 == 1){
continue;
}
sum3 += z;
}
System.out.println("1-100之间的偶数和:" + sum3);
//将刚才的switch语句,改成循环的形式
//用户选择一个功能,执行完后,可以继续选择,直到选择6,退出循环
// 接收用户输入的数字(1-6),返回对应可以做的事情
//1,查询所有用户 2,增加用户 3,删除用户 4,修改用户
// 5,查询单个用户 6,退出功能
//continue
public static void main(String[] args) {
//接收用户输入的数字(1-5),返回对应可以做的事情
//1,查询所有用户 2,增加用户 3,删除用户 4,修改用户 5,查询单个用户
Scanner scanner = new Scanner(System.in);
while (true){
System.out.println("请输入数字1-6,选择对应的操作:");
System.out.println("1,查询所有用户");
System.out.println("2,增加用户");
System.out.println("3,删除用户");
System.out.println("4,修改用户");
System.out.println("5,查询单个用户");
System.out.println("6,退出系统");
int num = scanner.nextInt();
//通过switch去判断
switch (num){
case 1:
System.out.println("查询所有用户!");
continue;
case 2:
System.out.println("增加用户!");
continue;
case 3:
System.out.println("删除用户!");
continue;
case 4:
System.out.println("修改用户!");
continue;
case 5:
System.out.println("查询单个用户!");
continue;
case 6:
System.out.println("退出系统!");
break;
default:
System.out.println("你输入的内容不正确!");
continue;
}
break;
}
}
}
do-while循环
语法 :跟while循环的用法类似,相比来说,需要先执行一次代码块,然后再去做循环判断,最少会执行一次代码块内容
do{
代码块;
}while(循环条件表达式);
for循环
//do-while循环的用法
public class Demo02 {
public static void main(String[] args) {
//使用do-while输出1-10
int i = 1;
do{
System.out.println(i);
i++;
}while (i <= 10);
//使用do-while完成1-100之间的偶数的求和
int sum = 0;
int a =0;
do {
sum += a;
a += 2;
}while (a <= 100);
System.out.println(sum);
}
}
语法 :
for(初始化语句a;判断条件语句b;控制条件语句c){
代码块;
}
For循环
执行流程 :
1,先执行初始化语句a
2,执行判断条件b,
如果b的结果为true,执行代码块内容
如果b的结果为false,循环结束
3,执行控制条件c
4,再执行b,依次循环
public class Demo03 {
public static void main(String[] args) {
//声明for循环 ,从1输出到10
for (int i = 1;i <= 10; i++){
System.out.println(i);
}
//使用for循环来求1-100的和
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum += i;
}
System.out.println(sum);
//求1-100的偶数和
int sum1 = 0;
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0){
sum1 += i;
}
}
System.out.println("1-100的偶数和为:" + sum1);
//输出所有的水仙花数
//水仙花数是一个三位数,特点是 :
//个位的三次方+十位的三次方+百位的三次方 的和 等于这个数字本身
for (int i = 100; i < 1000; i++) {
//个位的三次方+十位的三次方+百位的三次方的和
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (i == ge*ge*ge + shi*shi*shi + bai*bai*bai){
System.out.println("水仙花数:" + i);
}
}
}
}
for循环的嵌套使用
public class Demo04 {
public static void main(String[] args) {
/*
******
******
******
******
*/
//System.out.print("*");
//System.out.print("*");
//System.out.print("*");
//System.out.print("*");
//System.out.print("*");
//System.out.println("*");
//for (int i = 0; i < 6; i++) {
// System.out.print("*");
//}
//System.out.println();
//for (int i = 0; i < 6; i++) {
// System.out.print("*");
//}
//System.out.println();
//for (int i = 0; i < 6; i++) {
// System.out.print("*");
//}
for (int i = 0; i < 4; i++) { //外层循环,控制行
for (int j = 0; j < 6; j++) { //内层的循环,控制列
System.out.print("*");
}
System.out.println();
}
//使用for循环嵌套,完成一个乘法口诀表的编写
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i ; j++) {
System.out.print(j + "*" + i + "=" + (i*j)+ " ");
}
System.out.println();
}
}
}
break、continue、return的区别
1,break 只用在switch和循环中
2,continue一般用在循环中,表示跳出当次循环,继续下次循环
3,return用在方法中的,一般用来结束方法
continue
return的用法:用来结束方法的,不是用来结束循环的
/*
break在多重循环中的使用
*/
public class BreakDemo {
public static void main(String[] args) {
wc:for (int i = 0; i < 5; i++) {
nc:for (int j = 0; j < 4; j++) {
if (j == 2){
// break; //默认状态下,跳出内部循环
// break wc;
break nc;
}
System.out.print("*");
}
System.out.println();
}
}
}
/*
continue的用法
*/
public class ContinueDemo {
public static void main(String[] args) {
//有10个学生,循环录入学生的成绩,录完之后,统计
//80分以上的学生的占比情况
//1,创建需要的变量,输入对象
double count = 0.0;
Scanner scanner = new Scanner(System.in);
//2,写一个循环,循环10次,每次循环录入一个学生成绩
for (int i = 1; i <= 10; i++) {
//3,录入成绩后,可以判断这个学生的成绩是否超过80
System.out.println("请输入" + i +"号学生的成绩:");
int score = scanner.nextInt();
//4,如果超过80,记录到一个变量,不超过 continue
if (score < 80){
continue;
}
count++;
}
double total = count / 10;
//5,计算占比
System.out.println("超过80分的学生占比为:" + total * 100 +"%");
}
}
public class ReturnDemo {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
if (i == 5){
System.out.println("退出");
//break;
//continue;
return;
}
System.out.println(i);
}
System.out.println("运行结束");
}
// 1 2 3 4 退出 运行结束
// 1 2 3 4 退出 6 7 8 9 运行结束
//1 2 3 4 退出
}
增强for循环
语法: 一般用来遍历数组或者集合中的内容
for(数据类型 变量 : 数据集合){
}
引用数据类型:String,数组,类
创建方式 :
数据类型 变量名 = new 数据类型();
比如 :Scanner sc = new Scanner(System.in);
为什么叫引用类型?
因为,引用类型在创建的时候,都是创建在堆空间中,然后将堆空间中的区域的地址赋值给栈中的变量
这种地址赋值方式,就是一种引用的方式,所以,称为引用类型
String类型
String类型,是字符串类型,在Java中,用双引号引起来的内容就是字符串,它是一个比较特殊的引用类型
声明的时候,可以像基本类型一样,使用字面量的方式赋值,也可以通过new关键字来声明
public class StringDemo {
public static void main(String[] args) {
//String类型的声明方式
//方式1: 类似于基本类型的赋值方式
//字符串以字面量的方式赋值,值直接从常量池中获取
//从常量池中拿到的内容就是相同的
String s1 = "hello";
//方式2: 和引用类型赋值方式一样
//s2通过new的方式创建,但是输出的内容仍然是字符串
//而不是一个地址值
String s2 = new String("hello");
System.out.println(s1);
System.out.println(s2);
System.out.println(s1 == s2); //false
String s3 = "hello";
System.out.println(s1 == s3);//true
}
}
Java内存划分初识:
Java程序执行的时候,会将内存分为栈空间、堆空间
栈空间特点 :
1,自动分配,不需要程序员申请
2,栈空间存取数据的效率较高
3,栈空间的数据 按照 先进后出的方式管理
4,栈空间存储空间较小,不能存放太多的数据
5,JVM将基本类型的数据存放在栈空间
堆空间特点 :
1,需要程序员申请,通过new关键字申请
2,堆中存取效率相对较低
3,堆空间中存放数据是随机分配位置,所以在申请了空间后,会得到一个地址值,指向变量
4,堆空间存放数据的空间比较到,能存放大量的数据
5,引用类型的数据一般都存放在堆空间中
方法
Java是面向对象的语言,对象都会有一些方法,方法其实就是这些类可以做的事情,也称为函数
方法中,一般都会写一些执行了某些功能的代码
将来可以调用这个方法,从而去完成某些想要的功能
方法一般单独声明于类中
语法:
修饰符 返回值类型 方法名 (参数列表){
代码块;
return 返回值;
}
修饰符 : 暂时的写法 public static
返回值类型 :可以是void ,表示没有返回值 ,也可以是数据类型(基本类型和引用类型)
方法名 : 自己取的名字,遵循标识符命名规范
参数列表 :将来调用这个方法时候,是否需要传入值进行运算,如果不需要,可以不写
参数的定义方式 : 数据类型 变量名
代码块 : 将来方法中执行的功能
return :跟返回值类型挂钩
如果有返回值类型,那么就需要return 对应类型的值,
如果没有返回值类型,return可以省略
public class MethodDemo {
//写一个方法,调用之后,可以传入2个整数,并完成两个整数的计算求和后返回
public static int add(int a,int b){
int sum = a + b; //拿到传入的参数,并计算和
return sum; //把求和的结果返回
}
public static void main(String[] args) {
//调用方法,
//也可以将整个方法的表达式参与运算
System.out.println(add(10, 20));
// 可以声明一个变量,接收返回的值
int result = add(10,20);
System.out.println(result);
}
}
课后作业
1,计算星座
白羊:0321~0420 天秤:0924~1023
金牛:0421~0521 天蝎:1024~1122
双子:0522~0621 射手:1123~1221
巨蟹:0622~0722 摩羯:1222~0120
狮子:0723~0823 水瓶:0121~0219
处女:0824~0923 双鱼:0220~0320
根据上述描述,在程序中需要用户输入一个 4 位数字
再根据这个数字所处的范围进行判断,其中前两位是月份,后两位是日期
使用 switch 语句判断出生的月份,然后根据日期确定星座名称
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入出生日期,按照0128格式,即前月份后日期");
int date = scanner.nextInt();
//月份判断
int month = date/100;
int day = date % 100;
switch (month){
case 1:
if (day >= 1 && day <= 20)
System.out.println("摩羯座");
else if (day >= 21 && day <= 31)
System.out.println("水瓶座");
else System.out.println("日期有误!");
break;
case 2:
if (day >= 1 && day <= 19)
System.out.println("水瓶座");
else if (day >= 20 && day <= 29)
System.out.println("双鱼座");
else System.out.println("日期有误!");
break;
case 3:
if (day >= 1 && day <= 20)
System.out.println("双鱼座");
else if (day >= 21 && day <= 31)
System.out.println("白羊座");
else System.out.println("日期有误!");
break;
case 4:
if (day >= 1 && day <= 20)
System.out.println("白羊座");
else if (day >= 21 && day <= 30)
System.out.println("金牛座");
else System.out.println("日期有误!");
break;
case 5:
if (day >= 1 && day <= 21)
System.out.println("金牛座");
else if (day >= 22 && day <= 31)
System.out.println("双子座");
else System.out.println("日期有误!");
break;
case 6:
if (day >= 1 && day <= 21)
System.out.println("双子座");
else if (day >= 22 && day <= 30)
System.out.println("巨蟹座");
else System.out.println("日期有误!");
break;
case 7:
if (day >= 1 && day <= 22)
System.out.println("巨蟹座");
else if (day >= 23 && day <= 31)
System.out.println("狮子座");
else System.out.println("日期有误!");
break;
case 8:
if (day >= 1 && day <= 23)
System.out.println("狮子座");
else if (day >= 24 && day <= 31)
System.out.println("处女座");
else System.out.println("日期有误!");
break;
case 9:
if (day >= 1 && day <= 23)
System.out.println("处女座");
else if (day >= 24 && day <= 30)
System.out.println("天秤座");
else System.out.println("日期有误!");
break;
case 10:
if (day >= 1 && day <= 23)
System.out.println("天秤座");
else if (day >= 24 && day <= 31)
System.out.println("天蝎座");
else System.out.println("日期有误!");
break;
case 11:
if (day >= 1 && day <= 22)
System.out.println("天蝎座");
else if (day >= 23 && day <= 30)
System.out.println("射手座");
else System.out.println("日期有误!");
break;
case 12:
if (day >= 1 && day <= 21)
System.out.println("射手座");
else if (day >= 22 && day <= 31)
System.out.println("摩羯座");
else System.out.println("日期有误!");
break;
default:
System.out.println("月份输入有误!");
break;
}
}
使用嵌套 if,判断出生的月份,然后根据日期确定星座名称
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入出生日期,按照0128格式,即前月份后日期");
int date = scanner.nextInt();
//月份判断
int month = date/100;
int day = date % 100;
if(month == 1) {
if (day >= 1 && day <= 20)
System.out.println("摩羯座");
else if (day >= 21 && day <= 31)
System.out.println("水瓶座");
else System.out.println("日期有误!");
}
else if (month == 2) {
if (day >= 1 && day <= 19)
System.out.println("水瓶座");
else if (day >= 20 && day <= 29)
System.out.println("双鱼座");
else System.out.println("日期有误!");
}
else if (month == 3) {
if (day >= 1 && day <= 20)
System.out.println("双鱼座");
else if (day >= 21 && day <= 31)
System.out.println("白羊座");
else System.out.println("日期有误!");
}
else if (month == 4) {
if (day >= 1 && day <= 20)
System.out.println("白羊座");
else if (day >= 21 && day <= 30)
System.out.println("金牛座");
else System.out.println("日期有误!");
}
else if (month == 5) {
if (day >= 1 && day <= 21)
System.out.println("金牛座");
else if (day >= 22 && day <= 31)
System.out.println("双子座");
else System.out.println("日期有误!");
}
else if (month == 6) {
if (day >= 1 && day <= 21)
System.out.println("双子座");
else if (day >= 22 && day <= 30)
System.out.println("巨蟹座");
else System.out.println("日期有误!");
}
else if (month == 7) {
if (day >= 1 && day <= 22)
System.out.println("巨蟹座");
else if (day >= 23 && day <= 31)
System.out.println("狮子座");
else System.out.println("日期有误!");
}
else if (month == 8) {
if (day >= 1 && day <= 23)
System.out.println("狮子座");
else if (day >= 24 && day <= 31)
System.out.println("处女座");
else System.out.println("日期有误!");
}
else if (month == 9) {
if (day >= 1 && day <= 23)
System.out.println("处女座");
else if (day >= 24 && day <= 30)
System.out.println("天秤座");
else System.out.println("日期有误!");
}
else if (month == 10) {
if (day >= 1 && day <= 23)
System.out.println("天秤座");
else if (day >= 24 && day <= 31)
System.out.println("天蝎座");
else System.out.println("日期有误!");
}
else if (month == 11) {
if (day >= 1 && day <= 22)
System.out.println("天蝎座");
else if (day >= 23 && day <= 30)
System.out.println("射手座");
else System.out.println("日期有误!");
}
else if (month == 12) {
if (day >= 1 && day <= 21)
System.out.println("射手座");
else if (day >= 22 && day <= 31)
System.out.println("摩羯座");
else System.out.println("日期有误!");
}
else {
System.out.println("月份输入有误!");
}
}
2,使用for循环,完成以下图形的打印
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int j = 1; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i + 1; j++) {
System.out.print(" ");
}
for (int j = 4; j >= i; j--) {
System.out.print("*");
}
for (int j = 4; j - 1 >= i; j--) {
System.out.print("*");
}
System.out.println();
}
}
3,有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
for (int k = 1; k <= 4; k++) {
if (i != j && i != k && j != k) {
int number = i * 100 + i * 10 + k;
System.out.println(number);
count++;
}
}
}
}
System.out.println("由1,2,3,4组成的互不相同且无重复数字的三位数的数量为:" + count);
}