2012-02-26 79 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);

相关问题