Java 编程新手,需要少量帮助

Java 编程新手,需要少量帮助,java,Java,因此,基本上,我的程序编译正确,并且工作正常。但是,当程序计算单利时,显示的值不正确。假设它应该显示470美元,而只是打印出4.7,对不起,解释不好,有人能解释为什么会发生这种情况吗 import java.io.*; import java.util.Scanner; import java.io.File; //import java.nio.file.*; //import java.nio.file.Paths; public class BankInterest { public

因此,基本上,我的程序编译正确,并且工作正常。但是,当程序计算单利时,显示的值不正确。假设它应该显示470美元,而只是打印出4.7,对不起,解释不好,有人能解释为什么会发生这种情况吗

import java.io.*;
import java.util.Scanner;
import java.io.File;
//import java.nio.file.*;
//import java.nio.file.Paths;
public class BankInterest {

  public static void main (String [] args) throws IOException
  {

    /* TASK 1: Declare variables */

  Scanner user_input = new Scanner (System.in);

  boolean exit;
  int accountType;
    double balance;
    double principal;
  // double userInterest;
  double r;
  double r2;
  int year;

  String commBank = ("commbank.txt");
  String westPac = ("westpac.txt");

    /*Check if the expected command line is provided */

  if (args.length < 1) {
    /* Display the Usage */
    System.out.println("Usage: java BankInterest interestRateFileName");
    /* Programs quits with an error code */
    System.exit(-1);
 }

    /* TASK 2: Read interest rates from a file */

    String filename = (args[0]);
    Scanner textReader = new Scanner(new File(filename));
    r = textReader.nextDouble();
    r2 = textReader.nextDouble();
    System.out.println(r);
    System.out.println(r2);

    /* TASK 3: Take user input - Which Account */

    Scanner keyboard = new Scanner (System.in);
    System.out.println("Which Account: ");
    System.out.println("1 - Savings");
    System.out.println("2 - Term Deposits");

    accountType = keyboard.nextInt();

    if (accountType == 1) {
      accountType = 1;
 }
    else if (accountType == 2) {
      accountType = 2;
 }

    /* TASK 4: Take user input - Principal and Period */

    Scanner input = new Scanner (System.in);
    System.out.println("Principal: ");
    principal = keyboard.nextDouble();

    System.out.println("Years: ");
    year = keyboard.nextInt();

    /* TASK 5: Calculate balance for the chosen account type */

    if (accountType == 1) {
      double userBalance = principal * Math.pow((1 + r/100), year);
        double userInterest = userBalance-principal;
        System.out.println("");
      System.out.println("The Compound Interest is: " + userBalance);
    System.out.println("The total amount of Interest earned:" + userInterest);
  }
    else if (accountType == 2) {
      double userBalance = (principal * r2 * year) / 100;
        double userInterest = userBalance-principal;
     System.out.println("");
      System.out.println("The Simple Interest is: " + userBalance);
    System.out.println("The total amount of Interest earned:" + userInterest);

  }
 }
}
import java.io.*;
导入java.util.Scanner;
导入java.io.File;
//导入java.nio.file.*;
//导入java.nio.file.path;
公共级银行权益{
公共静态void main(字符串[]args)引发IOException
{
/*任务1:声明变量*/
扫描仪用户输入=新扫描仪(System.in);
布尔出口;
国际会计类型;
双平衡;
双重主体;
//双重用户利益;
双r;
双r2;
国际年;
字符串commBank=(“commBank.txt”);
字符串westPac=(“westPac.txt”);
/*检查是否提供了预期的命令行*/
如果(参数长度<1){
/*显示用法*/
System.out.println(“用法:java BankInterest InterestActeFileName”);
/*程序退出时出现错误代码*/
系统退出(-1);
}
/*任务2:从文件中读取利率*/
字符串文件名=(参数[0]);
扫描仪文本阅读器=新扫描仪(新文件(文件名));
r=textReader.nextDouble();
r2=textReader.nextDouble();
系统输出println(r);
系统输出打印项次(r2);
/*任务3:接受用户输入-哪个帐户*/
扫描仪键盘=新扫描仪(System.in);
System.out.println(“哪个帐户:”);
System.out.println(“1-节省”);
系统输出打印项次(“2-定期存款”);
accountType=keyboard.nextInt();
如果(accountType==1){
accountType=1;
}
else if(accountType==2){
accountType=2;
}
/*任务4:获取用户输入-本金和期间*/
扫描仪输入=新扫描仪(System.in);
System.out.println(“委托人:”);
principal=keyboard.nextDouble();
System.out.println(“年:”);
年份=键盘.nextInt();
/*任务5:计算所选帐户类型的余额*/
如果(accountType==1){
双用户余额=本金*数学功率((1+r/100),年份);
双重用户利息=用户余额本金;
System.out.println(“”);
System.out.println(“复利为:“+userBalance”);
System.out.println(“赚取的利息总额:“+用户利息”);
}
else if(accountType==2){
双用户余额=(本金*r2*年)/100;
双重用户利息=用户余额本金;
System.out.println(“”);
System.out.println(“简单的兴趣是:“+userBalance”);
System.out.println(“赚取的利息总额:“+用户利息”);
}
}
}

首先,公式是错误的。右侧为(假设
r2
表示%):


我想这只是因为你把利率定为%而已。我的意思是20%的利率,你档案中的价值是0.20。在你的计算中,再除以100。将兴趣值更改为20或在计算中去掉“除以100”部分。

使用调试器查看发生了什么,或者如果您不熟悉使用调试器(最好学习调试器),也可以在不同的位置打印这些值,以查看它们是否是您期望的值。是否停止除以100?我想我们需要更多的信息,比如你的输入是什么,然后我们才能帮助你。打印错误的数字,所有的数字(计算利息、总利息、单利)都会出现这种情况吗?它似乎总是打印1/100的数字,它应该考虑张贴一个。拆下/更换所有不需要证明问题的部件。我用年份表示钱在银行的年数,而不是百分比。这表明我们仍然缺少一些解释。答案可能是正确的,但你需要解释为什么它是错误的,然后发布正确的答案。这是一个简单的公式。我应该解释什么S.i.=本金*利率*时间/100@Ashish,我相信你的公式是不正确的,或者至少不是OP希望使用的公式。如果在末尾添加一次利息,@Ashish的公式是正确的。如果每年添加一次并持续一年以上,则@Perfecture原始公式中的基本思想是正确的。如果每年添加2到4次,我们还需要另一种配方。
double userInterest = (principal * r2 * year) / 100;
double userBalance = principal + userInterest;