【javaSE】 程序逻辑控制练习代码

简介: 【javaSE】 程序逻辑控制练习代码

练习1

根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)

import java.util.Scanner;
public class Age {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n <= 18) {
            System.out.println("少年");
        } else if(n <= 28) {
            System.out.println("青年");
        } else if(n <= 55) {
            System.out.println("中年");
        } else {
            System.out.println("老年");
        }
        sc.close();
    }
}

练习2

判定一个数字是否是素数

import java.util.Scanner;
public class PrimeNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int i = 2;
        boolean a = true;
        while(i <= n/2) {
            if(n%i == 0) {
                a = false;
                break;
            }
        }
        if(a) {
            System.out.println(n+"是素数");
        } else {
            System.out.println(n+"不是素数");
        }
        sc.close();
    }
}

练习3

打印 1 - 100 之间所有的素数

public class PrintPrime {
    public static void main(String[] args) {
        int n = 1;
        for(n = 1;n <= 100;n++) {
            int i = 2;
            boolean a = true;
            while(i <= n/2) {
                if(n%i == 0) {
                    a = false;
                    break;
                }
                i++;
            }
            if(a) {
                System.out.print(n+" ");
            }
        }
    }
}

练习4

输出 1000 - 2000 之间所有的闰年

public class LeapYear {
    public static void main(String[] args) {
        int a = 1000;
        for(a = 1000;a < 2000;a++) {
            if(a%100 == 0) {
                if(a%400 == 0) {
                    System.out.println(a+"是闰年");
                }
            } else if(a%4 == 0) {
                System.out.println(a+"是闰年");
            }
        }
    }
}

练习5

输出乘法口诀表

public class MultiplicationTable {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;
        for(i = 1;i <= 9;i++) {
            for(j = 1;j <= i;j++) {
                System.out.printf("%d*%d=%d ",j,i,i*j);
            }
            System.out.println();
        }
    }
}

练习6

求两个正整数的最大公约数

import java.util.Scanner;
public class CommonDivisor {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        boolean con = true;
        int c = 1;
        for( c = a<b?a:b;c>1;c--) {
            if(a%c == 0&&b%c == 0) {
                System.out.println(c+"为最大公约数");
                con = false;
                break;
            }
        }
        if(con) {
            System.out.println("没有公约数");
        }
    }
}

练习7

求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,

如: 153=1^3+5^3+3^3 ,则153是一个“水仙花数”。)

public class NarcissisticNumber {
    public static void main(String[] args) {
        int i = 1;
        int x = 0;
        double w = 1.0;
        for(i = 0;i<1000;i++) {
            x = 0;
            double p = Math.pow(10.0,w);
            while (i/Math.pow(10.0,w) > 1) {
                p = i/Math.pow(10.0,w);
                w++;
            }
            int j = i;
            while (j>0) {
                x += Math.pow(j%10,w);
                j /= 10;
            }
            if (x == i) {
                System.out.println(i);
            }
        }
    }
}

练习8

写一个函数返回参数二进制中 1 的个数,比如: 15 0000 1111 4 个 1

import java.util.Scanner;
public class BinarySystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count = 0;
        while ( n != 0) {
            if ((n&1) == 1) {
                count++;
            }
            n >>= 1;
        }
        System.out.println(count);
        sc.close();
    }
}

练习9

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

import java.util.Scanner;
public class BinarySequence {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int j = n;
        int o = n;
        int i = 0;
        while(i < 16) {
            System.out.print(j&1);
            j >>= 2;
            i++;
        }
        System.out.println();
        o >>= 1;
        i = 0;
        while(i < 16) {
            System.out.print(o&1);
            o >>= 2;
            i++;
        }
    }
}

总结

关于《程序逻辑控制练习代码》就讲解到这儿,欢迎各位留言交流以及批评指正,如果文章对您有帮助或者觉得作者写的还不错可以点一下关注,点赞,收藏支持一下。

相关文章
|
Linux 测试技术 Android开发
Linux下Android Studio 3.6以及最新版安装图文详解
Linux下Android Studio 3.6以及最新版安装图文详解
1104 0
|
存储 安全 Java
Spring Boot整合Spring Security--学习笔记
Spring Boot整合Spring Security--学习笔记
605 1
|
6月前
|
安全 API 开发工具
阿里云音视频通信RTC全栈对接指南:从架构原理到多端集成实战
本文系统讲解阿里云音视频通信RTC的完整对接流程,涵盖产品架构、服务开通、应用创建、Token鉴权机制、Web/Android/iOS/Windows等多端SDK集成、服务端API调用、旁路转推与云端录制等高级功能。文章深入分析了信令与媒体分离的分布式架构设计,提供了各端接入的完整代码示例,并包含计费模式解析、安全合规建议及常见问题解答,是一份兼具理论深度和工程实践的技术指南。
|
12月前
|
人工智能 缓存 测试技术
AI 英语口语 APP 的开发
针对AI英语口语APP开发验收,需重点关注端到端语音延迟与口语评测颗粒度。验收核心包括:语音交互响应延迟(首字&lt;800ms,首音&lt;1.5s)、打断响应(&lt;300ms)、抗噪能力;评测引擎的多维度评分、音节级纠错与录音对比;AI引导对话、语法纠偏与语速调节;成本控制如缓存、Token统计与多模型降级;并交付Prompt库、音色授权及压力测试报告。#AI英语 #AI教育 #软件外包公司
|
11月前
|
人工智能 资源调度 Cloud Native
不是模型变强,而是系统需要一个 AI 调度官来维持秩序
过去一年,系统复杂度已超越人力掌控极限。云原生时代,服务动态交互、自动决策频发,传统“人+规则”模式失效。系统正演化为具备生命特征的生态,必须引入AI调度官作为智能中枢,实现全局认知、调度与稳定治理。
354 1
|
11月前
|
人工智能 自然语言处理 安全
2026 国内常用的智能客服系统推荐,企业服务智能化优选
在数字化竞争时代,智能客服已升级为体验中枢与增长引擎。本文对比瓴羊Quick Service、探域智能体、智齿科技等五大主流系统,从场景适配、技术稳定、生态整合等维度解析差异。瓴羊凭借阿里生态与全场景AI能力,在高并发、全渠道服务中表现突出,成为多行业优选方案,助力企业实现服务智能化转型。
2026 国内常用的智能客服系统推荐,企业服务智能化优选
|
数据可视化 安全 中间件
5G视频在公安110接处警的应用
乐科技术将5G视频坐席融入110接处警系统,实现从“听得见”到“看得见”的跨越。通过实时视频连接,提升警情研判精准度与响应速度,强化沟通效率与证据采集,助力智慧警务升级,为公共安全构筑科技防线。
|
人工智能 运维 监控
HarmonyOS NEXT~鸿蒙系统运维:全面解析与最佳实践
本书《HarmonyOS NEXT~鸿蒙系统运维:全面解析与最佳实践》深入探讨了鸿蒙系统的运维管理。从架构特点到实际操作,涵盖分布式能力、性能优化、安全维护及故障排查。内容包括设备管理、系统监控、安全管理等核心任务,提供常见问题解决方案与工具推荐。面对未来超级终端和AI赋能的挑战,运维人员需不断学习,以充分发挥鸿蒙的分布式优势,为用户带来流畅体验。
1123 8
|
存储 编译器 C++
【C/C++】 静态内存分配与动态内存分配
C/C++ 中静态内存分配与动态内存分配相关内容,区别与比较
1125 0