1)更改程序,以便用户输入初始余额和利率。假设用户将以整数输入利率(例如,“5”表示5%的利率)。假设用户将输入仅为数字的初始余额 - 不使用逗号。 2)更改代码以显示投资需要多少年才能达到三倍。
我已进入扫描仪,因此用户可以输入他们的余额和兴趣。无论用户输入什么,输出2000。
import java.util.Scanner;
public class InvestmentRunner
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please Enter Initial Balance:");
String Balance = in.next();
System.out.print("Please Enter Interest Rate:");
String Interest = in.next();
final double INITIAL_BALANCE = 10000;
final double RATE = 5;
Investment invest = new Investment(INITIAL_BALANCE, RATE);
invest.waitForBalance(2 * INITIAL_BALANCE);
int years = invest.getYears();
System.out.println("The investment doubled after "
+ years + " years");
}
}
答案 0 :(得分:0)
根据问题评论员的评论:well no matter what the user inputs the output stays at 2000
这些是您构造Investment
对象的行,在此对象上调用方法,然后打印该值。
Investment invest = new Investment(INITIAL_BALANCE, RATE);
invest.waitForBalance(2 * INITIAL_BALANCE);
int years = invest.getYears();
System.out.println("The investment doubled after " + years + " years");
你还没有发布Investment
类,所以我必须假设这些方法没问题。问题在这里:
final double INITIAL_BALANCE = 10000;
final double RATE = 5;
这些是您发送给Investment
构造函数的变量。您已经硬编码的常量。同时,这是您接受用户输入的地方:
System.out.print("Please Enter Initial Balance:");
String Balance = in.next();
System.out.print("Please Enter Interest Rate:");
String Interest = in.next();
您选择Balance
(应为balance
),然后选择Interest
(应为interest
),然后您不对这些值执行任何操作。您需要解析这些double
中的String
,然后将它们作为参数发送给您的构造函数。