Java基础练习题

这些Java代码示例展示了基础的编程练习,包括预测子女身高、输出对应星期、计算银行定期存款利息、计算个人所得税后的工资、判断手机号办卡费用、提取邮箱用户名以及实现不同类型的死循环。此外,还有一个经典的数学问题——鸡兔同笼问题的解法。
摘要由CSDN通过智能技术生成

Java基础练习题

一、预测身高案例

我们可以通过父母的身高大致推断出子女的身高,假定父母与子女的身高遗传关系如下:
​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2
​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2
那已知:现有父亲身高175CM,母亲身高160CM。

public class t1 {
    public static void main(String[] args) {
        double fatherH = 175.00;
        double motherH = 160.00;
        double dh = (fatherH*0.923+motherH)/2;
        double sh = (fatherH+motherH)*1.08/2;
        System.out.println(dh);
        System.out.println(sh);
    }
}

二、输入数字1~7,输出对应星期几

public class t2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        switch (a){
            case 1:
                System.out.println("周一");
                break;
            case 2:
                System.out.println("周二");
                break;
            case 3:
                System.out.println("周三");
                break;
            case 4:
                System.out.println("周四");
                break;
            case 5:
                System.out.println(5);
                break;
            case 6:
                System.out.println(6);
                break;
            case 7:
                System.out.println(7);
                break;
            default:
                System.out.println("傻逼吗你");

        }
    }
}

三、银行收入计算

某银行推出了整存整取定期储蓄业务,其存期分为一年、两年、三年、五年,到期凭存单支取本息。存款年利率表如下:
​ 存期 年利率(%)
​ 一年 2.25
​ 两年 2.7
​ 三年 3.25
​ 五年 3.6
请存入一定金额(1000起存),存一定年限(四选一),计算到期后得到的本息总额。
提示:
​ 存入金额和存入年限均由键盘录入
​ 本息计算方式:本金+本金×年利率×年限

public class t3 {
    public static void main(String[] args) {
        int jine = new Scanner(System.in).nextInt();
        if (jine < 1000){
            System.out.println("这么点钱还想存,趴一边去");
        }else {
            int year = new Scanner(System.in).nextInt();
            double gmoey = getMoney(jine,year);
            System.out.println(gmoey);
        }



    }
    public static double getMoney (int jine,int year){
        double money = 0.0;


           if (year == 1){
               money = (double) (jine + jine * 3.25/100*year);
           } else if (year == 2) {
               money = (double)(jine + jine * 2.7/100*year);
           } else if (year == 3) {
               money = (double)(jine + jine * 3.25/100*year);
           } else if (year == 5) {
               money = (double)(jine + jine * 3.6/100*year);
           }else {
               System.out.println("存不了,滚蛋");
           }

        return money;
    }

}

四、求税后工资问题

2019年1月1日起,国家推出新的个人所得税政策,起征点上调值5000元。也就是说税前工资扣除三险一金(三险一金数额假设是税前工资的10%)后如果不足5000元,则不交税。如果大于5000元,那么大于5000元的部分按梯度交税,具体梯度比例如下:
​ 0 ~ 3000元的部分,交税3%
​ 3000 ~ 12000元的部分,交税10%
​ 12000 ~ 25000的部分 , 交税20%
​ 25000 ~ 35000的部分,交税25%
​ 35000 ~ 55000的部分,交税30%
​ 55000 ~ 80000的部分,交税35%
​ 超过80000的部分,交税45%
比如:小蓝入职一家企业后,税前工资是18000,则他每月该交个税的部分是18000-1800-5000=11200元,个税缴纳数额是3000×3%+8200×10%=910元。税后工资15290元。
请完成一个个税计算程序,在用户输入税前工资后,计算出他对应的纳税数额,以及税后工资为多少?

