JAVA基础:数据类型与运算符 (习题笔记)

1.输入自己的名字,年龄和性别,分别用不同的变量接收,并将输入的信息做输出。

java 复制代码
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你的名字:");
        String name = input.next();
        System.out.println("请输入你的年龄:");
        int age = input.nextInt();
        System.out.println("请输入你的性别:");
        String sex = input.next();

        System.out.println("你的姓名是:" + name + "\n你的年龄是:" + age + "\n你的性别是:" + sex);
    }

2.输入圆形半径,求圆形的周长和圆形的面积,并将结果输出。

java 复制代码
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入圆的半径:");
        final double PI = 3.14;//常量
        double r = input.nextDouble();
        double zhouchang = 2 * PI * r;
        double mianji = PI * r * r;
        System.out.println("该圆的半径是:R=" + r +
                "\n该圆的周长是:C=" + 2 + "*" + PI + "*" + r + "*" + "=" + zhouchang +
                "\n该圆的面积是:S=" + PI + "*" + r + "*" + r + "=" + mianji);

    }

3.银行利率表如下表所示,请计算存款10000元,活期1年、活期2年,定期1年,定期2年后的本息合计。

结果如下图所示。(结果四舍五入,不保留小数位。使用Math.round(double d)实现)

java 复制代码
//方法一
 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        final double BJ = 10000;//常量
        double huo1 = BJ * (1 + 0.35 / 100);
        double ding1 = BJ * (1 + 1.50 / 100);
        double huo2 = huo1 * (1 + 0.35 / 100);
        double ding2 = BJ * (1 + 2.10 / 100 * 2);
        System.out.println("本金" + BJ +
                "\n活期1年本金总计:" + Math.round(huo1) +
                "\n定期1年本金总计:" + Math.round(ding1) +
                "\n活期2年本金总计:" + Math.round(huo2) +
                "\n定期2年本金总计:" + Math.round(ding2));

    }
//方法二
 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int principal = 10000;//本金

        double currentRate = 0.35E-2;//活期年利率
        double regularRate1 = 1.50E-2;//定期1年的年利率
        double regularRate2 = 2.10E-2;//定期2年的年利率

        double current1 = principal * (1 + currentRate);//活期1年本金总计
        double current2 = principal * (1 + currentRate * 2);//活期2年本金总计
        double regular1 = principal * (1 + regularRate1);//定期1年本金总计
        double regular2 = principal * (1 + regularRate2 * 2);//定期2年本金总计

        System.out.println("本金:" + principal);
        System.out.println("活期1年本金总计:" + Math.round(current1));
        System.out.println("定期1年本金总计:" + Math.round(regular1));
        System.out.println("活期2年本金总计:" + Math.round(current2));
        System.out.println("定期2年本金总计:" + Math.round(regular2));


    }

4.某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。结果如图所示。

java 复制代码
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个4位正整数");
        int zheng = input.nextInt();
        int qian = ((zheng / 1000) + 5) % 10;
        int bai = (((zheng / 100) % 10) + 5) % 10;
        int shi = (((zheng / 10) % 10) + 5) % 10;
        int ge = ((zheng % 10) + 5) % 10;
        System.out.println("加密后的数字为:" + ge + shi + bai + qian);

    }
相关推荐
Escalating_xu5 分钟前
【Python】使用库:标准库、第三方库、pip、项目实战与程序打包
java·python·pip
Wang's Blog23 分钟前
Java框架 SpringCloud 快速入门: 过滤器链执行顺序
java·spring cloud
geovindu36 分钟前
rust: Abstract Factory pattern
开发语言·后端·设计模式·rust·抽象工厂模式
砚底藏山河38 分钟前
量化实战:回测数据底座快照与版本管理进阶
java·大数据·python·金融·maven
“AI国潮设计-小江”39 分钟前
《Python+SDXL实战:用ControlNet批量生成“英歌舞麻将糕”IP,附自动化脚本与商用思路》
开发语言·人工智能·python·prompt·aigc
Cc.Y1 小时前
Java零基础入门:封装与继承 —— 从“裸奔“到“穿衣服“,从“重复造轮子“到“站在巨人肩膀上“
java·开发语言
Escalating_xu1 小时前
【C 语言】深入理解指针(4):回调函数、qsort、void * 与泛型排序的模拟实现
java·c语言·开发语言
Shaoshing1 小时前
雪花算法
java
w_zero_one1 小时前
链表(4)
java·数据结构·算法
吠品1 小时前
GitHub 周榜拆解:决策小模型扎堆上位
c语言·开发语言·算法