JavaSE第四节————逻辑控制(下)

系列文章目录

JavaSE第四节————逻辑控制(下)

逻辑控制

  1. 顺序结构
  2. 分支结构
  3. 循环结构
  4. 输入输出
  5. 猜数字游戏
  6. 练习


一、猜数字游戏

游戏规则

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

生成随机数的方法( * * *

//生成随机数
            Random random = new Random(); // 默认随机种子是系统时间
            int toGuess = random.nextInt(100);//生成[0-100)的随机数不包含100

参考代码( * *

public static void main(String[] args) {
            //生成随机数
            Random random = new Random(); // 默认随机种子是系统时间
            int toGuess = random.nextInt(100);//生成[0-100)的随机数不包含100

            Scanner sc = new Scanner(System.in);
            // System.out.println("要猜的数字: " + toGuess);
            while (true) {
                System.out.println("请输入要输入的数字: (1-100)");
                int num = sc.nextInt();
                if (num < toGuess) {
                    System.out.println("低了");
                } else if (num > toGuess) {
                    System.out.println("高了");
                } else {
                    System.out.println("猜对了");
                    break;
                }
            }
            sc.close();
        }

在这里插入图片描述

二、练习

1.打印年龄分布

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

import java.util.Scanner; 需要导入 util 包,放在第一行
    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; 需要导入 util 包,放在第一行
    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 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 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 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; 需要导入 util 包,放在第一行
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是一个“水仙花数”。)
153 % 10 = 3
153 /10 = 12

12 % 10 = 2
12 / 10 = 1

1 % 10 = 1
1 / 10 =1

方法一

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);
                    }
                }
            }

在这里插入图片描述

方法二

  public static void main(String[] args) {
        // 153 = 1^3 + 5^3 + 3^3

        for (int i = 1; i < 1000; i++) {
            int count = 0;//用来计算i 是几位数
            int temp = i;//避免i的值被改变
            while (temp != 0){
                count++;
                temp = temp / 10;
            }
            //count的值计算完成后,i不改变

            //计算i的每一位
            temp = i;
            int sum = 0;
            while (temp != 0){
                 sum += Math.pow(temp%10,count);
                 temp /= 10;
            }
            if(sum == i ){
                System.out.println(i);
            }
           // 153 % 10 = 3
            //153 /10 = 12

           // 12 % 10 = 2
            //12 / 10 = 1

            //1 % 10 = 1
            //1 / 10 =0
        }
    }

在这里插入图片描述

8.写一个函数返回参数二进制中 1 的个数 * * *

比如: 15 0000 1111 4 个 1

方法一

7 ---- 0111
7 & 1 ---- 0001

7 ---- 0111
7 >> 1 ---- 0011

import java.util.Scanner; 需要导入 util 包,放在第一行
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();
        }

在这里插入图片描述

方法二

7(n) ---- 0111
6(n-1) ---- 0110
7 & 6 ---- 0110

6 ---- 0110
5 ---- 0101
6 & 5 — 0100

4 ---- 0100
3 ---- 0011
4 & 3 — 0000

import java.util.Scanner; 需要导入 util 包,放在第一行
public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int count = 0;
            int temp = n;
            while ( temp != 0){
                count++;
                temp = temp & (temp - 1);
            }
        System.out.println(n + "有" + count + "个1");
    }

在这里插入图片描述

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

方法一

import java.util.Scanner; 需要导入 util 包,放在第一行
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++;
            }

        }

在这里插入图片描述

方法二

import java.util.Scanner; 需要导入 util 包,放在第一行
 public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();//7----0000 0000 0000 0000 0000 0000 0000 0111
            //从左往右数

            //奇数位
            System.out.print("奇数位");
            for (int i = 31; i >=0 ; i-=2) {//有32位二进制数因此i= 31 i>= 0 ,>>一次判断是否是1
                System.out.print(((n >>> i) & 1) +" " );
            }

            System.out.println();

            //偶数位
            System.out.print("偶数位");
            for (int i = 30; i >=0 ; i-=2) {//有32位二进制数因此i= 31 i>= 0 ,>>一次判断是否是1
                System.out.print(((n >>> i) & 1) +" " );
            }
        }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值