java笔试编程题1

面试了几家公司,笔试题有很多类型,一些像考试试卷一样,一些就是简单的程序题,当然现在还没找到工作,先把记得的题分享一下。以下题是面试过程中遇到的题目,自己通过代码敲出来的,全部可以运行成功,当然代码可能不唯一,但这是我能想到最简洁的代码了,欢迎大家提出建议,持续更新中,建议看到此篇博客的Java友也能手动自己敲一遍,便于记忆。

1.利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

import java.util.Scanner;

public class demo {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        System.out.println("请输入分数:");
        int score = sr.nextInt();
        String str = null;
        str = score > 90 ? "A" : score < 60 ? "C" : "B";
        System.out.println("等级为"+str);
    }
}

1.1利用条件运算符的嵌套来完成此题:学习成绩> 80分的同学用A表示,70-80分之间的用B表示,60-70分之间的用C表示,60分以下的用D表示。

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner sr=new Scanner(System.in);
        System.out.println("请输入score:");
        int score=sr.nextInt();
        String lv=score>80?"A":score>70?"B":score>60?"C":"D";
        System.out.println("lv:"+lv);
    }
}

2
题目:判断101-200之间有多少个素数,并输出所有素数。

import java.util.Scanner;

public class demo {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 100; i <= 200; i++) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                count++;
                System.out.print(i + "\t");
            }
        }
        System.out.println();
        System.out.println("素数个数:" + count);
    }

}

结果`

101	103	107	109	113	127	131	137	139	149	151	157	163	167	173	179	181	191	193	197	199	
素数个数:21

3.输入两个正整数m和n,求其最大公约数和最小公倍数

import java.util.Scanner;

//最小公倍数,最大公约数
public class Demo2 {
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        System.out.println("请输入第一个数");
        int x = sr.nextInt();
        System.out.println("请输入第二个数");
        int y = sr.nextInt();

        int min = x < y ? x : y;
        int max = x * y;
        int yue = 0;//最大公约数
        for (int i = min; i <= max; i++) {
            if (i % x == 0 && i % y == 0) {
                System.out.println("最小公倍数:" + i);
                yue = max / i;
                System.out.println("最大公约数" + yue);
                break;
            }

        }
    }
    
}

结果

请输入第一个数
15
请输入第二个数
20
最小公倍数:60
最大公约数5

4.不死兔
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?

import java.util.Scanner;

//不死兔
public class Demo4 {
    public static void main(String[] args) {
        System.out.println("请输入月份:");
        Scanner sr=new Scanner(System.in);
        getDuiShuEveryMonth(sr.nextInt());

    }

    private static void getDuiShuEveryMonth(int month) {
        for (int i = 1; i <= month; i++) {
            System.out.println("第" + i + "个月的兔子的对数为:" + getDuiShu(i));
        }
    }

    private static int getDuiShu(int month) {
        if (month == 0) {
            return 0;
        } else if (month == 1) {
            return 1;
        } else if (month == 2) {

            return 1;
        } else {
            return getDuiShu(month - 1) + getDuiShu(month - 2);
        }
    }

}

结果

请输入月份:
7
第1个月的兔子的对数为:1
第2个月的兔子的对数为:1
第3个月的兔子的对数为:2
第4个月的兔子的对数为:3
第5个月的兔子的对数为:5
第6个月的兔子的对数为:8
第7个月的兔子的对数为:13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值