gpt4 book ai didi

java - 计算总工资、储蓄和投资

转载 作者:行者123 更新时间:2023-12-02 02:37:37 25 4
gpt4 key购买 nike

如果有任何建议帮助我弄清楚如何去掉“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));

关于java - 计算总工资、储蓄和投资,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46047560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com