【java练习题】4.程序逻辑控制

例题1:计算5的阶乘

public class TestDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int sum = 1;
        for (int i = 1; i <= n; i++){
            sum *= i;
        }
        System.out.println(sum);
    }
}
//运行结果
手动输入5
120

例题2:计算1!+2!+3!+4!+5!

public class TestDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int add_sum = 0;
        //循环n次
        for (int i = 1; i <= n; i ++){
            //计算阶乘i!
            int sum = 1;
            for (int j = 1; j <= i; j++){
                sum *= j;
            }
            add_sum += sum;
        }
        System.out.println(add_sum);
    }
}

image-20240122223314393

这里我们发现, 当一个代码中带有多重循环的时候, 代码的复杂程度就大大提高了. 而比较复杂的代码就更容易出错.

后面我们会采用更简单的办法来解决这个问题.

例题3:根据年龄,打印当前年龄的人是少年(不超过18), 青年(19-28), 中年(29-55), 老年(56以上)

public class TestDemo {
    public static void main(String[] args) {
        while(true){
            Scanner scanner = new Scanner(System.in);
            int year = scanner.nextInt();
            if (year <= 18){
                System.out.println("少年!!!");
            }else if (year >= 19 && year <=28){
                System.out.println("青年!!!");
            }else if (year >= 29 && year <= 55){
                System.out.println("中年!!!");
            }else{
                System.out.println("老年!!!");
            }
        }
    }
}

image-20240123155114539

例题4:判断一个数字是否是素数

分析:素数只能被 1 和他本身整除

法一:

public class TestDemo {
    public static void main(String[] args) {
        while(true){
            Scanner scanner = new Scanner(System.in);
            int x = scanner.nextInt();
            int i = 2;
            for (i = 2; i < x; i++){
                if (x % i == 0){
                    System.out.println("不是素数");
                    break;
                }
            }
            if (i == x){
                System.out.println("是素数");
            }
        }
    }
}

法二:优化

public class TestDemo {
    public static void main(String[] args) {
        while(true){
            Scanner scanner = new Scanner(System.in);
            int x = scanner.nextInt();
            int i = 2;
            for (i = 2; i <= Math.sqrt(x); i++){
                if (x % i == 0){
                    System.out.println("不是素数");
                    break;
                }
            }
            if (i > Math.sqrt(x)){
                System.out.println("是素数");
            }
        }
    }
}

例题5:打印1-100之间所有的素数

public class TestDemo {
    public static void main(String[] args) {
        //从 2 开始循环到 100
        for (int x = 2; x <= 100; x++){

            //判断当前 x 是不是素数
            int i = 2;
            for (i = 2; i <= Math.sqrt(x); i++){
                if (x % i == 0){
                    //System.out.println(x + "不是素数");
                    break;
                }
            }
            if (i > Math.sqrt(x)){
                System.out.println(x + "是素数");
            }

        }
    }
}

image-20240123161145386

例题6:输出1000-2000之间所有的闰年

闰年的定义:

1、非整百年份:能被4整除的是闰年。(如2004年就是闰年,2001年不是闰年)

2、整百年份:能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)

public class TestDemo {
    public static void main(String[] args) {
        //从 1000 开始循环到 2000
        for (int i = 1000; i <= 2000; i++) {
            if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0){
                System.out.println(i + "是闰年");
            }
        }
    }
}

例题7:输出乘法口诀表

public class TestDemo {
    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();
        }
    }
}

image-20240123161909100

例题8:求两个整数的最大公约数

法一:辗转相除法

定理:gcd(a,b) = gcd(b,a mod b)

证明:a可以表示成a = kb + r,则r = a mod b
假设d是a,b的一个公约数,则有
d|a, d|b,而r = a - kb,因此d|r
因此d是(b,a mod b)的公约数
假设d 是(b,a mod b)的公约数,则
d | b , d |r ,但是a = kb +r
因此d也是(a,b)的公约数
因此(a,b)和(b,a mod b)的公约数是一样的,其最大公约数也必然相等,得证

示例:

1999615的最大公约数
1999/615=3······152
615/152=4······7
152/7=21······5
7/5=1······2
5/2=2······1
2/1=2······0
public class TestDemo {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        //输入时 x要大于y
        int x = scanner.nextInt();
        int y = scanner.nextInt();

