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);

    }
相关推荐
TTBIGDATA8 分钟前
【Hue】Ambari 页面启动 Hue 失败 user ‘hadoop‘ does not exist
java·hadoop·ambari
饺子大魔王的男人1 小时前
Remote JVM Debug+cpolar 让 Java 远程调试超丝滑
java·开发语言·jvm
兩尛7 小时前
c++知识点2
开发语言·c++
fengfuyao9857 小时前
海浪PM谱及波形的Matlab仿真实现
开发语言·matlab
xiaoye-duck8 小时前
C++ string 底层原理深度解析 + 模拟实现(下)——面试 / 开发都适用
开发语言·c++·stl
不会代码的小猴8 小时前
Linux环境编程第六天笔记--system-V IPC
linux·笔记
Hx_Ma168 小时前
SpringMVC框架提供的转发和重定向
java·开发语言·servlet
乌恩大侠9 小时前
【笔记】USRP 5G 和 6G 参考架构
笔记·5g
biuyyyxxx9 小时前
Python自动化办公学习笔记(一) 工具安装&教程
笔记·python·学习·自动化
期待のcode9 小时前
原子操作类LongAdder
java·开发语言