我不确定如何为此进行计算。 我把一切都做好了,直到我试图真正获得投资金额。 我知道我现在拥有的是错误的,但谷歌对我并没有超级帮助,我的书只是没有我需要的东西来完成这个的事情。
这是我现在所拥有的
import java.util.Scanner;
class InvestmentCalculator {
public static void main(String[] args) {
// create a scanner object
Scanner input = new Scanner(System.in);
// Prompt user to enter investment amount
Scanner amount = new Scanner(System.in);
System.out.println("Enter Investment Amount");
while (!amount.hasNextDouble()) {
System.out.println("Only Integers");
amount.next();
}
//Promt user to enter interest rate
Scanner interest = new Scanner(System.in);
System.out.println("Enter Interest Percentage in decimals");
while (!interest.hasNextDouble()) {
System.out.println("Just like before, just numbers");
interest.next();
}
//Prompt user to enter number of years
Scanner years = new Scanner(System.in);
System.out.println("Enter Number of Years");
while (!years.hasNextDouble()) {
System.out.println("Now you are just being silly. Only Numbers allowed");
years.next();
}
//Compute Investment Amount
double future = amount * Math.pow((1 + interest), (years * 12));
//Display Results
System.out.println("Your future investment amount is " + future);
}
}
任何帮助都会非常非常有帮助!
首先amount
,interest
和years
是Scanners
,它们不是数字。 Scanner
可能包含任意数量的不同类型的内容,因此像Math.pow
这样的东西不可能知道它应该如何处理它们。
您需要将从用户那里读取的值分配给某些变量。
我会从使用单个Scanner
开始,比如说叫kb
...
Scanner kb = new Scanner(System.in);
然后在您想从用户那里获取值时使用它......
你的输入循环对我来说似乎是错误的,我对Scanner
不是特别有经验,所以可能有更好的方法来实现这一目标,但是......
int invest = -1; // amount
double rate = -1; // percentage
int period = -1; // period
do {
System.out.println("Only Integers");
String text = kb.nextLine();
try {
invest = Integer.parseInt(text);
} catch (NumberFormatException exp) {
System.out.println("!! " + text + " is not an integer");
}
} while (invest == -1);
System.out.println(invest);
一旦你有了你需要的所有信息,就用这些基元类型进行计算......
double future = invest * Math.pow((1 + rate), (period * 12));
<</div>
div class="one_answers"> 您不必为此使用 4 个不同的扫描仪,因为您正在从同一个流中读取......你的代码应该是这样的——
import java.util.Scanner;
类投资计算器 {
public static void main(String[] args) {
double amount=0;
int interest=0;
double years=0;
// create a scanner object
Scanner input = new Scanner(System.in);
// Prompt user to enter investment amount
Scanner amount = new Scanner(System.in);
System.out.println("Enter Investment Amount");
while (!input.hasNextDouble()) { // you say you only want integers and are
// reading double values??
System.out.println("Only Integers");
amount = input.nextDouble();
}
//Promt user to enter interest rate
System.out.println("Enter Interest Percentage in decimals");
while (!input.hasNextInt()) {
System.out.println("Just like before, just numbers");
interest= input.nextInt();
}
//Prompt user to enter number of years
System.out.println("Enter Number of Years");
while (!input.hasNextDouble()) {
System.out.println("Now you are just being silly. Only Numbers allowed");
years=input.nextDouble();
}
//Compute Investment Amount
double future = amount * Math.pow((1 + interest), (years * 12));
//Display Results
System.out.println("Your future investment amount is " + future);
}
}