        while (x%y != 0){
            int swap = x%y;
            x = y;
            y = swap;
        }
        System.out.println(y);
    }
}

image-20240123164558130

例题9:计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。

public class TestDemo {
    public static void main(String[] args) {
        double sum = 0;
        int flag = 1;
        for (int i = 1; i <= 100; i++){
            sum += 1.0/i *flag;
            flag = -flag;
        }
        System.out.println(sum);
    }
}

//运行结果
0.688172179310195

例题10:编写程序数一下 1到100的所有整数中出现多少个数字9

分析,1到100中出现的数字9有20个:9、19、29、39、49、59、69、79、89、90、91、92、93、94、95、96、97、08、99(99中有两个数字9).

public class TestDemo {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <=100; i++){
            if(i%10==9){
                count++;
            }
            if (i/10==9){
                count++;
            }
        }
        System.out.println(count);
    }
}

如果问的是,有多少个数字中包含9,那么是19个

public class TestDemo {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <=100; i++){
            if(i%10==9 || i/10==9){
                count++;
            }
        }
        System.out.println(count);
    }
}

例题11:求出0~99999之间的所有“水仙花数”并输出(“水仙花数”是指一个n位数,其各位数字的n次方和恰好等于该数本身,如153是一个3位数:153=13+53+3^3,则153是一个“水仙花数”)

public class TestDemo {
    public static void main(String[] args) {
        for (int i = 1; i <= 99999; i++){
            int count = 0;
            int tmp = i;
            //计算i包含几位
            while(tmp != 0){
                count++;
                tmp = tmp/10;
            }

            //将每一位的count次方加在一起
            tmp = i;
            int sum = 0;
            while(tmp != 0){
                sum += Math.pow(tmp%10,count);
                tmp = tmp/10;
            }

            if (sum == i){
                System.out.println(i + "符合条件");
            }
        }
    }
}

image-20240123181026666

例题12:编写代码 编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误, 可以重新输入,最多输入三次。三次均错,则提示退出程序

public class TestDemo {
    public static void main(String[] args) {
        int count = 3;
        while (count != 0){
            Scanner scanner = new Scanner(System.in);
            String password = scanner.nextLine();
            if (password == "123456"){
                System.out.println("密码正确");
                break;
            }else {
                count--;
                System.out.println("密码错误,你还有"+count+"次机会");
            }
        }
        if (count == 0){
            System.out.println("退出程序!!!");
            return;
        }
    }
}

image-20240123182120247

发现,即使密码正确,但输出确实密码错误

分析,虽然 password 和 “123456” 都是 String 类型,但是二者不能用 password == "123456"来进行比较,而应该用password.equals("123456")来进行字符串比较。

因此正确代码是:

public class TestDemo {
    public static void main(String[] args) {
        int count = 3;
        while (count != 0){
            Scanner scanner = new Scanner(System.in);
            String password = scanner.nextLine();
            if (password.equals("123456")){
                System.out.println("密码正确");
                break;
            }else {
                count--;
                System.out.println("密码错误,你还有"+count+"次机会");
            }
        }
        if (count == 0){
            System.out.println("退出程序!!!");
            return;
        }
    }
}

image-20240123182512590

例题13:写一个函数返回参数的二进制表示中1的个数,比如 参数:15, 二进制表示:0000 1111,其中有4个1

public class TestDemo {
    public static void main(String[] args) {
        int count = 0;
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        System.out.printf("%x\n",input);
        while(input != 0){
            //举例 0011 & 0001 结果若为 1 说明二进制最低位为 1
            if ((input & 1) != 0){
                count++;
            }
            //将 0011 无符号右移 1 位
            input = input >>> 1;
        }
        System.out.println(count);
    }
}

image-20240123184244473

例题14:输出一个整数的每一位

public class TestDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        while(input != 0){
            System.out.println(input%10);
            input /= 10;
        }
    }
}

image-20240123183344894

例题15:获取一个数的二进制序列中所有的偶数位和奇数位,分别输出二进制序列

image-20240123190435697

public class TestDemo {
    public static void main(String[] args) {
        int n = 7;
        for (int i = 30; i >= 0; i=i-2){
            System.out.print(((n>>>i)&1)+" ");
        }
        System.out.println();
        for (int i = 31; i >= 1; i=i-2){
            System.out.print(((n>>>i)&1)+" ");
        }
    }
}

image-20240123190649992

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值