1. 概述
顺序当中夹杂着循环,一次一次的运行
2. 顺序结构
也就是按照语句的顺序执行
3.分支结构
3.1 if 结构
//三种语法格式
if(布尔表达式){
// 语句
}
if(布尔表达式){
// 语句
}else{
//语句2
}
if(布尔表达式){
// 语句
}else if(布尔表达式){
//语句2
}else{
}
//判断一个数是奇数还是偶数
public class Main {
public static void main(String[] args) {
int m = 12;
if(m%2 == 0){
System.out.println("该数为偶数");
}else {
System.out.println("该数为奇数");
}
}
}
//判断一个数字是正数,负数,还是零
public class Main {
public static void main(String[] args) {
int num = -1;
if(num > 0){
System.out.println(num+"为正数");
} else if ( num < 0) {
System.out.println(num+"为负数");
}else {
System.out.println(num+"为0");
}
}
}
//判断一个年份是否为闰年
public class Main {
public static void main(String[] args) {
int year = 2025;
if((year%4 == 0) && (year%100 != 0)){
System.out.println(year + "为闰年");
} else if (year % 400 == 0) {
System.out.println(year + "为世纪闰年");
}else {
System.out.println(year + "不是闰年");
}
}
}
3.2 switch语句
switch(表达式){
case 常量值1:{
语句1;
break;
}
case 常量值2:{
语句2;
break;
}
default:{
内容都不满足时执行语句;
break;
}
}
/*执行流程:
1. 先计算表达式的值
2. 和case依次比较,一旦有响应的匹配就执行该项下的语句,直到遇到break时结束
3. 当表达式的值没有与所列项匹配时,执行default*/
4.循环结构
4.1 while 循环
while(循环条件){
循环语句;
}
//计算 1! + 2! + 3! + 4! + 5!
public class Main {
public static void main(String[] args) {
int i = 1;
int n = 1;
int result = 1;
int sum = 0;
//阶乘相加
while (i <= 5){
//求一个数的阶乘
while (i <= n){
result *= i;
i++;
}
n++;
sum += result;
}
System.out.println(sum);
}
}
4.2 break
break 的作用就是提前是程序结束
//找到 100 - 200 中第一个 3 的倍数
int num = 100;
while (num <= 200) {
if (num % 3 == 0) {
System.out.println("找到了 3 的倍数, 为:" + num);
break;
}
num++;
}
// 执行结果
找到了 3 的倍数, 为:102
4.3 continue
continue 的作用就是调过此次循环,进入下一次循环
//找到 100 - 200 中第一个 3 的倍数
int num = 100;
while (num <= 200) {
if (num % 3 != 0) {
num++;
continue;
}
System.out.println("找到了 3 的倍数, 为:" + num);
num++;
}
// 执行到 continue 语句的时候, 就会立刻进入下次循环(判定循环条件), 从而不会执行到下方的打印语句
4.4 for 循环
for(表达式①;布尔表达式②;表达式③){
表达式④;
}
//计算 1! + 2! + 3! + 4! + 5!
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5 ; i++) {
int result = 1;
for (int j = 1; j <= i ; j++) {
result *= j;
}
sum += result;
}
System.out.println(sum);
}
}
4.5 do while 循环 (选学)
do{
循环语句;
}while(循环条件);
//先执行循环语句, 再判定循环条件,循环条件成立则继续执行,否则循环结束。
5. 输入输出
5.1 输出
System.out.println(java); // 输出一个字符串, 带换行
System.out.print(java); // 输出一个字符串, 不带换行
System.out.printf(format, java); // 格式化输出
| 转换符 | 类型 | 举例 |
|---|---|---|
| d | 十进制整数 | ( “%d” ,10) |
| x | 十六进制整数 | ("%x", 100) |
| f | 定点浮点数 | ("%f",100.0) |
| s | 字符串 | ("%s",“abc”) |
| c | 字符 | ("%c",‘1’) |
5.2 输入
使用 Scanner 读取字符串 / 整数 / 浮点数
import java.util.Scanner; // 需要导入 util 包
// 输入一个整型
Scanner scanner = new Scanner(System.in);
int a=scanner.nextInt();
// 输入一个字符串
Scanner scanner = new Scanner(System.in);
String a=scanner.nextLine(); //注意 next 为读取到空格,而 nextLine 为读取一行
//输入一个浮点数
Scanner scanner = new Scanner(System.in);
double a = scanner.nextDouble();
//使用 Scanner 循环读取 N 个数字
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()){
int a = scanner.nextInt();
System.out.println(a);
}
}