2014-10-04 7 views
-2

프로그램에 세금이 부과되기 전에 소득을 두 번 입력해야 함 & 다른 세금 브래킷을 계산할 때 하나의 세금 브래킷 (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); 
       } 

      } 
     } 
    } 
+2

이 사이트는 질문 할 때 더 잘 작동합니다. –

+0

0-379,150의 숫자 중에서 무작위로 선택하십시오. 이 프로그램은 첫 번째 세금 괄호 (10 %)에 대해 정확하게 계산을 수행하지만 8,500 이후의 항목은 빚진 세금에 대해 동일한 비율을 사용합니다. – brittneyjava

+0

그리고 프로그램이 계산을 실행하기 전에 숫자를 두 번 입력해야합니다. – brittneyjava

답변

0

몇 가지 문제점이 있습니다.

(1) 입력을 userInput = keyboard.nextInt();으로 다시 한 번 입력하고 keyboard.nextDouble()으로 다시 입력 중입니다. 나는 당신이 더 많은 입력을 요구하는 대신 첫 번째 호출에서 결과를 재사용하고 싶다고 생각한다. 따라서 keyboard.nextDouble()이 발생할 때마다 초기 입력을 저장 한 변수 인 userInput으로 변경하십시오. ){ 사이의 사람 -

(2) 각 if 문 후 몇 가지 추가 ; 문자가 있습니다. 그들을 제거하십시오.

(3) 반올림 오류의 문제점. 정답을 원한다면 재무 계산을 위해 double 대신 BigDecimal을 사용하는 것이 좋습니다. 귀하가 코스에서 BigDecimal에 대해 배웠는지 확실하지 않습니다.

(4) if 문 중 하나 이상에 몇 가지 값이 있습니다. <<= 사이, 그리고 >>= 사이의 차이에 대해 좀 더주의해야합니다.

(5)이 경우 아무런 차이가 없지만 일반적으로 "AND"를 의미하면 & 대신 &&을 쓰는 것이 좋습니다. 왼손이 틀린 경우 컴퓨터가 오른손을 계산하지 않아도된다는 뜻입니다.

+0

귀하의 성명을 이해하려고합니다. keyboard.nextDouble()을 keyboard.userInput()으로 전환해야합니까? – brittneyjava

+0

아니요.'keyboard.nextDouble()'을 작성한 시점에서 이미 입력을 얻었습니다. 그러므로'keyboard'에 대해서는 잊어 버리고'userInput'을 작성하십시오. –

+0

설명해 주셔서 감사합니다. – brittneyjava

관련 문제