Java 如何计算一大堆不同的扫描仪输入?

Java 如何计算一大堆不同的扫描仪输入?,java,java.util.scanner,Java,Java.util.scanner,我不知道该怎么做这个计算。我把所有的东西都记下来,直到我想得到投资金额。我知道我现在所拥有的是错误的,但是谷歌对我并没有太大的帮助,我的书也没有我需要的东西来完成这该死的事情 这是我现在的东西 import java.util.Scanner; class InvestmentCalculator { public static void main(String[] args) { // create a scanner object Scanner

我不知道该怎么做这个计算。我把所有的东西都记下来,直到我想得到投资金额。我知道我现在所拥有的是错误的,但是谷歌对我并没有太大的帮助,我的书也没有我需要的东西来完成这该死的事情

这是我现在的东西

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

    }
}

任何帮助都会非常有帮助

首先,
金额
利息
扫描器
,它们不是数字。
Scanner
可以包含任意数量的不同类型的内容,因此像
Math.pow
这样的东西不可能知道应该如何处理它们

您需要将从用户处读取的值分配给一些变量

我首先使用一个
扫描仪
,比如说
kb

Scanner kb = new Scanner(System.in);
然后,每当你想从用户那里得到一个值时,就使用这个

您的输入循环对我来说似乎是错误的,我对
扫描仪不太熟悉,所以可能有更好的方法来实现这一点,但是

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

您不必为此使用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);

}

}

你的计算仍然是错误的……我不是在3小时前说过:P@MadProgrammer-“你的代码应该是这样的”-我没有说“这是你的代码”。我没有说代码是这样的,但我很确定我说过OP不需要使用不同的扫描仪,这一个就足够了;)