jeudi 18 novembre 2021

(java code) can you guys help me out on this?

I'm having errors "The local variable tax may not have been initialized" on lines-39-40-41 (I marked). The point is, 'income' is known so, doesn't it mean 'tax' will be initialized automatically??

import java.util.Scanner;


public static void main(String[] args) {
    // TODO Auto-generated method stub

    // Name: Ferhat Sirkeci  Student ID: 150120067
    
    // The purpose of this program is calculating income tax rates in Turkey according to year and income.
 
    Scanner input = new Scanner(System.in);
       
    int year = input.nextInt();
    double income = input.nextDouble();
    double tax;
       
    while (year<2017 || year>2020 || income<=0)  {
        if (year<2017 || year>2020) {
            System.out.println("Undefined year value!");
            break;
        }
           
        else if (income<=0)  {
           System.out.println("Income must be > 0");
           break;
        }
    } 
      
    // while (year>=2017 && year<=2020 && income>0)  {
    switch (year)  {
    case 2017 :
        if (income < 13000)    {
            tax = (income * 0.15);   
        }   else if (income < 30000 && income >= 13000)   {
            tax = (1950 + (income - 13000) * 0.2);    
        }   else if (income < 110000 && income >=30000)   {
            tax = (5350 + (income - 30000) * 0.27);
        }   else if (income >= 110000)   {
            tax = (26950 + (income - 110000) * 0.35);
        }   
        double iat = income - tax;         <------------ THIS LINE.
        double rtr = 100*tax/income;       <------------ THIS LINE.
        System.out.println("Income: " + income);
        System.out.println("Tax amount: " + tax);        <------------- THIS LINE.
        System.out.println("Income after tax: " + iat);
        System.out.println("Real tax rate: %" + rtr);
        break;

Aucun commentaire:

Enregistrer un commentaire