JAVA经典习题详解

JAVA经典习题详解

前言

以下习题有不理解的可以私信我,若习题有错误也请大家指出,非常感谢。

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

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 3;
        while (count != 0) {
            System.out.println("请输入密码,你还剩" + count + "次机会");
            String password = sc.nextLine();
            if (password.equals("1234")) { //字符串比较相等不相等
                System.out.println("登陆成功");
                break;
            } else {
                System.out.println("密码错误");
                count--;
            }
        }
    }

二、输出一个整数的每一位

public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入一个数字");
       while (sc.hasNextInt()) {
           int number = sc.nextInt();
           while (number != 0) {
               System.out.println(number % 10 + " ");
               number /= 10;
           }
       }
   }
}

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

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 31; i >= 0; i -= 2) {           //输出奇数位
            System.out.print(((n >>> i) & 1) + " ");//0&1=0 1&1=1
        }
        System.out.println();
        for (int i = 30; i >= 0; i -= 2) {        //输出偶数位
            System.out.print(((n >>> i) & 1) + " ");
        }
    }
}

四、完成猜数字游戏,用户输入数字,判断改数字是大于还是小于,还是等于随机生成的数字,等于是退出程序。

  public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random r = new Random();
        int number = r.nextInt(100) + 1;
        System.out.println(number);      //先输出随机数
        while (true) {
            System.out.println("请输入你要猜的数字");
            int num = sc.nextInt();
            if (num < number) {
                System.out.println(num + "猜小了");
            } else if (number < num) {
                System.out.println(num + "猜大了");
            } else {
                System.out.println("猜中了");
                break;
            }
        }
    }

五、输入一个数,求100~输入数字以内的水仙花数

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入");
        int n = sc.nextInt();
        for (int i = 100; i < n; i++) {
            int count = 0;
            int flg = i;
            while (flg != 0) {
                count++;
                flg /= 10;
            }
            int sum = 0;
            flg = i;
            while (flg != 0) {
                sum += Math.pow(flg % 10, count);//求flg%10的count次方
                flg /= 10;
            }
            if (sum == i) {
                System.out.println(i);
            }
        }
    }
}

六、给两个数求最大公约数

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入第一个数字");
       int num1 = sc.nextInt();
       System.out.println("请输入第二个数字");
       int num2 = sc.nextInt();
       if (num2 > num1) {             //辗转相除法
           int temp = 0;
           temp = num1;
           num1 = num2;
           num2 = temp;
       }
       int c = num1 % num2;
       while (c != 0) {
           num1 = num2;
           num2 = c;
           c = num1 % num2;
       }
       System.out.println(num2);
   }
}

七、求一个数字二进制中一的个数

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数");
        int n = sc.nextInt();
        int count = 0;
        while (n != 0) {
            count++;
            n = n & (n - 1);         
        }
        System.out.println(count);
    }
  • 18
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小强在此

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值