Java Reference
In-Depth Information
Display 3.1
Tax Program
1
import
java.util.Scanner;
2
public class
IncomeTax
3 {
4
public static void
main(String[] args)
5 {
6 Scanner keyboard =
new
Scanner(System.in);
7
double
netIncome, tax, fivePercentTax, tenPercentTax;
8 System.out.println("Enter net income.\n"
9 + "Do not include a dollar sign or any commas.");
10 netIncome = keyboard.nextDouble( );
11
if
(netIncome <= 15000)
12 tax = 0;
13
else if
((netIncome > 15000) && (netIncome <= 30000))
14 //
tax = 5% of amount over $15,000
15 tax = (0.05*(netIncome - 15000));
16
else
//netIncome > $30,000
17 {
18 //
fivePercentTax = 5% of income from $15,000 to $30,000
.
19 fivePercentTax = 0.05*15000;
20 //
tenPercentTax = 10% of income over $30,000
.
21 tenPercentTax = 0.10*(netIncome - 30000);
22 tax = (fivePercentTax + tenPercentTax);
23 }
24 System.out.printf("Tax due = $%.2f", tax);
25 }
26 }
27
Sample Dialogue
Enter net income.
Do not include a dollar sign or any commas.
40000
Tax due = $1750.00
The
switch
Statement
The
switch
statement
is the only other kind of Java statement that implements
multiway branches. The syntax for a
switch
statement and a simple example are
shown in the box entitled “The
switch
Statement.”
switch
statement