简体   繁体   English

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

[英]How do i calculate a whole bunch of different scanner inputs?

I'm not quiet sure how to go about doing the calculations for this. 我不确定要如何进行计算。 I have everything down right up until i'm trying to actually get the investment amount. 在我试图实际获得投资金额之前,我一直都准备就绪。 I know what i have right now is wrong, but google isn't being super helpful for me, and my book just does not have what i need in order to complete this darn thing. 我知道我现在所拥有的是错误的,但是google并不是对我有超级帮助,而我的书根本没有我所需要的东西来完成这令人毛骨悚然的事情。

Here what i have right now 这是我现在所拥有的

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

    }
}

Any assistance would be very very helpful! 任何帮助都将非常非常有帮助!

First of all amount , interest and years are Scanners , they are not numbers. 首先, amountinterestyearsScanners ,而不是数字。 The Scanner could contain any number of different types of content so it's impossible for something like Math.pow to know what it should do with them Scanner可能包含许多不同类型的内容,因此Math.pow之类的东西不可能知道应该如何处理

You need to assign the values you read from the user to some variables. 您需要将从用户读取的值分配给一些变量。

I'd start by using a single Scanner , say called kb ... 我将从使用单个Scanner ,比如说kb ...

Scanner kb = new Scanner(System.in);

Then use this whenever you want to get a value from the user... 然后,只要您想从用户那里获取价值,就可以使用它...

You input loops seem wrong to me, I'm not particularly experienced with the Scanner , so there is probably a better way to achieve this, but... 您输入的循环对我来说似乎是错误的,我对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);

Once you have all the information you need, make your calculations with these primitive types... 获得所有需要的信息后,使用这些原始类型进行计算...

double future = invest * Math.pow((1 + rate), (period * 12));

You don't have to use 4 different scanners for this because you are reading from the same stream.... Your code should be something like this - 您不必为此使用4个不同的扫描仪,因为您是从同一流中读取...。您的代码应该是这样的-

import java.util.Scanner; 导入java.util.Scanner;

class InvestmentCalculator { 类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);

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM