mardi 18 septembre 2018

If statement defaulting incorrectly

For a class I have to write a program using a large if statement to calculate a person's taxes for the year based on their income and marital status. For some reason that I can't find, it keeps defaulting to the married selection in my if statement even if I put in single. I'm new to Java so any help figuring out why this isn't working would be appreciated.

import java.util.Scanner;
public class CalculateTax {
public static void main(String[] args) {

final int S2 = 800;
final int S3 = 4400;
final int M2 = 1600;
final int M3 = 8800;
double userInput, incomeOver, taxed, printTaxed;
String bracket, single = "single", married = "married";

Scanner console = new Scanner(System.in);       

System.out.println("Please enter single or married: ");

bracket = console.next();

System.out.println("Please enter your total income for the year: ");

userInput = console.nextDouble();

if (bracket.equalsIgnoreCase(single)) {
    if ((userInput >=0) && (userInput < 8000)) {
        taxed = userInput * 0.1;
        System.out.println("You owe " + taxed + " in taxes for the year 
based on your " + userInput + " income.");
    } else if ((userInput >=8000) && (userInput < 32000)) {
        incomeOver = userInput - 8000;
        taxed = incomeOver * 0.15;
        printTaxed = S2 + taxed;
        System.out.println("You owe " + printTaxed + " in taxes for the 
year based on your " + userInput + " income.");
    } else if (userInput >= 32000) {
        incomeOver = userInput - 32000;
        taxed = incomeOver * 0.25;
        printTaxed = taxed + S3;
        System.out.println("You owe " + printTaxed + " in taxes for the 
year based on your " + userInput + " income.");
    } else {
        System.out.println("Income Error");
    }
} else if (bracket.equalsIgnoreCase(married)) {
    if ((userInput >=0) && (userInput < 16000)) {
        taxed = userInput * 0.1;
        System.out.println("You owe " + taxed + " in taxes for the year 
based on your " + userInput + " income.");
    } else if ((userInput >=16000) && (userInput < 64000)) {
        incomeOver = userInput - 16000;
        taxed = incomeOver * 0.15;
        printTaxed = M2 + taxed;
        System.out.println("You owe " + printTaxed + " in taxes for the 
year based on your " + userInput + " income.");
    } else if (userInput >= 64000) {
        incomeOver = userInput - 64000;
        taxed = incomeOver * 0.25;
        printTaxed = taxed + M3;
        System.out.println("You owe " + printTaxed + " in taxes for the 
year based on your " + userInput + " income.");
        } else {
            System.out.println("Income Error");
    }
    } else {
        System.out.println("Error. Must be single or married");
    }

    console.close();
    }//end main
}//end class

Aucun commentaire:

Enregistrer un commentaire