JAVA逻辑控制刷题

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

public class control {
    
    
    public static int sum(int n) {
    
    
        int count=0;
        for(int i=0;i<=n;i++) {
    
    
            if(i%10==9){
    
    
                count++;
            }
            if(i/10==9) {
    
    
                count++;
            }
        }
        return count;
    }
    public static void main(String[] args) {
    
    
        int n=100;
        int s=sum(n);
        System.out.println("9的个数为"+s);
    }
}

在这里插入图片描述

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

public class means {
    
    
    public static void main(String[] args) {
    
    
        int s=0;
        int i=0;
        for(i=1000;i<2000;i++) {
    
    
            if((i%4==0&&i%100!=0)||(i%400==0)) {
    
    
                System.out.println(i);
            }
        }
    }
}

在这里插入图片描述

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

public class control {
    
    
    public static void main(String[] args) {
    
    
        int i=0;
        for(i=2;i<100;i++) {
    
    
            boolean s=fun(i);
            if(s==true) {
    
    
                System.out.println(i);
            }
        }
    }
    public static boolean fun(int n) {
    
    
        for(int i=2;i<n/2;i++) {
    
    
            if(n%i==0) {
    
    
                return false;
            }
        }
        return true;
    }
}

在这里插入图片描述

4.给定一个数字,判定一个数字是否是素数

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

在这里插入图片描述

5.求出0~999之间的所有“水仙花数”并输出。

import java.util.Scanner;

public class control {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        fun(n);
    }
    public static void fun(int n) {
    
    

        for(int i = 100;i <= n;i++) {
    
    
            int sum = 0;
            int count = 0;
            int tmp = i;
            while (tmp != 0) {
    
    
                count++;
                tmp /= 10;
            }
            tmp = i;//123
            while (tmp != 0) {
    
    
            sum += Math.pow(tmp%10,count);
            tmp = tmp/10;
        }
        if(sum == i) {
    
    
            System.out.println(i);
        }
    }
}
}

在这里插入图片描述

6.JAVA版猜数字游戏

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

public class control {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc=new Scanner(System.in);
        //电脑需要随机生成数字
        Random random =new Random();
        int rand =random.nextInt(100)+1;

        while (true) {
    
    
            System.out.println("请输入数字:");
            int num = sc.nextInt();
            if(num == rand) {
    
    
                System.out.println("相等");
                break;
            }else if(num < rand) {
    
    
                System.out.println("小了");
            }else {
    
    
                System.out.println("大了");
            }
        }
    }
}

在这里插入图片描述

7.编写代码模拟三次密码输入的场景

import java.util.Scanner;

public class control{
    
    
    public static void main(String[] args) {
    
    
        guessPassword();
    }
    public static void guessPassword() {
    
    
        Scanner scanner =new Scanner(System.in);
        int sum=3;
        while (sum!=0) {
    
    
            System.out.println("请输入你要猜的密码:");
            String password =scanner.nextLine();
            if(password.equals("java")) {
    
    
                System.out.println("登录成功!");
                return ;
            } else {
    
    
                sum--;
                System.out.println("你还有"+sum+"次机会");
            }
        }
    }
}

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_52988578/article/details/119479664