0

我不确定如何为此进行计算。在我试图真正获得投资金额之前,我已经准备好了一切。我知道我现在拥有的东西是错误的,但谷歌对我并没有太大帮助,而且我的书没有我需要的东西来完成这件该死的事情。

这是我现在拥有的

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);

    }
}

任何帮助都会非常有帮助!

4

2 回答 2

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));
于 2013-10-04T01:51:52.983 回答
0

您不必为此使用 4 种不同的扫描仪,因为您正在从同一个流中读取......您的代码应该是这样的 -

导入 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);

}

}

于 2013-10-04T05:03:06.847 回答