面向对象基础的Java作业三

第一题:

使用循环结构显示10000元钱,年利率为5.6,存款期限为10年的每年到期本金加利率的值是多少。

提示:输出10年共10个存款结果,注意下一年的本金是上一年的本金加利率的结果。

代码如下:

public class t5 {
    public static void main(String[] args) {
    double principal = 10000;
    double annualInterestRate = 5.6 / 100;
    int years = 10;
    for (int i = 0; i < years; i++) {
        System.out.println("第" + (i + 1) + "年的本金加利率为:" + (principal * (1 + annualInterestRate)));
        principal += principal * annualInterestRate;
    }
}
}

第二题:

小丫向银行贷款22万元,分30年还清,其首年每月还款1699元,之后每年递减5%,请问30年后,小王总共要向银行还本带利多少钱。

代码如下:

public class t6 {
    public static void main(String[] args) {
        double loanAmount = 220000; // 贷款金额,单位:元
        int years = 30; // 贷款年限
        double monthlyPayment = 1699; // 首年每月还款金额,单位:元
        double interestRate = 0.05; // 每年递减的利率

        // 总还款额计算
        double totalPayment = calculateTotalPayment(loanAmount, years, monthlyPayment, interestRate);

        System.out.printf("小丫30年后总共要向银行还本带利 %.2f 元。", totalPayment);
    }

    /**
     * 计算总还款额
     * @param loanAmount 贷款金额
     * @param years 贷款年限
     * @param monthlyPayment 首年每月还款金额
     * @param interestRate 每年递减的利率
     * @return 总还款额
     */
    public static double calculateTotalPayment(double loanAmount, int years, double monthlyPayment, double interestRate) {
        double totalPayment = 0; // 总还款额
        double remainingLoan = loanAmount; // 剩余贷款金额
        int months = years * 12; // 总月数
        int month = 0; // 当月序号

        while (remainingLoan > 0) {
            totalPayment += monthlyPayment; // 加上当月还款额
            remainingLoan *= (1 + interestRate); // 计算下个月的剩余贷款金额,考虑利息
            month++; // 当月序号加1
            int year = month / 12; // 计算当前年份
            int monthOfYear = month % 12; // 计算当前月份
            if (year > years) break; // 如果已经超过了贷款年限,退出循环
            if (monthOfYear == 12) year++; // 如果是12月份,下一年的年份加1
        }

        return totalPayment; // 返回总还款额
    }
}

第三题:

用循环来实现20个斐波那契数列

1,1,2,3,5,8,13,21,34,55,89…

提示:定义两个变量a1 = 1, a2 = 1, 先输出它们两个的值,然后从第三个数开始计算,计算方法(每次循环中):

(1)新的数字为a1和a2的和,输出产生的新数字

(2)修改新的a1和a2,a1为原a2的值,a2为新产生的数字

代码如下:

public class t7 {
    public static void main(String[] args) {
        int a1 = 1, a2 = 1;
        System.out.print(a1 + "," + a2);
        for (int i = 3; i <= 20; i++) {
            int newNumber = a1 + a2;
            System.out.print("," + newNumber);
            a1 = a2;
            a2 = newNumber;
        }
    }
}

第四题:

水仙花数是指一个 n 位正整数 ( n>=3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153),求100—1000的水仙花数。

代码如下:

public class t8 {
    public static void main(String[] args) {
        for (int i = 100; i < 1000; i++) {
            if (isNarcissisticNumber(i)) {
                System.out.println(i);
            }
        }
    }
    public static boolean isNarcissisticNumber(int num) {
        int a = num / 100;
        int b = (num % 100) / 10;
        int c = num % 10;
        return num == (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3));
    }
}

第五题:

绘制一个20*20的正三角形

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
final class Trang extends JFrame{
    private static final Dimension SIZE = new Dimension(600,400);
    public Dimension getMinimumSize() {return SIZE;}
    public Dimension getMaximumSize() {return SIZE;}
    public Dimension getPreferredSize() {return SIZE;}
    public String getTitle() {return "Trang";}
    private JComponent canvas;
    private Observable containerSizeObserval = new Observable(){
        public void notifyObservers() {
            setChanged();
            super.notifyObservers();
        }
        public void notifyObservers(Object arg) {
            setChanged();
            super.notifyObservers(arg);
        }
    };
    private Trang() throws HeadlessException {
        super();
        init();
        addListeners();
        doLay();
    }
    private void init() {
        TranBrush brush = new TranBrush();
        containerSizeObserval.addObserver(brush);
        canvas = new MyCanvas() {
            Brush getBrush() {
                return brush;
            }
        };
    }
    private void doLay(){
        getContentPane().add(canvas, BorderLayout.CENTER);
        pack();
        setVisible(true);
    }
    private void addListeners(){
//通知画笔重绘
        addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                super.componentResized(e);
                containerSizeObserval.notifyObservers(e.getComponent().getSize());
            }
        });
    }
    public static void main(String... args) {
        SwingUtilities.invokeLater(Trang::new);
    }
    abstract private static class MyCanvas extends JComponent {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            getBrush().paint(g);
        }
        abstract Brush getBrush();
    }
    /**
     * 画笔
     */
    private interface Brush {
        void paint(Graphics g);
    }
    private static class TranBrush implements Brush, Observer {
        private Dimension rectangeSize = new Dimension();
        private Color color = Color.black;
        private Polygon polygon = new Polygon(new int[]{10,30,20}, new int[]{20,20,0},3);
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
            g2.setColor(Color.BLACK);
            g2.fillRect(0,0, rectangeSize.width, rectangeSize.height);
            g2.setColor(color);
            g2.draw(polygon);
        }
        public void update(Observable o, Object arg) {
            if (arg instanceof Dimension) {
                rectangeSize.setSize((Dimension) arg);
            }
            if (arg instanceof Color) {
                color = (Color) arg;
            }
        }
    }
}

运行结果如下:

最后这个题是有点费脑子的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你二舅ya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值