-5

1) Change the program so that the user has to enter the initial balance and the interest rate. Assume that the user will enter interest rates in whole numbers ("5" would represent an interest rate of 5%, for example). Assume that the user will enter initial balances that are numeric only - no commas. 2) Change the code to display how many years it takes an investment to triple.

I have entered my scanner so the user can input their balance and interest. no matter what user enters it outputs 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");


  }   
}  
4

1 回答 1

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从这些Strings 中解析出一个,然后将它们作为参数发送给您的构造函数。

于 2013-10-29T23:21:53.397 回答