Java练习

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

这道题需要了解什么是闰年,

普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。

世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。

public static void main(String[] args){
    System.out.printl("1000~2000之间的闰年:");
    for(int year = 1000; year <= 2000; year++){
       if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
           System.out.print(year+" ");
       }
    }
}
    

2、1~10的和

public static void main(String[] args) {
        //1~10的和
        int a = 1;
        int sum = 0;
        while (a <= 10){
            sum += a;
            a++;
        }
        System.out.println(sum);
    }

3、5的阶乘

public static void main(String[] args) {
        //5的阶乘
        int i = 1;
        int ret = 1;
        while (i <= 5){
            ret *= i;
            i++;
        }
        System.out.println(ret);
    }

4、1~5的阶乘相加

public static void main(String[] args) {
        //1~5的阶乘相加
        int n = 1;
        int sum = 0;
        while (n <= 5){//各阶乘积相加
            int i = 1;
            int ret = 1;
            while (i <= n){//1~5阶乘
                ret *= i;
                i++;
            }
            sum += ret;
            n++;
        }
        System.out.println(sum);
    }

5、判定一个数字是否是素数

这道题需要理解什么为素数,素数又称质数,指在大于1的自然数中,除了1和该数自身外,无法被其他自然数整除的数。

public static void main(String[] args) {
        //判定一个数字是否是素数
        System.out.printf("请输入一个数:");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        for(int i = 2;i < num;i++){
            if(num % i == 0){
                System.out.println("该数字不是素数。");
                return;
            }
        }
        System.out.println("该数字是素数。");
    }

//方法二
public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int x = scanner.nextInt();
        int i = 2;
        while (i <= Math.sqrt(x)){//Math.sqrt(x)  x的开根号
            if(x % i == 0){
                System.out.println(x+ " 不是素数!");
                return;
            }
            i++;
        }
        System.out.println(x+" 是素数!");
    }

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

public static void main(String[] args) {
        int num = 0;
        for (num = 3; num < 100; num++){
            boolean flag = true;//boolean为true打印num
            for (int i = 2; i <= Math.sqrt(num); i++){
                if (num % i == 0){
                    flag = false;
                    break;
                }
            }
            if(flag){
                System.out.println(num+" 为素数");
            }
        }
    }

7、编写程序数一下 1到 100 的所有整数中出现多少个数字9

public static void main(String[] args) {
        //编写程序数一下 1到 100 的所有整数中出现多少个数字9
        int num = 1;
        int count = 0;
        while (num < 100){
            if(num % 10 == 9){
                count++;
            }
            //需要考虑到99
            if (num / 10 == 9){
                count++;
            }
            num++;
        }
        System.out.println(count);
    }

8、计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值

public static void main(String[] args) {
        //计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
        double sum = 0;
        int ret = 1;
        for (int i = 1; i <= 100; i++){
            sum += 1.0/i *ret;//ret用来处理+-
            ret = -ret;
        }
        System.out.println(sum);
    }

9、输出一个整数的每一位,如:123的每一位是3,2,1

public static void main(String[] args) {
        //输出一个整数的每一位,如:123的每一位是3,2,1
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        while (num > 0){
            int a = 0;
            a = num % 10;
            num = num/10;
            System.out.print(a+",");
        }
    }

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

public static void main(String[] args) {
        /*编写代码模拟三次密码输入的场景。
        最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
        可以重新输 入,最多输入三次。三次均错,则提示退出程序 */
        int count = 3;
        Scanner scanner = new Scanner(System.in);
        while (count != 0){
            System.out.println("请输入你的密码:");
            String passWorld = scanner.nextLine();
            if(passWorld == "123456"){
                System.out.println("登录成功!");
            }else {
                count--;
                System.out.println("密码错误!");
                System.out.println("你还有"+count+"次机会");
            }
        }
        System.out.println("退出程序!");
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值