有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java所得税计算器需要添加提示才能添加更多客户

我有一个java程序,可以计算用户的所得税,有一些帮助项目现在可以正常运行,但我应该添加一个选项,让用户在完成与客户的交易后,通过在提示符中输入“y”来处理其他客户。我需要提示:“处理另一个客户?(y/n)?”由于用户也可以点击“n”,因此此提示符还必须有一个用于“y”和“n”的命令。如何以及在何处执行此操作,以及需要将提示符放在代码中的何处?这是我的密码:

import java.util.Scanner;

public class TaxCalculator {
    public static void main(String[] args) {
        final double RATE1 = 0.20;
        final double RATE2 = 0.25;
        final double RATE3 = 0.10;
        final double RATE4 = 0.15;
        final double RATE5 = 0.30;
        final double RATE1_SINGLE_LIMIT = 0;
        final double RATE2_MARRIED_LIMIT = 0;
        final double RATE3_COHABITATING_LIMIT = 20000;
        final double RATE4_COHABITATING_LIMIT = 50000;
        double tax = 0;

        //Enter Income
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter your income: ");
        double income = in.nextDouble();

        System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
        String maritalStatus = in.next();

        //Calculate Taxes

        if (maritalStatus.equals("s") && income > RATE1_SINGLE_LIMIT) {
            tax = RATE1 * income;
        }else if (maritalStatus.equals("m") && income > RATE2_MARRIED_LIMIT) {
            tax = RATE2 * income;
        }else if (maritalStatus.equals("c") && income <= RATE3_COHABITATING_LIMIT) {
            tax = RATE3 * income;
        }else if (maritalStatus.equals("c") && income <= RATE4_COHABITATING_LIMIT) {
            tax = RATE4 * income;
        } else {
            tax= RATE5 * income;
        }

        System.out.print("Your tax is: " + tax);
    }
}

共 (2) 个答案

  1. # 1 楼答案

    利用do...while循环
    The while and do-while Statements | Java Documentation

    import java.util.Scanner;
    
    public class TaxCalculator {
        static void calculate() {
    
            final double RATE1 = 0.20;
            final double RATE2 = 0.25;
            final double RATE3 = 0.10;
            final double RATE4 = 0.15;
            final double RATE5 = 0.30;
            final double RATE1_SINGLE_LIMIT = 0;
            final double RATE2_MARRIED_LIMIT = 0;
            final double RATE3_COHABITATING_LIMIT = 20000;
            final double RATE4_COHABITATING_LIMIT = 50000;
            double tax = 0;
            Scanner in = new Scanner(System.in);
            //Enter Income
            System.out.print("Please enter your income: ");
            double income = in.nextDouble();
            in.nextLine();
    
            System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
            String maritalStatus = in.next();
            in.nextLine();
    
            //Calculate Taxes
    
            if (maritalStatus.equals("s") && income > RATE1_SINGLE_LIMIT) {
                tax = RATE1 * income;
            } else if (maritalStatus.equals("m") && income > RATE2_MARRIED_LIMIT) {
                tax = RATE2 * income;
            } else if (maritalStatus.equals("c") && income <= RATE3_COHABITATING_LIMIT) {
                tax = RATE3 * income;
            } else if (maritalStatus.equals("c") && income <= RATE4_COHABITATING_LIMIT) {
                tax = RATE4 * income;
            } else {
                tax = RATE5 * income;
            }
    
            System.out.print("Your tax is: " + tax);
    
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String newResponse = "";
            do {
                calculate();
                System.out.println();
                System.out.println("Process another response?. Please enter 'y' for yes, or 'n' for no: ");
                newResponse = in.next();
                in.nextLine();
            } while (newResponse.equals("y"));
    
        }
    }
    
  2. # 2 楼答案

    您可以在程序结束时执行此操作,在显示第一个用户的税务后,您可以询问是否有新用户,如果有,您将重复与第一个用户相同的步骤,如果没有,您的程序将完成。为此,您必须先添加一个布尔变量(或您想要的整数),以了解是否有新用户:

    boolean newCustomer = false;
    

    然后,您的整个代码将在do while循环中,如下所示:

    do{
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter your income: ");
    double income = in.nextDouble();
    
    System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
    String maritalStatus = in.next();
    
    //Calculate Taxes
    
    if (maritalStatus.equals("s") && income > RATE1_SINGLE_LIMIT) {
        tax = RATE1 * income;
    }else if (maritalStatus.equals("m") && income > RATE2_MARRIED_LIMIT) {
        tax = RATE2 * income;
    }else if (maritalStatus.equals("c") && income <= RATE3_COHABITATING_LIMIT) {
        tax = RATE3 * income;
    }else if (maritalStatus.equals("c") && income <= RATE4_COHABITATING_LIMIT) {
        tax = RATE4 * income;
    } else {
        tax= RATE5 * income;
    }
    
    System.out.println("Your tax is: " + tax);
    
        System.out.print("Please type 'y' for a new customer, or 'n' to finish the program : ");
        String customer = in.next();
        if(customer.equals("y"))
            newCustomer = true;
        else
            newCustomer = false;
    }
    while(newCustomer);