1.表达式 and 语句
常量、变量代表数据。由变量或常量 + 运算符构成的计算表达式
1.1 表达式一共分为三种:
(1)变量或常量 + 运算符构成的计算表达式
(2)new 表达式,结果是一个数组或类的对象。(后面讲)
(3)方法调用表达式,结果是方法返回值或void(无返回值)。(后面讲)
程序的功能是由语句来完成的,语句分为单语句和复合语句。
1.2 单语句又分为:
(1)空语句,什么功能都没有。它就是单独的一个分号;(==需要避免==)
(2)表达式语句,就是表达式后面加分号;
//空语句
;
//表达式语句
i++; //自增表达式 + ;
System.out.println("HelloWorld"); //方法调用表达式 + ;
1.3 复合语句分为:
(1)分支语句:if...else,switch...case
(2)循环语句:for,while,do...while
(3)跳转语句:break,continue,return,throw
(4)try语句:try...catch...finally(后面学习)
(5)同步语句:synchronized(后面学习)
2.顺序结构
顺序结构就是程序从上到下行地执行。表达式语句都是顺序执行的。并且上一行对某个变量的修改对下一行会产生影响。
public static void main(String[] args) {
int a = 2;
int b = 3;
System.out.println(a);
System.out.println(b);
// 对 a 的值进行修改
a++;
System.out.println(a);
a++;
System.out.println(a);
}
3.输入输出语句
3.1 两种常见的输入输出语句(基础阶段常用)
- 换行输出
System.out.println(输出内容); // 输出内容之后,紧接着换行
- 不换行输出
System.out.print(输出内容); // 输出内容之后不换行
- + 拼接输出
public static void main(String[] args) {
// 可以为空,换行
System.out.println("你好呀!");
System.out.println("终于要放假了!");
System.out.println("");
System.out.println();
System.out.println("太高兴了!");
// 不可以为空,不换行
System.out.print("你好呀!");
System.out.print("终于要放假了!");
System.out.print("");
// System.out.print();
System.out.print("太高兴了!");
}
3.2 格式化输出
占位符: %d : 整数, %f : 浮点数, %c : 单个字符, %b : boolean值, %s : 字符串
public static void main(String[] args){
System.out.println("+++++++输出多个语句+++++++");
int age = 20;
float height = 165;
String name = "张美丽";
// 双引号内容,原样输出
System.out.println("年龄:" + age + ",身高:" + height + ",爱称:" + name);
System.out.println("+++++++++++++printf 格式化输出++++++++++++");
// 占位符: %d:整数, %f:浮点数, %c:单个字符, %b:boolean值, %s:字符串
System.out.printf("年龄:%d, 身高%.2f,爱称:%s", age, height, name);
char grade = 'A';
boolean marry = false;
System.out.printf("年龄:%d,身高:%.2f,爱称:%s,成绩:%c,婚姻状态:%b", age, height, name, grade, marry);
}
3.3 输入语句
键盘输入代码的四个步骤:
1、申请资源,创建Scanner类型的对象
2、提示输入xx
3、接收输入内容
4、全部输入完成之后,释放资源,归还资源
import java.util.Scanner;
/* 扫描对象输入 Scanner:
nextInt() 整形
nextFloat() 浮点型
next() 字符串
nextLong() 长整型
nextBoolean() 布尔型
next().charAt(1) 字符型
*/
public class ScannerCase2 {
public static void main(String[] args) {
// 1.整形输入
Scanner scan = new Scanner(System.in);
System.out.print("请输入你的年龄:");
int age = scan.nextInt();
System.out.println("age:" + age);
// 2.浮点型
System.out.print("请输入你的身高:");
float height = scan.nextFloat();
System.out.println("身高:" + height);
// 3.字符串
System.out.print("请输入你的名言:");
String solgan = scan.next();
System.out.println("名言:" + solgan);
// 4.long 型
System.out.print("请输入你一年的工资:");
long salary = scan.nextLong();
System.out.println("工资:" + salary);
// 5.接受输入的字符
System.out.println("请输入一个字符:");
char study = scan.next().charAt(1);
System.out.println("幸运字符:" + study);
// 6.求球的体积
Scanner scan = new Scanner(System.in);
System.out.print("请输入球的半径:");
double r = scan.nextDouble();
double v = (double) (4 / 3) * Math.PI * Math.pow(r, 3);
// Math:是 java 核心基础语法提供的工具方法
System.out.println("球的半径:" + r + "球的体积:" + (float) v);
System.out.printf("球的半径:%.2f,球的体积:%.2f", r, v);
// 4.结束输入 对象名.close()
scan.close();
}
}
4.分支语句
4.1 单分支语句
如果表达式为真,执行 if 中的执行语句;如果为假,则不执行
-
if语句第一种格式: if
if(条件表达式){
语句体;
}
4.2 双分支条件判断:if ... else
如果表达式条件为真,执行 if 中的执行语句;如果为假,则执行 else 中的执行语句
// if 后允许不带 {} ,如果超过两句执行语句,必须带上 {}
if (age >= 18)
System.out.println("恭喜你成年了,能去网吧了!");
else
System.out.println("快快长大吧!");
4.3 多分支条件判断 if ... else if
- if语句第三种格式: if...else if ...else
if (判断条件1) {
执行语句1;
} else if (判断条件2) {
执行语句2;
}
...
}else if (判断条件n) {
执行语句n;
} else {
执行语句n+1;
}
案例代码:
public static void main(String[] args) {
// 100 满分; 90-99 优秀; 80-89 良好; 60-79 及格; >60 不及格
Scanner scan = new Scanner(System.in);
System.out.print("请输入你的成绩:");
int score = scan.nextInt();
if (score == 100) {
System.out.println("恭喜您获得满分!");
} else if (score >= 90 && score < 100) {
System.out.println("优秀");
} else if (score >= 80 && score < 90) {
System.out.println("良好");
} else if (score >= 60 && score < 80) {
System.out.println("及格");
} else if (score < 60&&score>=0) {
System.out.println("继续努力,不及格");
} else {
System.out.println("这TM纯粹是来捣乱的!");
}
}
4.4 if ... else 嵌套
在if的语句块中,或者是在else语句块中, 又包含了另外一个条件判断(可以是单分支、双分支、多分支)
执行的特点:
(1)如果是嵌套在if语句块中的 只有当外部的if条件满足,才会去判断内部的条件
(2)如果是嵌套在else语句块中的 只有当外部的if条件不满足,进入else后,才会去判断内部的条件
public static void main(String[] args){
System.out.print("请输入年份:");
int year = scan.nextInt();
System.out.print("请输入月份:");
int mon = scan.nextInt();
scan.close();
if (year > 0) {
if (mon > 0 && mon < 13) {
if (mon == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("2月有29天");
} else {
System.out.println("2月有28天");
}
} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
System.out.println(mon + "月有30天");
} else {
System.out.println(mon + "月有31天");
}
} else {
System.out.println("请输入正确的月份!");
}
} else {
System.out.println("请您输入正确的年份!");
}
}
4.5 switch ... case 多分支选择结构
语法格式:
switch(表达式){
case 常量值1:
语句块1;
break;
case 常量值2:
语句块2;
break;
...
default:
语句块n+1;
break;
}
4.5.1 如何避免 case 穿透
案例:从键盘输入星期的整数值,输出星期的英文单词
public static void main(String[] args) {
// 格式化星期几
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个数字:");
int week = scan.nextInt();
switch (week) {// char 类型可以 String 类型也可
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("你在搞什么,小傻瓜!");
}
}
4.5.2 利用 case 的穿透性
在switch语句中,如果case的后面不写break,将出现穿透现象,也就是一旦匹配成功,不会在判断下一个case的值,直接向后运行,直到遇到break或者整个switch语句结束,switch语句执行终止。
练习:根据指定的月份输出对应季节
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个数字:");
int season = scan.nextInt();
scan.close();
// 季节匹配
switch (season) {
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;
case 12:
case 1:
case 2:
System.out.println("冬季");
break;
default:
System.out.println("你在搞什么,小傻瓜!");
}
}
4.6 if 语句与 switch 语句比较
-
if 语句的条件是一个布尔类型值,if 条件表达式为 true 则进入分支,可以用于范围的判断,也可以用于等值的判断,使用范围更广。
-
switch 语句的条件是一个常量值(byte , short , int , char ,枚举 , String),只能判断某个变量或表达式的结果是否等于某个常量值,使用场景较狭窄。
-
当条件是判断某个变量或表达式是否等于某个固定的常量值时,使用 if 和 switch 都可以,习惯上使用 switch 更多。当条件是区间范围的判断时,只能使用if语句。
-
另外,使用 switch 可以利用穿透性,同时执行多个分支,而 if ... else 没有穿透性。
5.循环语句
5.1 for 循环
for循环语句格式:
// int i = 1; 表达式1,初始化变量,执行 1 次
// i<=10; 表达式2,执行 11 次,条件表达式为真,执行循环体语句,当循环体语句执行完毕时,再执行表达式3
// i++; 表达式3,执行 10 次,执行 ++ or -- 操作,继续执行表达式2,再次判断是否执行循环语句,判断条件为假,就结束整个循环
for (int i = 0; i <=10 ; i++) {
循环体语句
}
注意:
(1)for( ; ; )中的两个;是不能多也不能少
(2)循环条件必须是boolean类型
案例:输出 1~5 数字
// 输出 1~5 数字
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
死循环:
while (循环条件语句) {
循环体语句;
}
for(;;){
循环体语句块;//如果循环体中没有跳出循环体的语句,那么就是死循环
}
5.2 关键字: break continue
5.2.1 break
使用场景:终止switch或者当前循环
-
在选择结构switch语句中
-
在循环语句中
-
离开使用场景的存在是没有意义的
5.2.2 continue
使用场景:提前结束本次循环,继续下一次的循环
5.3 while 循环
while循环语句基本格式:
while (循环条件语句) {
循环体语句;
}
注意:
while(循环条件)中循环条件必须是boolean类型
public static void main(String[] args) {
// for (int i = 0; i < 10; i++) {}
// while (条件表达式){}
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
死循环:
while(true){
循环体语句;//如果此时循环体中没有跳出循环的语句,就是死循环
}
注意:
(1)while(true):常量true表示循环条件永远成立
(2)while(循环条件),如果循环条件中的循环变量值不修改,那么循环条件就会永远成立
(3)while()中的循环条件不能空着
5.4 do ... while 循环
do...while循环语句标准格式:
注意:
(1)while(循环条件)中循环条件必须是boolean类型
(2)do{}while();最后有一个分号
(3)do...while结构的循环体语句是至少会执行一次,这个和for和while是不一样的
do {
循环体语句①;
} while (循环条件语句);
// 示例代码
public static void main(String[] args) {
/*do {
循环体语句
}while(条件表达式);*/
int i = 1;
do {
System.out.println(i);
i++;
} while (i < 6);
}
死循环:
do{
循环体语句;//如果此时循环体中没有跳出循环的语句,就是死循环
}while(true);
注意:
(1)while(true):常量true表示循环条件永远成立
(2)while(循环条件),如果循环条件中的循环变量值不修改,那么循环条件就会永远成立
(3)while()中的循环条件不能空着
5.5 循环语句的区别
-
从循环次数角度分析
-
do...while循环至少执行一次循环体语句
-
for和while循环先循环条件语句是否成立,然后决定是否执行循环体,至少执行零次循环体语句
-
-
如何选择
-
遍历有明显的循环次数(范围)的需求,选择for循环
-
遍历没有明显的循环次数(范围)的需求,循环while循环
-
如果循环体语句块至少执行一次,可以考虑使用do...while循环
-
本质上:三种循环之间完全可以互相转换,都能实现循环的功能
-
-
三种循环结构都具有四要素:
-
(1)循环变量的初始化表达式
-
(2)循环条件
-
(3)循环变量的修改的迭代表达式
-
(4)循环体语句块
-
5.6 循环嵌套
所谓嵌套循环,是指一个循环的循环体是另一个循环。比如for循环里面还有一个for循环,就是嵌套循环。当然可以是三种循环任意互相嵌套。
例如:两个for嵌套循环格式
for(初始化语句①; 循环条件语句②; 迭代语句⑦) {
for(初始化语句③; 循环条件语句④; 迭代语句⑥) {
循环体语句⑤;
}
}
执行特点:外循环执行一次,内循环执行一轮。
6.案例
6.1 猜数字
// 0~10 之间的数字,提示猜大,猜小
int ran = (int) (Math.random() * 11);
Scanner scan = new Scanner(System.in);
int count = 0;
while (count < 3) {
System.out.print("请输入你要猜的数字(0-10):");
System.out.println("随机数为:" + ran);
int guess = scan.nextInt();
count++;
if (guess > 10 || guess < 0) {
System.out.println("你在搞什么!输入超出范围!!!");
} else if (guess > ran) {
System.out.println("猜大了!请重新输入!!!");
} else if (guess < ran) {
System.out.println("猜小了!请重新输入!!!");
} else if (guess == ran) {
System.out.println("恭喜你,猜对了!!!");
break;
} else {
System.out.println("你搞什么!重新输入!!!");
}
}
scan.close();
6.2 直角三角形
for (int i = 0; i < 5; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
6.3 金字塔
Scanner scan = new Scanner(System.in);
System.out.print("请输入行数:");
int num = scan.nextInt();
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num - i; j++) {
System.out.print(" " + " ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*" + " ");
}
System.out.println();
}
后续内容,正在更新...