我不确定如何为此进行计算。我把所有东西都放下来,直到我想要实际获得投资金额。我知道我现在所拥有的是错的,但谷歌对我来说并不是非常有用,而且我的书本身并没有我需要的东西才能完成这件事。
这就是我现在所拥有的
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);
}
}
任何帮助都会非常有帮助!
答案 0 :(得分:2)
首先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));
答案 1 :(得分:0)
您不必为此使用4种不同的扫描仪,因为您正在从同一个流中读取....您的代码应该是这样的 -
import java.util.Scanner;
class InvestmentCalculator {
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);
}
}