Java将两个双倍乘以另一个双倍

时间:2013-02-04 03:08:40

标签: java double

我正在尝试将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)));




    }
}

1 个答案:

答案 0 :(得分:7)

在Java中 - 以及许多其他语言 - 正在进行

result = operand1 operator operand2

立即计算结果 。 Java不会将结果中的程序行保留在内存中,只要 hoursWorked payRate 就会更新 grossPay 被修改,但只有 double 结果。

所以当你

    double hoursWorked =0.00;
    double payRate =0.00;
    double grossPay= (payRate * hoursWorked);

grossPay 0 * 0的结果(即零)。

你只需要移动 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);

让它发挥作用。