mardi 18 juillet 2017

Pulling Var Data Out Of A "If" [duplicate]

This question already has an answer here:

I'm new to programming and need help pulling data out of an if var. This is small section of my program that I'm having issues with. This program is used to calculate after tax income using the data provided by a user.

I'm able to pull this off but only if the var is declared right above the if statement. If I declare the var at the top and try to pull the data towards the bottom it does not work. It states var might not have been initialized.

This is where my problem is. combTotal += weekStTotal37k;

package test; 
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class Test {

public static void main(String[] args) {
    double combTotal;
    double grossSalIncomeDou = 10000;
    double weekIncomeDou9k;
    double weekIncomeDou80k;
    double weekStTotal37k;

    DecimalFormat df = new DecimalFormat ("#,###.00");

        if (grossSalIncomeDou > 9325 && grossSalIncomeDou <= 37950) {
            weekIncomeDou9k = 9325 * .90;
            weekIncomeDou9k /= 52;

            weekIncomeDou80k = grossSalIncomeDou - 9325;
            weekIncomeDou80k *= .85;
            weekIncomeDou80k /= 52;

            combTotal = weekIncomeDou9k + weekIncomeDou80k;
            JOptionPane.showMessageDialog(null, "Weekly income after taxes " + df.format (combTotal));      

        } 

        //If used to calculate income after state tax.
        if (grossSalIncomeDou <= 37110) {
            weekStTotal37k = grossSalIncomeDou * .9435;
            weekStTotal37k /= 52;

        }
            combTotal += weekStTotal37k;
            JOptionPane.showMessageDialog(null, "Weekly income after state and fed taxes " + df.format (combTotal));
}  
}

Aucun commentaire:

Enregistrer un commentaire