程序和人生是一样:顺序中夹杂着循环,伴随一次次选择不断成长
- 程序逻辑控制分为:顺序结构、分支结构、循环结构
1. 顺序结构
- 一行行代码进行书写
public static void main(String[] args) {
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("d");
System.out.println("e");
}
如果调整代码的书写顺序, 则执行顺序也发生变化
2. 分支结构
2.1 if语句
2.1.1 语法格式
if(布尔表达式1){
// 语句1
}else if(布尔表达式2){
// 语句2
}else{
// 语句3
}
- 表达式1成立,执行语句1,否则表达式2成立,执行语句2,否则执行语句3
例如
考生分数在 [90, 100] 之间的,为优秀
分数在 [80, 90) 之前的,为良好分数在 [70, 80) 之间的,为中等
分数在 [60, 70) 之间的,为及格
分数在 [ 0, 60) 之间的,为不及格
错误数据
public static void main(String[] args) {
int score = 96;
if (score>=90){
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("错误数据");
}
}
2.1.2 练习
-
判断一个数字是奇数还是偶数
-
判断一个数字是正数,负数,还是零
-
判断一个年份是否为闰年
public static void main(String[] args) {
//判断是奇数还是偶数
int num = 10;
if (num%2==0){
System.out.println("偶数");
}else{
System.out.println("奇数");
}
}
public static void main(String[] args) {
//判断一个数字是正数,负数,还是零
int num = 10;
if (num>0){
System.out.println("正数");
}else if (num==0){
System.out.println("零");
}else {
System.out.println("负数");
}
}
public static void main(String[] args) {
//判断是否是闰年
int year = 2020;
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)){
System.out.println("闰年");
}else {
System.out.println("不是闰年");
}
}
2.1.3 注意事项
- 代码风格问题
风格1推荐:左花括号在表达式后,{ 放在 if / else 同一行. 代码更加紧凑
int x = 10;
if (x == 10) {
// 语句1
} else {
// 语句2
}
风格2
int x = 10;
if (x == 10)
{
// 语句1
} else
{
// 语句2
}
- 分号问题
in
t x = 20;
if (x == 10);
{
System.out.println("hehe");
}
// 运行结果
hehe
if (x == 10);
此处多写了一个 分号, 导致分号成为了 if 语句的语句体, 而 { } 中的代码已经成为了和一个 if 无关的代码块.
- 悬垂else问题
int x = 10;
int y = 10;
if (x == 10)
if (y == 10)
System.out.println("aaa");
else
System.out.println("bbb");
//结果:aaa
if / else 语句中可以不加大括号,此时 else 是和最接近的 if 匹配.(最好加上大括号)
2.2 switch语句
2.2.1 语法格式
switch(表达式){
case 常量值1:{
语句1;
[break;]
}
case 常量值2:{
语句2;
[break;]
}
.....
default:{
内容都不满足时执行语句;
[break;]
}
}
执行流程
- 先计算表达式的值
- 和case依次比较,一旦有响应的匹配就执行该项下的语句,直到遇到break时结束
- 当表达式的值没有与所列项匹配时,执行default
2.2.2 注意事项
-
多个case后的常量值不可以重复
-
switch的括号内只能是以下类型的表达式:
基本类型:byte、char、short、int,注意不能是long类型
引用类型:String常量串、枚举类型
-
break 不要遗漏, 否则会失去 “多分支选择” 的效果
-
switch 不能表达复杂的条件,比如
num > 10 && num < 20
-
switch 虽然支持嵌套, 代码写出来很丑,不推荐
例子
public static void main(String[] args) {
int day = 1;
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;
}
3. 循环结构
3.1 while 循环
3.1.1 语法格式
while(循环条件){
循环语句;
}
循环条件为 true, 则执行循环语句; 否则结束循环.
3.1.2 示例
- 打印 1 - 10 的数字
- 计算 1 - 100 的和
- 计算 5 的阶乘
- 计算 1! + 2! + 3! + 4! + 5!
//打印 1 - 10 的数字
public static void main(String[] args) {
int i = 1;
while (i<=10) {
System.out.println(i);
i++;
}
}
//计算 1 - 100 的和
public static void main(String[] args) {
int i = 1;
int sum = 0;
while(i<=10){
sum += i;
i++;
}
System.out.println(sum);
}
//计算 5 的阶乘
public static void main(String[] args) {
int i = 1;
int num = 1;
while(i<=5){
num *= i;
i++;
}
System.out.println(num);
}
//计算 1! + 2! + 3! + 4! + 5!
public static void main(String[] args) {
int i = 1;
int sum = 0;
while(i<=5){
int num = 1;
int j = 1;
while(j<=i){
num *= j;
j++;
}
sum += num;
i++;
}
System.out.println(sum);
}
3.1.3 注意事项
- 和 if 类似, while 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
- 和 if 类似, while 后面的 { 建议和 while 写在同一行.
- 和 if 类似, while 后面不要多写分号, 否则可能导致循环不能正确执行
3.1.4 break
break 的功能是让循环提前结束,执行到 break 就会让循环结束
3.1.5 continue
continue 的功能是跳过这次循环, 立即进入下次循环 ;执行到 continue 语句的时候, 就会立刻进入下次循环(判定循环条件), 从而不会执行到continue 下方的语句
3.2 for 循环
3.2.1 语法格式
for(表达式1;布尔表达式2;表达式3){
表达式4;
}
-
表达式1: 用于初始化循环变量初始值设置,在循环最开始时执行,且只执行一次
-
表达式2: 循环条件,满足则循环继续,否则循环结束
-
表达式3: 循环变量的更新方式
3.2.2 示例
- 打印 1 - 10 的数字
- 计算 1 - 100 的和
- 计算 5 的阶乘
- 计算 1! + 2! + 3! + 4! + 5!
//打印 1 - 10 的数字
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
//计算 1 - 100 的和
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("sum = " + sum);
//计算 5 的阶乘
int result = 1;
for (int i = 1; i <= 5; i++) {
result *= i;
}
System.out.println("result = " + result);
//计算 1! + 2! + 3! + 4! + 5!
int sum = 0;
for (int i = 1; i <= 5; i++) {
int tmp = 1;
for (int j = 1; j <= i; j++) {
tmp *= j;
}
sum += tmp;
}
System.out.println("sum = " + sum);
3.2.3 注意事项
- 和 if 类似, for 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
- 和 if 类似, for 后面的 { 建议和 for 写在同一行.
- 和 if 类似, for 后面不要多写分号, 否则可能导致循环不能正确执行.
- 和while循环一样,结束单趟循环用continue,结束整个循环用break
3.3 do while 循环(不常用)
3.3.1 语法格式
do{
循环语句;
}while(循环条件);
先执行循环语句, 再判定循环条件,循环条件成立则继续执行,否则循环结束
3.3.2 注意事项
-
do while 循环最后的分号不要忘记
-
一般 do while 很少用到, 更推荐使用 for 和 while
4. 输入输出
4.1 输出到控制台
4.1.1 基本语法
System.out.println(msg); // 输出一个字符串, 带换行
System.out.print(msg); // 输出一个字符串, 不带换行
System.out.printf(format, msg); // 格式化输出
println
输出的内容自带\n
,print
不带\n
printf
的格式化输出方式和 C 语言的printf
是基本一致的
4.2 从键盘输入
4.2.1 基本语法
使用 Scanner 读取字符串/整数/浮点数
import java.util.Scanner;//导包
public class TestDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的姓名");
String name = scanner.nextLine();//读取一行
// String name = scanner.next();//遇到空格会结束
System.out.println("请输入你的年龄");
int age = scanner.nextInt();//读取整数
System.out.println("请输入你的工资水平");
float wage = scanner.nextFloat();//读取浮点数
System.out.println("你的信息如下:");
System.out.print("姓名:" + name + "\n" + "年龄:" + age + "\n" + "工资:" + wage + "\n");
scanner.close();//调用关闭方法
}
//输出结果
//请输入你的姓名
//张三
//请输入你的年龄
//19
//请输入你的工资水平
//15000.10
//你的信息如下:
//姓名:张三
//年龄:19
//工资:15000.1
4.2.2 练习
使用 Scanner 循环读取 N 个数字,并求取其平均值
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = 0;
int sum =0;
while(scanner.hasNextInt()){
int tmp = scanner.nextInt();
sum += tmp;
num ++;
}
System.out.println("sum = " + sum);
System.out.println("average = " + sum/num);
}
}
//结果
//3
//5
//7
//13
//^D
//sum = 28
//average = 7
注意事项
- 当循环输入多个数据的时候, 使用
ctrl + z
来结束输入 - Windows 上使用
ctrl + z
, Linux / Mac 上使用ctrl + d
5. 猜数字游戏
游戏规则
系统自动生成一个随机整数(1-100), 然后由用户输入一个猜测的数字. 如果输入的数字比该随机数小, 提示 “低了”, 如果输入的数字比该随机数大, 提示 “高了” , 如果输入的数字和随机数相等, 则提示 “猜对了” .
import java.util.Random;
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int toGuess = random.nextInt(100);//1-100(不包含100)产生随机数
while(true){
System.out.println("请输入你猜的数字:");
int num = scanner.nextInt();
if(num > toGuess){
System.out.println("猜大了");
} else if (num < toGuess) {
System.out.println("猜小了");
}else{
System.out.println("猜对了");
break;
}
}
scanner.close();
}
}
6. 练习
- 根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的年龄:");
while(scanner.hasNextInt()){
int age = scanner.nextInt();
if(age<=18){
System.out.println("少年");
} else if (age>=19 && age <=28) {
System.out.println("青年");
} else if (age>=29 && age <= 55) {
System.out.println("中年");
}else {
System.out.println("老年");
}
}
}
}
- 判定一个数字是否是素数
import java.util.Scanner;
public class TestDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入数字用于判断是否是素数:");
while(scanner.hasNextInt()){
int num = scanner.nextInt();
if (num <= 0){
System.out.println(num + "不是素数");
} else if (num == 1 || num == 2 || num == 3) {
System.out.println(num + "是素数");
}else {
int flag = 0;
for(int i = 2; i <= Math.sqrt(num); i++){
if(num % i == 0){
System.out.println(num + "不是素数");
flag = 1;
break;
}
}
if(flag==0){
System.out.println(num + "是素数");
}
}
}
}
}
- 打印 1 - 100 之间所有的素数
public class TestDemo {
public static void main(String[] args) {
int num = 1;
while(num<=100){
if (num == 1 || num == 2 || num == 3) {
System.out.print(num +" ");
}else {
int flag = 0;
for(int i = 2; i <= Math.sqrt(num); i++){
if(num % i == 0){
flag = 1;
break;
}
}
if(flag==0){
System.out.print(num + " ");
}
}
num++;
}
}
}
- 输出 1000 - 2000 之间所有的闰年
public class TestDemo {
public static void main(String[] args) {
int year = 1000;
int count = 0;
while(year <= 2000){
if((year&400)==0 || ((year&4)==0 && (year%100)!=0 )){
System.out.print(year + " ");
count++;
if(count%10==0){
System.out.println();
}
}
year++;
}
}
}
- 输出乘法口诀表
public static void main(String[] args) {
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= i; j++){
System.out.print(i + "*" + j + "=" + (i*j)+" ");
}
System.out.println();
}
- 求两个正整数的最大公约数
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a = scanner.nextInt();
System.out.println("请输入第二个整数:");
int b = scanner.nextInt();
int c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
System.out.println("最大公约数为"+b);
}
- 求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如:
153=1^3+5^3+3^3
,则153是一个“水仙花数”)
public static void main(String[] args) {
for(int i = 100; i <= 999; i++){
int num1 = i % 10;
int num2 = (i / 10) % 10;
int num3 = (i / 100) % 10;
if(i == (Math.pow(num3, 3) + Math.pow(num2, 3) + Math.pow(num1, 3))){
System.out.println(i+"是水仙花数");
}
}
}
- 写一个函数返回参数二进制中 1 的个数比如: 15 0000 1111 4 个 1
public static void main(String[] args) {
//写一个函数返回参数二进制中 1 的个数比如: 15 0000 1111 4 个 1
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要判断的数:");
int n = scanner.nextInt();
int count = 0;
while(n!=0){
if((n&1)==1){
count++;
}
n = n>>>1;
}
System.out.println("该数字的二进制中1的个数为:" + count);
}
- 获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要判断的数:");
int n = scanner.nextInt();
//0000 0000 0000 0000 0000 0000 0000 0000 1010
System.out.println("奇数位的二进制序列如下:");
for(int i = 30; i >= 0; i -= 2){
int tmp = n;
tmp = tmp >> i;
int t = tmp & 1;
System.out.print(t + "");
}
System.out.println();
System.out.println("偶数位的二进制序列如下:");
for(int i = 31; i >= 1; i -= 2){
int tmp = n;
tmp = tmp >> i;
int t = tmp & 1;
System.out.print(t + "");
}
}
- 完成猜数字游戏
见上!