2013-10-04 29 views
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); 

    } 
} 

任何援助將是非常非常有幫助!

回答

2

首先amount,interestyearsScanners,它們不是數字。 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)); 
0

您不必使用4臺不同的掃描儀對於這一點,因爲你是從同一個流閱讀... 。你的代碼應該是這樣的 -

import java.util.Scanner;

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

} 

}

+0

你的計算仍然是錯誤的......和我沒有說,2小時前:P – MadProgrammer

+0

@MadProgrammer - 「你的代碼應該是這樣的「 - 我沒有說」這是你的代碼「.. – TheLostMind

+1

我沒有說代碼是,但我很確定我說OP沒有必要使用不同的掃描儀,而且一個就足夠了;) – MadProgrammer