三、程序逻辑控制

一、顺序结构

比较简单,按照代码的顺序一行一行输出执行。

二、分支结构

1、 if语句
if(布尔表达式) {
	//语句
	}


if(布尔表达式){
	//语句1
	} else {
	//语句2
	}


if(布尔表达式){
	//语句1
	} else if {
	//语句2
	} else  {
	//语句3
	}

注意: 这里的 ifelse 是互斥关系,要么走 if ,要么走 else。

//判断一个年份是否为闰年。
public class IfTest {
    public static void main(String[] args) {
        int year = 1200;
        if (year % 100 ==0) {
            if (year % 400 == 0) {
                System.out.println(year +"是世纪闰年");
            } else {
                System.out.println(year +"不是闰年");
            }
        } else if (year % 4 == 0) {
            System.out.println(year +"是闰年");
        } else {
            System.out.println((year +"不是闰年"));
        }
    }
}

2、switch语句
switch(表达式) {
case常量值1:{
语句1;
break;		//退出当前循环;
}
case常量值2:{
语句2;
break;
}
default:{
case都不满足时的语句;
break}
}

执行流程:
1、 满足case i 的时候,从 i 进入,依次往下执行,遇到break就退出该循环,没有遇到相等的case时,执行最后的default语句。
2、switch(表达式)括号内只能是byte,char,int,short,String常量串,枚举类型。

三、循环结构

1、while循环
while(循环条件) {
循环语句;
}				//循环条件为真,则执行循环语句;否则循环结束。

不知道循环次数且知道循环结束条件时用while。

举例:1. 打印1-10的数字

public class WhileTest {
    public static void main(String[] args) {
        int i = 1;
        while(i <= 10) {
            System.out.println(i);
            i++;
        }
    }
}
  1. 打印1-100的和
public class WhileTest {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i <= 100) {
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }											//5050
}

3、计算5!

public class WhileTest {
    public static void main(String[] args) {
        int i = 1;
        int fac = 1;
        while (i <= 5) {
            fac = fac * i;
            i++;
        }
        System.out.println(fac);
    }
}														// 120

4、计算 1! + 2! + 3! + 4! + 5!

public class WhileTest {
    public static void main(String[] args) {
        int n = 1;
        int sum = 0;
        while (n <= 5) {
            int i = 1;
            int fac = 1;
            while ( i <= n) {
                fac = fac * i;
                i++;
            }
            sum = sum + fac;
            n++;
        }
        System.out.println(sum);
    }
}												// 153

2、break

break的功能是让循环提前结束。

示例:1、找出100-200中第一个3的倍数。

public class BreakTest {
    public static void main(String[] args) {
        int n = 100;
        while (n <= 200) {
            if (n % 3 == 0) {
                System.out.println(n);
                break;
            }
            n++;
        }
    }
}																// 102

3. continue

continue的作用是推出这次循环,提前进入下一个循环。

示例: 1、 找出100-200中所有3的倍数。

public class BreakTest {
    public static void main(String[] args) {
        int n = 100;
        while (n <= 200) {
            if (n % 3 != 0) {
                n++;
                continue;
            }
            System.out.println(n);
            n++;
        }
    }
}

4、for循环

基本语法:

for(表达式1;表达式2;表达式3) {
	表达式4;
}

知道循环次数时用for。

示例: 1、打印1-10的数字

public class ForTest {
    public static void main(String[] args) {
        for (int i = 1; i <11 ; i++) {
            System.out.println(i);
        }
    }
}

2、计算1-100的和。

public class ForTest {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 0; i <= 100 ; i++) {
            sum = sum + i;
        }
        System.out.println("1-100的和="+sum);
    }
}

3、计算5!

public class ForTest {
    public static void main(String[] args) {
        int factor = 1;
        for (int i = 1; i <= 5 ; i++) {
            factor = factor * i;
        }
        System.out.println("5!="+factor);
    }
}

4、计算 1! + 2! + 3! + 4! + 5! + 6!

public class ForTest {
    public static void main(String[] args) {
        int sum = 0;
        int fac = 1;
        for (int i = 1; i <= 5 ; i++) {
            fac = fac * i;
            sum = sum + fac;
        }
        System.out.println(sum);
    }
}

或者
public class ForTest {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 5 ; i++) {
            int fac = 1;
            for (int n = 1; n <= i ; n++) {
                fac = fac * n;
            }
            sum = sum + fac;
        }
        System.out.println(sum);
    }
}

5、do while循环

do {
循环语句;
} while(循环条件);

先执行循环语句,在判断循环条件,循环条件成立则继续执行,否则跳出循环 。

示例 :1、打印1-10的数字;

public class WhileTest {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.println(i);
            i++;
        } while (i <= 10);
    }
}

四、输入输出

1、输出到控制台
System.out.println(n);											//输出n 带换行
System.out.print(n)												//输出n 不带换行
System.out.printf(format, msg); 							// 格式化输出
2、从键盘输入

使用 Scanner 读取字符串/整数/浮点数

import java.util.Scanner;       //需要导入util包

public class ScannerTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入你的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入你的工资:");
        float salary = sc.nextFloat();
        System.out.println("你的信息如下:");
        System.out.println("姓名:"+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary);
        sc.close();			//这里要记得调用关闭方法。
    }
}

从键盘中获取内容,使用Scanner类
(字符串,整数,浮点数)
1、产生一个Scanner对象,Scanner sc = new Scanner(System.in);
2、从键盘中获取字符串内容,使用 nextLine 方法;
3、从键盘中获取整型内容,使用 nextInt 方法;
4、从键盘中获取浮点型内容,使用 nextDouble 方法。

示例: 1、使用 Scanner 循环读取 N 个数字,并求取其平均值

import java.util.Scanner;

public class ScannerTest {
    public static void main(String[] args) {
        int sum = 0;
        int count = 0;
        double ret = 0.0;
        Scanner scanner = new Scanner(System.in);
        //hasNextInt() 是判断是否还有整数输入,如果输入的还是整数,则循环继续。
        while (scanner.hasNextInt()) {
            int tmp = scanner.nextInt();
            sum = sum + tmp;
            count++;
        }
        ret = sum / count * 1.0;
        System.out.println("sum = " + sum);
        System.out.println("平均数=" + ret);
    }
}
3、随机数
import java.util.Random;

// 获取随机数
public class ScannerTest {
    public static void main(String[] args) {
        Random random = new Random();
        // [0,20)
        int num = random.nextInt(20);
        System.out.println(num);
    }
}

示例: 1、系统自动生成一个随机整数(1-100), 然后由用户输入一个猜测的数字. 如果输入的数字比该随机数小, 提示 “低了”, 如果输入的数字比该随机数大, 提示 “高了” , 如果输入的数字和随机数相等, 则提示 “猜对了” .

public class ScannerTest {
    public static void main(String[] args) {
        Random random = new Random();
        int toguess = random.nextInt(100);
            System.out.println("请输入1-100的数字:");
            Scanner scanner = new Scanner(System.in);
        while (true) {
            int n = scanner.nextInt();
            if (n < toguess) {
                System.out.println("小了");
            } else if (n > toguess) {
                System.out.println("大了");
            } else {
                System.out.println("猜对了");
                break;
            }
        }
        scanner.close();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KuKudebdw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值