在Java中的基本数学计算
在Java中的基本数学计算
我正在学习Java,并且正在开发一个基本程序,用户可以输入一个成本金额、通胀百分比和时间(年份),以便计算包含通胀的成本。\n我使用了三个类,1个应用程序类,1个驱动程序类和1个单一类。我这样做是为了按照书本上的要求来构建面向对象的技能(我知道还有更简单的过程化/结构化方法,但这不是我练习的重点)。\n我遇到的问题是,无论我输入什么数字,输出的答案都是0美元。这显然不应该是这样的。如果有人能够请快速浏览我的代码并指点我方向,我将不胜感激。\n应用程序类:\n
package priceestimator; public class PriceEstimator { public static void main(String[] args) { CostCalculator CostCalc = new CostCalculator(); CostCalc.calculateCost(); } }
\n驱动程序类:\n
public class CostCalculator { public void calculateCost(){ Calculator calculator = new Calculator(); calculator.calculator(); Calculator years = new Calculator(); years.years(); Calculator inflation = new Calculator(); inflation.inflation(); Calculator price = new Calculator(); price.price(); } }
\n单一类:\n
package priceestimator; import java.util.Scanner; public class Calculator { private Scanner myScanner = new Scanner(System.in); private double cost; private double time; private double costDif; private void setTime(int time){ this.time = time; } private double getTime(){ return time; } private void setCost(double cost){ this.cost = cost; } private double getCost(){ return cost; } private void setCostDif(double costDif){ this.costDif = costDif; } private double getCostDif(){ return costDif; } private void getMyScanner(){ this.myScanner = myScanner; } public void calculator(){ System.out.println("Enter the current cost (numbers only):"); cost = myScanner.nextDouble(); } public void years(){ System.out.println("Enter the time difference in years:"); time = myScanner.nextDouble(); } public void inflation(){ double costTimeDif; double decimalConversion; int percent = 100; double input; System.out.println("Enter the current inflation rate percentage (numbers only):"); input = myScanner.nextDouble(); decimalConversion = input / percent; costTimeDif = time * decimalConversion; costDif = cost * costTimeDif; } public void price(){ double price; price = costDif + cost; System.out.println("The estimated price is: $" + price); } }
\n感谢您的时间。