我的税务计算器程序已基本完成,但有一些问题

时间:2014-10-04 23:38:13

标签: java oop

我必须在计划执行所欠税款之前两次[输入]收入。它只使用一个税率(10%)来计算每个其他税率。我的以下代码如下:

package org.tax.tutorial;

import java.util.Scanner;

public class Taxability {

    //The main method of this program   
        public static void main(String[] args){

            int userInput = 0;
            double bracketOne = 0.10;
            double bracketTwo = 0.15;
            double bracketThree = 0.25;
            double bracketFour = 0.28;
            double bracketFive = 0.33;
            double bracketSix = 0.35;
            double total = 0.0;


            //Inform user about the program 
            System.out.println("This program is designed to calcualte your annual income and determine how much taxes you currently owe." + "\n");

            //Inform user about tax brackets
            System.out.print("Based on your income you may be classifed under one of the six brackets:" + "\n" + "\n" + "Bracket 1: 0–$8,500 10%" + "\n" + "Bracket 2: $34,500 15%" + "\n" + "Bracket 3: $34,500–$83,600 25%" + "\n" +  "Bracket 4: $83,600–$174,400 28%" + "\n" + "Bracket 5: $174,400–$379,150 33%" + "\n" + "Bracket 6: $379,150 above 35%" + "\n" + "\n");


            {
                //User inputs their annual income
                System.out.println("Please submit your annual income for the following year 2013-2014:");

                //Create a Scanner object for keyboard input
                @SuppressWarnings("resource")
                Scanner keyboard = new Scanner(System.in);
                userInput = keyboard.nextInt();

                if ((userInput >= 0) & (userInput<= 8500 ));{
                total = bracketOne * keyboard.nextDouble();
                System.out.println("Total tax owed is $" + total + "\n" + "\n" + "Thank your for using the tax calculator.");
                }
                if ((userInput >=8500) & (userInput <= 34500));{
                total = bracketTwo * keyboard.nextDouble();
                System.out.println("Total tax owed is $" + total + "\n" + "\n" + "Thank your for using the tax calculator.");

                }
                if ((userInput >=34500) & (userInput <= 83600));{
                    total = bracketThree * keyboard.nextDouble();
                    System.out.println("Total tax owed is $" + total + "\n" + "\n" + "Thank your for using the tax calculator.");
                    System.exit(0);
                    }
                if ((userInput >=83600) & (userInput <= 174400));{
                    total = bracketFour * keyboard.nextDouble();
                    System.out.println("Total tax owed is $" + total + "\n" + "\n" + "Thank your for using the tax calculator.");

                }
                if ((userInput >=174400) & (userInput <= 379150));{
                    total = bracketFive * keyboard.nextDouble();
                    System.out.println("Total tax owed is $" + total + "\n" + "\n" + "Thank your for using the tax calculator.");

                }
                if (userInput >=379150);{
                    total = bracketSix * keyboard.nextDouble();
                    System.out.println("Total tax owed is $" + total + "\n" + "\n" + "Thank your for using the tax calculator.");
                    System.exit(0);
                }

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

你有一些问题。

(1)您正在使用userInput = keyboard.nextInt();keyboard.nextDouble()再次阅读输入。我想你想重新使用第一次调用的结果,而不是需要更多的输入。因此,将keyboard.nextDouble()的每次出现更改为userInput,这是您存储初始输入的变量。

(2)每个;语句后面都有一些额外的if个字符 - ){之间的字符。删除它们。

(3)舍入错误的问题。如果您需要确切的答案,最好使用BigDecimal而非double进行财务计算。我不确定你是否在课程中学到了BigDecimal

(4)在多个if语句中出现了一些值。您需要更加注意<<=之间以及>>=之间的差异。

(5)在这种情况下不会有任何区别,但是当你的意思是“和”时,通常最好写&&而不是&。这意味着在左侧出现虚假的情况下,计算机不会费心计算右侧。