public class t4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你的月薪:");
        double beforSalary = sc.nextDouble();

        double calculate = calculate(beforSalary);
        System.out.println("需要交税的金额是:" + calculate);
        double afterScalary = beforSalary * 0.9 - calculate;
        System.out.println("税后工资是:" + afterScalary);
    }

    public static double calculate(double beforSalary) {
        double shuiHou = beforSalary * (1 - 0.1) - 5000;
        double shui = 0;
        if (shuiHou <= 3000) {
            shui = shuiHou * (0.03);
        } else if (shuiHou > 3000 && shuiHou <= 12000) {
            shui = 3000 * (0.03) + (shuiHou - 3000) * 0.1;
        } else if (shuiHou > 12000 && shuiHou <= 25000) {
            shui = 3000 * (0.03) + 9000 * (0.1) + (shuiHou - 12000) * 0.2;
        } else if (shuiHou > 25000 && shuiHou <= 35000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + (shuiHou - 25000) * 0.25;
        } else if (shuiHou > 35000 && shuiHou <= 55000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + (shuiHou - 35000) * 0.3;
        } else if (shuiHou > 55000 && shuiHou <= 80000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + 20000 * 0.3 + (shuiHou - 55000) * 0.35;
        }else if (shuiHou > 80000 ) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + 20000 * 0.3 + 25000 * 0.35 + (shuiHou - 80000) * 0.45;
        }
        return shui;
    }
}

五、手机选号:根据用户输入的手机号来确认用户实际支付的价格

如果尾数为8,需支付办卡费50元
如果尾数为4,需支付办卡费用0元
如果是其他尾号,需支付办卡费用20

public class t5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入你的手机号");
        String tel  = sc.nextLine();
        getPrice(tel);

    }
    public static void getPrice (String tel){
        char  charss = tel.charAt(tel.length()-1);
        //第一种方法
//        if (tel.length() !=  11){
//            System.out.println("非法,请重新输入");
//        }else if (charss == 8){
//            System.out.println("费用为50元");
//        } else if (charss == 4) {
//            System.out.println("免费");
//        } else {
//            System.out.println("费用为20");
//        }
        //第二种
        if (tel.length() !=  11){
            System.out.println("非法,请重新输入");
        }
        switch (charss){
            case 8:
                System.out.println("需支付办卡费50元");
            case 4:
                System.out.println("需支付办卡费用0元");
            default:
                System.out.println("需支付办卡费用20");
        }
    }

}

六、获取邮箱名字

`
接收用户输入的邮箱名,获取邮箱的名字
比如:cxy@163.com,输出cxy

主要考察字符串中  查询指定字符第一次所在的索引
                  int  indexOf(int ch) 查找当前字符 在字符串中第一次出现的索引位置
               截取字符串
                    String substring(int startIndex,int endIndex)    截取指定索引范围内的字符串 start  end
String类
     查看字符串索引
        public  char charAt(int index) 根据索引找字符
        int  indexOf(int ch) 查找当前字符 在字符串中第一次出现的索引位置
        int indexOf(int ch,int fromIndex) 从fromIndex开始查询
        int lastIndexOf(int ch)  查询指定字符 在字符串中最后一次出现的索引
        int lastIndexOf(String str)
      字符串截取
        String substring(int startIndex)  从开始索引截取到末尾
        String substring(int startIndex,int endIndex)    [start,end)

`


public class t6 {
    public static void main(String[] args) {
        System.out.println("输入你的邮箱");
        String email = new Scanner(System.in).nextLine();
        String name = getName(email);
        System.out.println("邮箱名是:" + name);
    }
    public static String  getName(String email){
        int index = email.indexOf("@");
        String name = email.substring(0,index);
        return name;
    }
}

七、分别通过for循环/While循环/do-While循环写一个死循环

public class t7 {
    public static void main(String[] args) {
        //for循环的死循环
//        for (int i = 0; ; i++) {//不对i的范围进行限制
//            System.out.println("今日分享的是基础内容");
//        }
        //while循环
//        while (true){
//            System.out.println("只要一直返回true,循环就不会停止");
//        }
        //do while循环
        do{
            System.out.println("先执行一次do循环,后判断为true,继续下一次循环  但是一直为true 循环不会停止");
        }while (true);
    }
}

八、鸡兔同笼问题

public class t8 {
    public static void main(String[] args) {
        for (int kun = 0 , t = 35; kun <= 35; kun++ , t--) {
            if ((kun * 2 + t * 4) == 94){
                System.out.println("坤坤有:"+ kun + "只");
                System.out.println("兔子有:"+ t + "只");
            }
        }
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值