简体   繁体   English

程序编译但不打印结果

[英]Program Compiles but does not print results

TL:DR program compiles but results do not print out TL:DR程序已编译,但结果未打印出来

Hi, I am working on this assignment for my 1st year programming class. 嗨,我正在为我的一年级编程课做这项作业。 Here is the question: 这是问题:

: In this problem, you need to compute compound interest for various interest rates and various time periods. :在此问题中,您需要计算各种利率和不同时间段的复利。 More precisely, this means that given an amount to invest and an interest rate (a specific amount above the prime interest rate, which for this assignment is 1.00 percent), compute the amount of money an individual would have after n years. 更精确地讲,这意味着给定一个投资金额和一个利率(高于最优惠利率的特定金额,对于此分配,该金额为1.00%),计算出一个人在n年后将拥有的金额。 The formula to calculate this is given by: 计算公式如下:

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

we need to invoke another class in this assignment from a prebuilt so the class they gave us was 我们需要从预构建中调用此分配中的另一个类,所以他们给我们的类是

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

}

and my code is here 我的代码在这里

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

    }
}

So I typed it all up, and the program compiles, but when I run it, nothing prints out. 因此,我将其全部键入,程序得以编译,但是当我运行它时,没有任何输出。

Technically you didn't ask a question you made a statement. 从技术上讲,您没有提出问题就发表了声明。 I assume you meant to ask why your program does not produce any output of results after the initial prompts for input. 我假设您要问的是为什么在最初的输入提示后,您的程序为什么不输出任何结果。

If you follow the execution of your code it goes like this: 如果您遵循代码的执行,则代码将如下所示:

The main method is executed first: 首先执行main方法:

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

As you can see your execute method is called from main , so now the execution goes here: 如您所见,您的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);

}

Before I move on I just want to mention that naming methods so that they begin with a capital letter violates accepted Java standard naming practice . 在继续之前,我只想提及命名方法,使它们以大写字母开头,这违反了公认的Java标准命名惯例 Methods are always named using camel case and starting with lowercase. 方法始终使用驼峰式大小写并以小写字母开头。 Similarly, naming variables in this manner also violates the naming conventions of Java. 同样,以这种方式命名变量也违反了Java的命名约定。 For example, InterestRate should be interestRate because otherwise it reads like a class name. 例如, InterestRate应该是interestRate因为否则它读起来就像一个类名。 Generally only constants use underscores between words so things like initial_money would be more consistent with naming convention if written as initialMoney . 通常,只有常量在单词之间使用下划线,因此,如果将initial_money编写为initialMoney与命名约定更加一致。

Getting back to your code - from where we left off your logic flows into Calculations and Calculations2 : 回到您的代码-从我们中断您的逻辑的地方开始进入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;

}

Now you will notice none of these methods have any calls to System.out.print or System.out.println . 现在,您会注意到这些方法都没有对System.out.printSystem.out.println任何调用。 This is why you don't see any output. 这就是为什么您看不到任何输出的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM