程序编译但不打印结果

时间:2016-10-22 04:25:17

标签: java

TL:DR程序编译但结果不打印

嗨,我正在为我的第一年编程课程完成这项任务。这是一个问题:

:在这个问题中,您需要计算各种利率和不同时间段的复利。 更确切地说,这意味着给定投资金额和利率(特定金额高于主要利息) 这个任务的费率为1.00%),计算个人在n年后的金额。 计算这个的公式由下式给出:

final amount = amount(1.0 + (prime+rate/100.0)^n)

我们需要从预构建中调用此赋值中的另一个类,因此它们给我们的类是

import java.util.*;

//DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it

public class UserInteraction
{

    public String getStringValueFromUser(String message)

    {

        String value = "";

        Scanner input = new Scanner(System.in);

        System.out.print(message + " : ");

        value = input.nextLine();

        return value;

    }

    //DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it

    public double getDoubleValueFromUser(String message)

    {

       double value = 0;

       Scanner input = new Scanner(System.in);

       System.out.print(message + " : ");

       value = input.nextDouble();

       return value;

    }

   //DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it

   public int getIntValueFromUser(String message)

   {

       int value = 0;

       Scanner input = new Scanner(System.in);

       System.out.print(message + " : ");

       value = input.nextInt();

       return value;

   }


    //DO NOT PUT OTHER METHODS IN THIS CLASS or make any changes to it

}

我的代码在这里

 import java.util.*;

 public class InterestRate

 {

    UserInteraction input =  new UserInteraction();

    public static void main (String[] args)

    {

       InterestRate program = new InterestRate();

       program.execute();

    }
    void execute()
    {
         double initial_money = input.getDoubleValueFromUser("Your initial amount");
         double prime_rate = input.getDoubleValueFromUser("Enter the Prime Rate");
         double InterestRate = input.getDoubleValueFromUser("Enter the Interest Rate as a value above prime");
         double time = input.getDoubleValueFromUser("Enter the length of the investment in years to a decimal");
         double InterestRate2 = input.getDoubleValueFromUser("Enter the second Interest Rate as a value above prime");

         Calculations (initial_money, prime_rate, InterestRate, time);
         Calculations2 (initial_money, prime_rate, InterestRate2, time);


    }

    void finaltotal(double totalrate4, double totalrate4a, double initial_money, double prime_rate, double InterestRate, double time, double InterestRate2)

    {
        double final_amount = (initial_money * totalrate4);
        double final_amounta = (initial_money * totalrate4a);

        printWinnings(final_amount, initial_money, InterestRate, prime_rate, final_amounta, time);
    }

    double Calculations(double initial_money, double prime_rate, double InterestRate, double time)
    {
       double totalrate = prime_rate + InterestRate;
       double totalrate2 = totalrate / 100.0;
       double totalrate3 = totalrate2 + 1.0;
       double totalrate4 = Math.pow(time, totalrate3);


       return totalrate4;


    }
    double Calculations2(double initial_money, double prime_rate, double InterestRate2, double time)
    {
       double totalrate1a = prime_rate + InterestRate2;
       double totalrate2a = totalrate1a / 100.0;
       double totalrate3a = totalrate2a + 1.0;
       double totalrate4a = Math.pow(time, totalrate3a);
       //double final_amounta = initial_money * totalrate4;

       return totalrate4a; 

    }

    void printWinnings(double final_amount, double initial_money, double prime_rate, double InterestRate, double time, double final_amounta)

    {
        System.out.println(" The amount of money you can expect to get over the " + time + " years you invest will be " + final_amount + " with your initial investment of  " + initial_money + " at an rate of " + InterestRate);

    }
}

所以我输入了所有内容,并且程序编译,但是当我运行它时,没有打印出来。

1 个答案:

答案 0 :(得分:1)

从技术上讲,你没有问过你发表的声明。我假设您打算在初始提示输入后询问为什么您的程序不会产生任何结果输出。

如果您按照代码执行,它会像这样:

首先执行main方法:

public static void main(String[] args)
{
    InterestRate program = new InterestRate();
    program.execute();
}

正如您所看到的,execute方法是从main调用的,现在执行就在这里:

void execute() {
    double initial_money = input
            .getDoubleValueFromUser("Your initial amount");
    double prime_rate = input
            .getDoubleValueFromUser("Enter the Prime Rate");
    double InterestRate = input
            .getDoubleValueFromUser("Enter the Interest Rate as a value above prime");
    double time = input
            .getDoubleValueFromUser("Enter the length of the investment in years to a decimal");
    double InterestRate2 = input
            .getDoubleValueFromUser("Enter the second Interest Rate as a value above prime");

    Calculations(initial_money, prime_rate, InterestRate, time);
    Calculations2(initial_money, prime_rate, InterestRate2, time);

}

在我继续之前,我只想提一下这些命名方法,以便它们以大写字母开头,违反了已接受的Java standard naming practice。始终使用camel case命名方法,并以小写字母开头。同样,以这种方式命名变量也违反了Java的命名约定。例如,InterestRate应该是interestRate,否则它会像类名一样读取。通常只有常量在单词之间使用下划线,因此initial_money之类的内容如果写成initialMoney则更符合命名约定。

回到您的代码 - 从我们的逻辑流程停止到CalculationsCalculations2

double Calculations(double initial_money, double prime_rate,
        double InterestRate, double time) {
    double totalrate = prime_rate + InterestRate;
    double totalrate2 = totalrate / 100.0;
    double totalrate3 = totalrate2 + 1.0;
    double totalrate4 = Math.pow(time, totalrate3);

    return totalrate4;

}

double Calculations2(double initial_money, double prime_rate,
        double InterestRate2, double time) {
    double totalrate1a = prime_rate + InterestRate2;
    double totalrate2a = totalrate1a / 100.0;
    double totalrate3a = totalrate2a + 1.0;
    double totalrate4a = Math.pow(time, totalrate3a);
    // double final_amounta = initial_money * totalrate4;

    return totalrate4a;

}

现在您将注意到这些方法都没有调用System.out.printSystem.out.println。这就是你没有看到任何输出的原因。