Java:switch语句的复合兴趣问题 [英] Java: Compound interest problem with switch statement

查看:150
本文介绍了Java:switch语句的复合兴趣问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试使用switch语句创建复利计算器,该语句提供4个不同的帐户,每个帐户持有不同的利率帐户为S,D,C或L.用户输入他们希望的金额在帐户中,然后他们选择他们想要使用的帐户,然后它应该在他们的帐户1年后输出他们的钱,但它只输出他们的初始金额,下面是我的代码,我感谢任何帮助!



Hi I'm trying to create a compound interest calculator using a switch statement which offers 4 different accounts which each hold a different interest rate the accounts are S, D, C or L. The user enters the amount they wish to place in the account then they choose which account they'd like to use and then then it should output the value after of their money after being in the account for 1 year but it's only outputting their initial amount, below is my code I appreciate any help!

import java.util.*;

public class CompoundInterest {
    public static void main(String[] args) {
    double amount;
    double rate = 0;
    double time = 1;
    char group;
    Scanner keyboard = new Scanner(System.in);
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter an amount");
    
    int p = sc.nextInt();
    
    System.out.println("Please enter an account type (S, D, C or L");
    group = keyboard.next() .charAt(0);
   
    switch(group)
    {
        case 'S' : rate = 3.0;
        break;
        case 'D' : rate = 0.5;
        break;
        case 'C' : rate = 1.5;
        break;
        case 'L' : rate = 4.0;
    }
    amount = p * Math.pow(1+rate, time);
    System.out.println("Value after one year is: " + amount +"" );
    }

}

推荐答案

您的公式不正确,请参阅http://www.icsejavatutorial.com/programs/compound-interest [ ^ ]。您的费率值也是100倍,它们应该是0.03,0.005,0.015,0.04。
Your formula is not correct, see http://www.icsejavatutorial.com/programs/compound-interest[^]. Also your rate values are 100 times too high, they should be 0.03, 0.005, 0.015, 0.04.


这篇关于Java:switch语句的复合兴趣问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