计算总薪酬,储蓄和投资

时间:2017-09-05 05:29:16

标签: java

我很感激任何建议,以帮助我弄清楚如何摆脱“IRA投资金额”和“储蓄和IRA金额总和”的两组小数。

我想要达到的预期输出是:

Enter the gross pay: 
Enter the savings rate %: 
Enter the IRA rate %: 
Gross pay: 1000.0
Savings rate %: 10.0
Savings amount: 100.0
IRA rate %: 5.0
IRA investment amount: 50.0
Total of savings and IRA amounts: 150.0

我一直得到这个输出:

Enter the gross pay: 
Enter the savings rate %: 
Enter the IRA rate %: 
Gross pay:4 $1000.0
Savings rate %: 10.0
Savings amount: 100.0
IRA rate %: 5.0
IRA investment amount: 100.050.0              // See here
Total of savings and IRA amounts: 100.050.0   // See here

这是我到目前为止所写的内容。

import java.util.Scanner;

public class Main_02 {

public static void main (String[]args) {

    Scanner console = new Scanner(System.in);
    double grossPay = 0.0;        // First number to average
    double savingsRate = 0.0;     // Second number to average
    double iraRate = 0.0;         // Average of the input values
    double savingAmt = 0.0;
    double iraAmt = 0.0;
    double totalSavings = 0.0;


    // Input the two numbers
    System.out.print("Enter the gross pay: ");
    grossPay = console.nextDouble();

    System.out.print("Enter the savings rate %: ");
    savingsRate = console.nextDouble();

    System.out.print("Enter the IRA rate %: ");
    iraRate= console.nextDouble();

    // Calculate the average of the two numbers

    savingAmt = (grossPay * savingsRate) / 100.0;

    iraAmt = (grossPay * iraRate) / 100.0;

    totalSavings = math.ceil(savingAmt + iraAmt);

    // Output the results
    System.out.println("Gross pay:4 $" + grossPay);
    System.out.println("Savings rate %: " + savingsRate);
    System.out.println("Savings amount: " + savingAmt);
    System.out.println("IRA rate %: " + iraRate);
    System.out.println("IRA investment amount: " + savingAmt + iraAmt);
    System.out.println("Total of savings and IRA amounts: " + savingAmt + iraAmt);
}

2 个答案:

答案 0 :(得分:2)

这正在转换为String

System.out.println("IRA investment amount: " + savingAmt + iraAmt);

所以改为

System.out.println("IRA investment amount: " + (savingAmt + iraAmt));

我还建议您将System.printf用作最后一行

System.out.printf ("Total of savings %f and IRA amounts %f %n", savingAmt , iraAmt);

如果您只想打印iraAmt(50.0),请不要向其添加savingAmt

System.out.println("IRA investment amount: " + (iraAmt));

答案 1 :(得分:0)

当您使用savingAmt + iraAmt与字符串" "连接时,您最终会添加两个字符串。

Savings amount: 100.0 
IRA rate %: 5.0

i.e. 100.050.0

根据预期输出,您可能想要更改

System.out.println("IRA investment amount: " + savingAmt + iraAmt);
System.out.println("Total of savings and IRA amounts: " + savingAmt + iraAmt);

System.out.println("IRA investment amount: " + iraAmt);
System.out.println("Total of savings and IRA amounts: "  + (savingAmt + iraAmt));