2017-02-09 3 views
0

Я создал простой расчет налоговой скобки для класса, и теперь нам нужно добавить цикл, чтобы пользователь мог рассчитать свой налог столько раз, сколько захотите. Я с трудом понимаю петли. Любая конструктивная обратная связь будет признательна.Петли, новые для программирования

/** 
* 
* @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(); 
} 
+0

Вы должны попробовать 'делать-while' петлю. –

+0

Что именно о циклах вызывает путаницу? – nitind

ответ

1

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'); 
Смежные вопросы