简体   繁体   中英

Loops, new to programming

I created a simple tax bracket calculation for class, and now we need to add a loop so the user can calculate their tax as many times as they want. Im having difficulty understanding loops. Any constructive feedback would be appreciative.

/**
 * 
 * @author James
 * Assignment 3 Taxes Owned
 */

import java.util.Scanner;

public class TaxOwned {

public static void main(String[] args) {

    //Initializing input Scanner
    Scanner input = new Scanner(System.in);

    //Declare and initialize gender, age, martial status, income and name variables


    //Ask user to enter name
    System.out.println("Enter you first name:");
    String name = input.next();

    //Asks user to enter age
    System.out.println("Enter age in years:");
    Byte age = input.nextByte();

    // Asks user to enter gender
    System.out.println("Enter your gender(M/F):");
    char gender = input.next().charAt(0);

    //Ask user to enter martial status
    System.out.println("Enter your martial status(S/M/D/W):");
    char martial_status = input.next().charAt(0);

    // Ask user to enter taxable income 
    System.out.println("Enter your taxable income for 2016:");
    long income = input.nextLong();

    //Declaring variables to store name prefix and tax amount
    String name_prefix;
    double tax_amount;

    //Checking gender and age to assign correct prefix
    if (gender == 'M')
    {
        name_prefix = (age < 18) ? "Master." : "Mr.";   
    }
    else
    {
        name_prefix = (martial_status == 'M') ? "Mrs." : "Ms."; 
    }
// Calculating tax based on martial status and income
    switch (martial_status)
    {
    case 'M':
        if (income < 8500)
        {   
            tax_amount = 0;
            System.out.println(name_prefix + "" + name + ", based on the income provided, you owe no tax for the fiscal year 2016");
        }
        else
        {
            if (income < 24000)
            { 
                tax_amount= income * 0.01;
            }
            else
            {
                tax_amount = income * 0.025;
            }
    System.out.println(name_prefix + "" + name + ",based on the income provided, you owe a tax of $" + tax_amount + " for the fiscal year 2016");
        }
        break;
    case 'S':
        if (income < 8500)
        {
            tax_amount = 0;
            System.out.println(name_prefix + "" + name + ",based on the income provided, you owe no tax for the fiscal year 2016");
        }
        else
        {
            if(income < 24000)
            {
                tax_amount = income * 0.015;
            }
            else
            {
                tax_amount = income * 0.034;
            }
        System.out.println(name_prefix + " " + name + ",based on the income provided, you owe a tax of $" + tax_amount + " for the fiscal year 2016");
        }
        break;
    case 'D':
        if (income > 8500)
        {
            tax_amount = 0;
            System.out.println(name_prefix + " " + name + ", based on the income provided, you owe no tax for the fiscal year 2016");
        }
        else
        {
            if ( income < 2400)
            {
                tax_amount = income * 0.01;
            }
            else
            {
                tax_amount = income * 0.025;
            }
            System.out.println(name_prefix + " " + name + ",based on the income provided, you owe a tax of $" + tax_amount + " for the fiscalyear 2016 ");
        }
        break;
    case 'W':
        if (income < 8500)
        { tax_amount = income * 0.015;
        }
        else
        {
            tax_amount = income * 0.034;
            }
        System.out.println(name_prefix + " " + name + ", based on the income you provided, you owe a tax $" + tax_amount + " for the fiscal year 2016");


    break;
    default : System.out.println(" Sorry! Our system is unable to calculate your tax at this time.");
    }
    System.out.println("Thank you!");
    //closing all objects
    input.close();
}

do-while will give what you are trying to implement,

String retry="";
do{

// your whole code inside of main except `input.close()` and `Scanner input = new Scanner(System.in);` line

Sytem.out.println("Want to retry Y/N");
retry = input.nextLine();


}while(retry.charAt(0) == 'Y' || retry.charAt(0) == 'y');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM