Java 将两个双精度数乘以另一个双精度数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14679858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java multiplying two doubles into another double
提问by montenegroxnj
I'm trying to replace payRate * hoursWorked
into a single variable grossPay
but when I do it's returning a value of 0.0 for any calculation. double grossPay
is commented out on purpose so you can see what I was trying to do.
我正在尝试替换payRate * hoursWorked
为单个变量,grossPay
但是当我这样做时,它会为任何计算返回 0.0 的值。double grossPay
是故意注释掉的,所以你可以看到我在做什么。
import java.text.*;
import java.util.Scanner;
public class elseIfPayroll{
public static void main(String[] args){
double hoursWorked =0.00;
double payRate =0.00;
int dependents =0;
Scanner keyboard = new Scanner(System.in);
NumberFormat f = new DecimalFormat("#0.00");
System.out.print("Enter pay rate: ");
payRate = keyboard.nextInt();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
System.out.print("Enter amount of dependents: ");
dependents = keyboard.nextInt();
double grossPay= (payRate * hoursWorked);
//gross pay decision statements
if (hoursWorked<=40) {
System.out.println("Total $ Earned: " + payRate * hoursWorked);
}
else if (hoursWorked >= 40) {
System.out.println("Total $ Earned: " + payRate * hoursWorked +(payRate * 0.5));
}
else if (hoursWorked>=60){
System.out.println("Total $ Earned: " + payRate * hoursWorked + (payRate * 2));
}
//else if statements for dependents
if (dependents == 0){
f.format(payRate * hoursWorked);
}
else if (dependents == 1){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.04) );
}
else if (dependents == 2){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.0775));
}
else if (dependents == 3){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.1125) );
}
else if (dependents == 4){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.145) );
}
else if (dependents == 5){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.175) );
}
else if (dependents >= 6){
f.format(payRate * hoursWorked - (payRate * hoursWorked*0.2025) );
}
System.out.println("New gross Pay: " + f.format(payRate * hoursWorked - (payRate * hoursWorked*0.04)));
}
}
回答by e2-e4
In Java - and many other languages - doing
在 Java - 和许多其他语言 - 做
result = operand1 operator operand2
calculates the result immediately. Java doesn't keep in memory the program line in resultthat would update grossPayas soon as hoursWorkedor payRateare modified, but only the doubleresult.
立即计算结果。Java 不会将程序行结果保留在内存中,该结果会在hoursWorked或payRate被修改后立即更新grossPay,而只会保留双重结果。
So when you
所以当你
double hoursWorked =0.00;
double payRate =0.00;
double grossPay= (payRate * hoursWorked);
grossPaytakes the result of 0 * 0
(ie zero).
grossPay取0 * 0
(即零)的结果。
You just have to move a bit the grossPaycalculation
你只需要稍微移动一下grossPay计算
System.out.print("Enter pay rate: ");
payRate = keyboard.nextInt();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
System.out.print("Enter amount of dependents: ");
dependents = keyboard.nextInt();
double grossPay= (payRate * hoursWorked);
to make it to work.
使其工作。