2012-02-26 86 views
1

我有一個問題,輸出應該是雙重的,但它是字符串 我想添加兩個double值,但它將它作爲一個字符串。我正在使用eclipse。目前該程序正在編譯和運行。如果有人有片刻我會很感激。乾杯。這是源代碼。double + double = String?

import java.util.Scanner; 

public class FutureInvestment 
{ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Enter investment amount: "); 
     double investmentAmount = input.nextDouble(); 

     System.out.println("Enter monthly interest rate: "); 
     double monthlyInterestRate = input.nextDouble(); 

     System.out.println("Enter number of years: "); 
     int numberOfYears = input.nextInt(); 

     double futureInterestValue = investmentAmount * (Math.pow((1 + monthlyInterestRate), numberOfYears * 12)); 
     System.out.println("Accumulated value is: " + futureInterestValue + investmentAmount); 


    } 

} 
+0

這裏的輸出我得到進入投資金額:輸入月息: 4.25 輸入的年數:累計值是:4.384414858452464E111000.0 – Kiril 2012-02-26 15:55:01

+0

和預期結果的累計值1043.34 – Kiril 2012-02-26 15:55:19

+0

我想我的回答可以幫助你 – 2012-02-26 16:07:25

回答

2

您需要格式化你的輸出。您可以使用DecimalFormat或者你可以嘗試String#format功能:

System.out.println(
    String.format("Accumulated value is: %.2f", 
     futureInterestValue + investmentAmount)); 

這樣你就可以得到2位小數輸出。另外,我建議建立與結果的變量,所以你可以把你的代碼

double accumulatedValue = futureInterestValue + investmentAmount; 
System.out.println(
    String.format("Accumulated value is: %.2f", accumulatedValue); 
0

您缺少一些括號,所以您的語句從左到右執行,因此將double附加到字符串。你會需要這樣的東西:

的System.out.println( 「累計值是:」 +(futureInterestValue + investmentAmount));

1
double accumulatedValue = futureInterestValue + investmentAmount; 
System.out.println("Accumulated value is: " + accumulatedValue); 

試試這個。

由於連接到串的任何東西都被轉換爲字符串,因此您正在獲取串聯結果。因此,您需要事先按照上面所示完成該值,否則您需要使用括號。

+0

相同的輸出:累計值是:4.384414868452464E11 – Kiril 2012-02-26 16:05:17

+1

如果你指的是「E」內數字,這是一個雙重符號不是一個字符串 – 2012-02-26 16:07:50

1

我想改變這會工作:

double futureInterestValue = investmentAmount * (Math.pow((1 + monthlyInterestRate/100), numberOfYears * 12)); 
    System.out.println("Accumulated value is: " + (futureInterestValue + investmentAmount)); 
+0

恐怕吧是不工作我有以下輸出==>累計值是:4.384414868452464E11 – Kiril 2012-02-26 16:04:16

+0

我認爲你的計算是錯誤的,你需要首先分100利息,我已經更新了我的代碼 – 2012-02-26 16:11:43

0
System.out.println("Accumulated value is: " + (futureInterestValue + investmentAmount)); 

第一+後,Java已經串聯起來的第一雙的第一個字符串,導致串。然後再與第二個double進行連接。您需要先計算出結果,然後再從中取出一個字符串。

2

既然你正在做一個println,它正在做字符串連接。如果你想把這個double加在一起,你需要用()對它們進行分組。

嘗試

System.out.println("Accumulated value is: " + (futureInterestValue + investmentAmount)); 
0

的問題是你的號碼是越來越方式過大,並打印值時的Java切換到科學記數法。

如果您的每月利率輸入爲4.25(意思是4.25%),則必須將其轉換爲0.0425的正確十進制表示形式,然後再將其用於計算 - 您必須將其除以100。噸,所使用的利率將遠遠超出您的預期;在這種情況下爲425%。

換句話說,更改

double monthlyInterestRate = input.nextDouble(); 

double monthlyInterestRate = input.nextDouble()/100; 
0

你可以試試:

System.out.println("Accumulated value is: " + (futureInterestValue + investmentAmount)); 

或添加變量像double accumulatedValue=futureInterestValue + investmentAmount; 然後System.out.println("Accumulated value is: " + accumulatedValue);

相關問題