初学者必会java逻辑控制练习题

1、数字9 出现的次数

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

/**
 * Created with IntelliJ IDEA.
 * Description:编写程序数一下 1到 100 的所有整数中出现多少个数字9
 * User: starry
 * Date: 2020 -12 -05
 * Time: 20:42
 */
public class work1 {
    
    
    public static void main(String[] args) {
    
    
        int count=0;
        for(int i=0;i<10;i++){
    
    
            for(int j=0;j<10;j++){
    
    
                if(i==9){
    
    
                    count++;
                }
                if(j==9){
    
    
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

2、输出闰年

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

/**
 * Created with IntelliJ IDEA.
 * Description:输出 1000 - 2000 之间所有的闰年
 * User: starry
 * Date: 2020 -12 -05
 * Time: 20:54
 */
public class work2 {
    
    
    public static void main(String[] args) {
    
    
        for(int i=1000;i<=2000;i++){
    
    
            if(i%4==0&&i%100!=0||i%400==0){
    
    
                System.out.println(i);
            }
        }
    }
}

3、打印素数

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

/**
 * Created with IntelliJ IDEA.
 * Description:打印 1 - 100 之间所有的素数
 * User: starry
 * Date: 2020 -12 -05
 * Time: 21:01
 */
public class work3 {
    
    
    public static void main(String[] args) {
    
    
        int j=0;
        for(int i=1;i<=100;i++){
    
    
            for(j=2;j<i;j++){
    
    
                if(i%j==0){
    
    
                    break;
                }else{
    
    
                    continue;
                }
            }
            if(j==i){
    
    
                System.out.println(i);
            }
        }
    }
}

4、判定素数

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

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:给定一个数字,判定一个数字是否是素数
 * User: starry
 * Date: 2020 -12 -06
 * Time: 9:39
 */
public class work4 {
    
    
    public static void main(String[] args) {
    
    
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        int j=0;
        for(j=2;j<a;j++) {
    
    
            if (a % j == 0) {
    
    
                break;
            } else {
    
    
                continue;
            }
        }
        if(j==a){
    
    
            System.out.println("是素数");
        }
        else{
    
    
            System.out.println("不是素数");
        }
    }
}

5、年龄打印

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

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:根据输入的年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
 * User: starry
 * Date: 2020 -12 -06
 * Time: 9:49
 */
public class work5 {
    
    
    public static void main(String[] args) {
    
    
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        if(a<0){
    
    
            System.out.println("输入无效");
        }
        if(a<=18){
    
    
            System.out.println("少年");
        }else if(a>18&&a<=28){
    
    
            System.out.println("青年");
        }else if(a>28&&a<=55){
    
    
            System.out.println("中年");
        }else{
    
    
            System.out.println("老年");
        }
    }
}

6、打印 X 图形

输入描述:
多组输入,一个整数(2~20),表示输出的行数,也表示组成“X”的反斜线和正斜线的长度。
输出描述:
针对每行输入,输出用“*”组成的X形图案。

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:打印X图形
 * User: starry
 * Date: 2020 -12 -06
 * Time: 9:55
 */
public class work6 {
    
    
    public static void main(String[] args) {
    
    
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        for(int i=1;i<a+1;i++) {
    
    
            for(int j=1;j<a+1;j++){
    
    
                System.out.print(" ");
                if(i==j){
    
    
                    System.out.print("*");
                }
                if(j==(a-i+1)){
    
    
                    System.out.print("*");
                }
            }
            System.out.println("");
        }
    }
}

7、猜数字游戏

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

import java.util.Random;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:完成猜数字游戏 ,用户输入数字,判断该数字是大于,小于,还是等于随机生成的数字,等于的时候退出程序。
 * User: starry
 * Date: 2020 -12 -06
 * Time: 10:15
 */
public class work7 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int randNum = random.nextInt(100)+1;//[1-101)
        while (true) {
    
    
            int inputNum = scanner.nextInt();
            if(inputNum < randNum  ) {
    
    
                System.out.println("小");
            }else if(inputNum > randNum) {
    
    
                System.out.println("大");
            }else {
    
    
                System.out.println("==");
                break;
            }
        }
    }
}

8、水仙花数

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

import java.lang.Math;
/**
 * Created with IntelliJ IDEA.
 * Description:求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本 身,如;153=1+5+3?,则153是一个“水仙花数“。)
 * User: starry
 * Date: 2020 -12 -06
 * Time: 10:22
 */
public class work8 {
    
    
    public static void main(String[] args) {
    
    
        for(int i=0;i<10;i++){
    
    
            if(i==Math.pow(i,3)){
    
    
                System.out.println(i);
            }
        }
        for(int i=10;i<100;i++){
    
    
            int j=i/10;//十位
            int n=i%10;
            if(Math.pow(n,3)+Math.pow(j,3)==i){
    
    
                System.out.println(i);
            }
        }
        for(int i=100;i<1000;i++){
    
    
            int a=i%10;//个位
            int j=i/10;
            int b=j%10;//十位
            int c=j/10;//百位
            if(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)==i){
    
    
                System.out.println(i);
            }
        }
    }
}

9、计算分数的值

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

import java.lang.Math;
/**
 * Created with IntelliJ IDEA.
 * Description:计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值 。
 * User: starry
 * Date: 2020 -12 -06
 * Time: 10:50
 */
public class work9 {
    
    
    public static void main(String[] args) {
    
    
        double sum=0;
        for(int i=1;i<101;i++){
    
    
            sum=sum+Math.pow(-1,i+1)*(1.0/i);
        }
        System.out.println(sum);
    }
}

10、最大公约数

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

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:求两个正整数的最大公约数
 * User: starry
 * Date: 2020 -12 -06
 * Time: 10:59
 */
public class work10 {
    
    
    public static void main(String[] args) {
    
    
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        int b=cin.nextInt();
        int min=(a>b?a:b);
        for(int i=min;i>0;i--){
    
    
            if(a%i==0&&b%i==0){
    
    
                System.out.println(i);
                break;
            }
        }
    }
}

11、二进制1的个数

求一个整数,在内存当中存储时,二进制1的个数。

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:求一个整数,在内存当中存储时,二进制1的个数。
 * User: starry
 * Date: 2020 -12 -06
 * Time: 11:09
 */
public class work11 {
    
    
    public static void main(String[] args) {
    
    
        int count=0;
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        if(a<0){
    
    
            a=-a;
            count++;
        }
        for(int i=0;true;i++){
    
    
            if(a%2==0){
    
    
                a=a/2;
                continue;
            }else{
    
    
                a=a/2;
                count++;
            }
            if(a==0){
    
    
                break;
            }
        }
        System.out.println(count);
    }
}

12、二进制序列

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

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列
 * User: starry
 * Date: 2020 -12 -06
 * Time: 11:28
 */
public class work12 {
    
    
    public static void main(String[] args) {
    
    
        Scanner cin=new Scanner(System.in);
        System.out.println("请输入数字:");
        int value=cin.nextInt();
        System.out.println("偶数序列:");
        for(int i=31;i>0;i-=2){
    
    
            System.out.print((value>>i)&1);
        }
        System.out.println("");
        System.out.println("奇数序列:");
        for(int i=30;i>=0;i-=2){
    
    
            System.out.print((value>>i)&1);
        }
    }
}

13、模拟登陆

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

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
 * 可以重新输 入,最多输入三次。三次均错,则提示退出程序
 * User: starry
 * Date: 2020 -12 -06
 * Time: 11:56
 */
public class work13 {
    
    
    public static void main(String[] args) {
    
    
        int i=3;
        Scanner cin=new Scanner(System.in);
        String a="123456";
        System.out.println("请输入密码");
        while(i>0){
    
    
            String b=cin.nextLine();
            if(a.equals(b)){
    
    
                System.out.println("登陆成功");
                break;
            }else{
    
    
                System.out.println("密码错误");
            }
            i--;
        }
    }
}

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

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

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:输出一个整数的每一位,如:123的每一位是1 , 2 , 3
 * User: starry
 * Date: 2020 -12 -06
 * Time: 12:04
 */
public class work14 {
    
    
    public static void main(String[] args) {
    
    
        int i;
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt();
        int b=a;
        for(i=1;true;i++){
    
    
            if(b/10==0){
    
    
                break;
            }
            b=b/10;
        }
        int[] arr=new int[i];
        for(int j=0;j<i;j++){
    
    
            arr[j]=a%10;
            a=a/10;
        }
        for(int j=i-1;j>=0;j--){
    
    
            System.out.println(arr[j]);
        }
    }
}

15、输出乘法口诀表

输出n*n的乘法口诀表,n由用户输入。

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:输出n*n的乘法口诀表,n由用户输入。
 * User: starry
 * Date: 2020 -12 -06
 * Time: 13:04
 */
public class work15 {
    
    
    public static void main(String[] args) {
    
    
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        for(int i=1;i<=n;i++){
    
    
            for(int j=1;j<=i;j++){
    
    
                System.out.print(i+"*"+j+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/starry1441/article/details/111824078