(一)顺序逻辑
单线程下一定按照代码顺序执行
(二)分支逻辑
1. if语句
(1) if(表达式 == true){
}else{
}
(2).多分支(多个 if...else)
输入一个年份,判断是否为闰年(世纪闰年)
public class Test {
public static void main(String[] args) {
//输入一个年份,判断是否是一个闰年(世纪闰年)
//分支嵌套
int year = 2000;
//能否被100整除
if( year % 100 == 0){
//此时year是100的倍数 2000 3000
if(year % 400 == 0){
System.out.println(year + "是一个世纪闰年");
}else{
System.out.println(year + "不是一个世纪闰年");
}
}else{
//此时year不是100的倍数,2022
//判断此时year能否被4整除
if(year % 4 == 0){
System.out.println(year + "是一个普通闰年");
}else{
System.out.println(year + "不是一个普通闰年");
}
}
}
}
if...else的判断有就近原则,else和最近的if做匹配(推荐加上大括号)
2. switch(char | int | String | 枚举)
public class Test {
public static void main(String[] args) {
int day = 6;
switch (day){
case 1 :
System.out.println("星期1");
break;
case 2 :
System.out.println("星期2");
break;
case 3 :
System.out.println("星期3");
break;
case 4 :
System.out.println("星期4");
break;
case 5 :
System.out.println("星期5");
break;
default :
System.out.println("周末");
}
}
}
switch在每个case后面一定记得加上break表示终止,若没有break终止,当switch碰到满足条件的分支后,会一直执行到switch最后
(三)循环结构
代码块中的内容可能会被执行多次
1. while循环
while(表达式为真){
}
public class Test {
public static void main(String[] args) {
int a = 1000;
//寻找1000-2000之间所有3的倍数
//不知循环几次,用while
while(a <= 2000){
if(a % 3 == 0){
System.out.println("找到了3的倍数:" +a);
a ++;
continue;
}else{
a ++ ;
}
}
}
}
死循环:循环条件一直满足,循环无法终止
若需要让循环提前终止,使用break(跳出本次循环,循环终止),当有多层循环嵌套时,break就近判断。
若需要结束当次循环,使用continue,表示继续执行下一次循环。
(1)实现5的阶乘
public class Test {
public static void main(String[] args) {
//实现5的阶乘 5! = 1*2*3*4*5
int i = 5;
//存储阶乘的值
int result = 1;
while(i >= 1){
result *= i; // result = result * i
i --;
}
System.out.println(result);
}
}
(2)实现1-100的求和
public class Test {
public static void main(String[] args) {
//实现1-100求和
int num = 1;
//定义一个存储相加结果的值
int result = 0;
while(num <= 100){
result += num; //result = result + num
num ++;
}
System.out.println(result);
}
}
(3)计算1!+2!+3!+4!+5!
public class Test {
public static void main(String[] args) {
//求1!+2!+3!+4!+5!
int num = 5;
int result = 0;
while(num >= 1){
int tmp = 1;
//存储当前num的阶乘值
int tmpResult = 1;
//内层循环,计算阶乘
while(tmp <= num){
tmpResult *= tmp;
tmp ++;
}
result += tmpResult;
num --;
}
System.out.println(result);
}
}
2. for循环
for(循环开始条件;循环终止条件;循环继续声明){
循环体
}
(1)for循环计算1!+2!+3!+4!+5!
public class Test {
public static void main(String[] args) {
//for循环实现1!+2!+3!+4!+5!
//存储最终的结果
int result = 0;
//表示当前进行哪个数的阶乘
for(int i =1; i <= 5; i++){
//存储当前阶乘的值
int tmpResult = 1;
//实际进行计算阶乘的操作
for (int j = 1; j <= i ; j++) {
tmpResult *= j;
}
result += tmpResult;
}
System.out.println(result);
}
}
(2)for循环和while循环的最大区别:
while循环常用于不知道循环次数的情况
for循环用于已知循环次数的情况
(3)当while和for循环不加{},循环体代码只有一行。因此以后有循环体一定要加{}
3. do...while循环
do...while,无论条件是否满足,do{}先执行一次
(四)系统输入输出
1.系统输出:输出到显示器
System.out.println(); // 一组重载函数,自 带换行,支持所有数据类型, System.out.print(); // 同上 , 只是 不带换行 System.out.printf("占位符",变量) // 同c printf printf("%d",a) 2.输入 使用JDK提供的Scanner类 数据类型 变量名称 = scanner.next数据类型(); String str = scanner.next(); 生成随机数//生成0-100之间的随机数 Random random = new Random(); int randomNum = random.nextInt(100); System.out.println(randomNum);



