我将不胜感激任何建议,以帮助我弄清楚如何摆脱“ 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);
}

最佳答案

这正在被转换为字符串

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

09-05 01:02