首页 > 基础资料 博客日记

【Java SE】循环一些基本练习

2023-12-14 21:28:17基础资料围观221

这篇文章介绍了【Java SE】循环一些基本练习,分享给大家做个参考,收藏Java资料网收获更多编程知识

判定一个数字是否是素数

public class Test {
    public static int is_sushu(int n) {
        if(n == 1) {
            return 0;
        }
        int i ;
        for (i = 2; i <= Math.sqrt(n); i++) {
            if(n % i == 0 ) {
               break;
            }
        }
        if (i > n) {
            return 1;
        }
        return 0;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ret = is_sushu(n);
        if(ret == 1 ) {
            System.out.println(n + "是素数");
        } else {
            System.out.println(n + "不是素数");
        }
    }
}

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

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

 输出乘法口诀表

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

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

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        if(b > a) {
            int tmp = b;
            b = a;
            a = b;
        }
        int c =a % b;
        while(c != 0) {
            a = b;
            b = c;
            c = a % b;
        }
        System.out.println(b);
    }
}

 求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数
本身,如: 153=1^3+5^3+3^3 ,则153是一个“水仙花数”。)

public class Test {
    public static void main(String[] args) {
        int i;
        for (i = 100; i < 1000 ; i++) {
            int k = i;
            int sum = 0;
            while(k != 0) {
                sum+=Math.pow(k%10,3);
                k /= 10;
            }
            if (sum == i) {
                System.out.println(i);
            }
        }
    }
}

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

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = 0;
        /*for (int i = 0; i < 32; i++) {
            if ((n >> i & 1) == 1) {
                count++;
            }
        }*/
        while (n != 0) {
            count++;
            n = n & (n-1);
        }
        System.out.println(count);
    }
}

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

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i;
        // 00000000 00000000 00000000 00001010
        // 00000000 00000000 00000000 00000001
        // 00000000 00000011
        // 00000000 00000000
        System.out.print("偶数位:");
        for ( i = 31; i > 0; i-=2) {
            if((n >> i & 1) == 1) {
                System.out.print(1 + " ");
            } else {
                System.out.print(0 + " ");
            }
        }
        System.out.println("");
        System.out.print("奇数位:");
        for ( i = 30; i >= 0; i-=2) {
            if((n >> i & 1) == 1) {
                System.out.print(1 + " ");
            } else {
                System.out.print(0 + " ");
            }
        }
    }
}

 

 猜数字游戏

import java.util.Random;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //获取随机数
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int n = random.nextInt(100);// [0,100)
        int count =  3;
        while (count != 0) {
            System.out.println("输入你的数字");
            int ret = scanner.nextInt();
            if(ret > n) {
                System.out.println("猜大了");
            } else if(ret < n) {
                System.out.println("猜小了");
            } else {
                System.out.println("恭喜你猜对了");
                break;
            }
            count--;
            if(count == 0) {
                System.out.println("你没有机会了");
                break;
            }
            System.out.println("你还有" + count + "机会");
        }
    }
}

 


文章来源:https://blog.csdn.net/chaodddddd/article/details/134485555
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云