引用类型 Scanner
1. 导包
2. 使用 new 关键字创建 Scanner 对象
3. 调用对象的方法
Scanner scan = new Scanner(System.in);
String result = scan.next();
System.out.println("result = " + result);
Math类
// 求绝对值 abs();
int result = Math.abs(-5);
System.out.println("result = " + result);
// 平方根 Math.sqrt();
System.out.println(Math.sqrt(2));
// 四舍五入函数
System.out.println(Math.round(1.5));
// 向下取整
System.out.println(Math.floor(1.9));
// 向上取整
System.out.println(Math.ceil(1.1));
if循环
// 单分支
/*
伪代码:
if (条件表达式) {
代码体;
}
if (如果小明考试100分) {
带去公园玩耍;
}
*/
int point = 99;// 基本类型变量
/*
如果 if (条件表达式 结果是 true 则执行) {
执行这里的代码
}
*/
if (point == 100) {
System.out.println("带去公园玩耍!");
// 使用 new 关键字创建引用类型
Scanner scan = new Scanner(System.in);
System.out.print("请输入小明考试分数:");
int score = scan.nextInt();
// 如果小明考试 100 分 去公园玩耍
if (score == 100) {
System.out.println("对小明的奖励:去公园玩耍。");
} else {
System.out.println("对小明的惩罚:在家写作业。");
}
/*
多分枝语法格式:
方式一:
if (条件表达式) {
语句体;
} else if (条件表达式) {
语句体;
} else if (条件表达式) {
语句体;
}
方式二:
if (条件表达式) {
语句体;
} else if (条件表达式) {
语句体;
} else if (条件表达式) {
语句体;
} else {
语句体;
}
*/
/*
案例:根据班级同学的考试分数,做一个评定
int score;// 分数
90+ 优秀 score >= 90
80 ~ 89 良好 score >= 80 && score <= 89
70 ~ 79 中等 score >= 70 && score <= 79
60 ~ 69 普通 score >= 60 && score <= 69
还需要继续努力
*/
int score;// 声明一个分数的变量
Scanner scan = new Scanner(System.in);
System.out.println("请输入考试成绩,返回评定结果:");
score = scan.nextInt();
// 根据输入的成绩判断返回哪个评定结果
if (score >= 90) {
System.out.println("该学生的成绩评定为:优秀");
} else if (score >= 80 && score <= 89) {
System.out.println("该学生的成绩评定为:良好");
} else if (score >= 70 && score <= 79) {
System.out.println("该学生的成绩评定为:中等");
} else if (score >= 60 && score <= 69) {
System.out.println("该学生的成绩评定为:普通");
} else {
System.out.println("还需要继续努力。");
}
Switch
/*
int score = 88;
语法格式:
switch (表达式) {
case 值1:
语句体;
case 值2:
语句体;
case 值3:
语句体;
default:
语句体;
}
*/
/*
100 / 10 = 10
90 ~ 99 / 10 = 9
80 ~ 89 / 10 = 8
70 ~ 79 / 10 = 7
60 ~ 69 / 10 = 6
*/
int score;// 声明一个分数的变量
Scanner scan = new Scanner(System.in);
System.out.println("请输入考试成绩,返回评定结果:");
score = scan.nextInt();
// case 穿透?break 防止 case 穿透
// 重复的业务逻辑
switch (score / 10) {
case 10:
case 9:
System.out.println("该学生的成绩评定为:优秀");
break;
case 8:
System.out.println("该学生的成绩评定为:良好");
break;
case 7:
System.out.println("该学生的成绩评定为:中等");
break;
case 6:
System.out.println("该学生的成绩评定为:普通");
break;
default:
System.out.println("还需要继续努力。");
}
嵌套循环
// 需求:输出5行5列的矩形 '*'
// 1. 循环输出5行*
for (int i = 0; i < 5; i++) {
System.out.println("*");
}
System.out.println("-------------分割线-----------");
// 2. 循环输出5个*
for (int j = 0; j < 5; j++) {
System.out.print("*");
}
System.out.println("\n-------------分割线-----------");
// 3. 将两个循环合二为一
for (int i = 0; i < 5; i++) {// 操作行数
for (int j = 0; j < 5; j++) {// 操作个数
System.out.print("*");
}
System.out.println();// 换行
}
System.out.println("-----------分割线------------");
// 需求:5行5列的空心方形
/*
*****
* *
* *
* *
*****
*/
/*
分析: i = 0 代表第一行 i = 4 代表最后一行
j = 0 代表第一个 j = 4 代表最后一个
*/
for (int i = 0; i < 5; i++) {// 操作行数
for (int j = 0; j < 5; j++) {// 操作个数
// 判断是否是第一行或最后一行 ||
if (i == 0 || i == 4) {
System.out.print("*");
} else {
// 判断是否是第一个或最后一个
if (j == 0 || j == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
System.out.println();// 换行
}
while
/*
先判断后执行。
语法格式:
while (条件表达式) {
语句体;
}
解释:如果条件表达式满足,则执行语句体,然后继续判断条件表达式是否满足,满足继续执行,如此反复,
一直到条件表达式返回 false, 循环结束。
*/
// 需求:打印数字 1 ~ 5
// ----------------整体 开始----------------
int index = 6;// 起始变量
while (index <= 5) {
System.out.println("index = " + index);// 打印变量
index++;// 变量自增
}
// -----------------整体 结束--------------
System.out.println("我是后续代码。");
}
do — while
/*
先执行后判断,至少会执行一次。
do {
语句体;
} while (条件表达式);
解释:先执行 do 内部的语句体,然后判断条件表达式是否成立,为true继续执行do内部的语句体,如此反复。
*/
// 需求:打印数字 1 ~ 5
int index = 6;// 起始变量
do {
System.out.println("index = " + index);
index++;
} while (index <= 5);