java - 循环,编程新手

标签 java eclipse

我为类(class)创建了一个简单的税级计算,现在我们需要添加一个循环,以便用户可以根据需要多次计算其税费。我很难理解循环。 任何建设性的反馈意见将不胜感激。

/**
 * 
 * @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 将给出您想要实现的内容,

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');

关于java - 循环,编程新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42139402/

相关文章:

Java 对象重用

java - org.springframework.web.servlet.PageNotFound noHandlerFound在eclipse中

java - JPA 中是否有某种方法可以根据其他值自动增加列

eclipse - 在Intellij IDEA中自动激活maven配置文件

java - SQLite数据库错误: table has no column name

java - 在 Java 8 中找不到 AnnotationProcessorFactory 类

java - 无法将 Scala 对象导入到 Java 项目中

java - Eclipse 系统属性

java - 我可以设置 Git 在每次修改某个文件时通知我吗

eclipse - Eclipse 中的 `SDK` 包