Java Reference
In-Depth Information
break;
case "Investment":
interestRate = 0.059;
break;
default:
interestRate = 0;
Place these statements between the { } of the switch statement.
8. Next, add a print statement to show the outcome of the switch cases. Use the following statement:
System.out.println (loanType + " loans have an annual interest rate of "
+ interestRate*100 + "%.");
This time, make sure it is placed after the closing bracket (}) of the switch statement, but before
the closing bracket (}) of the main method.
9. Your class body should now look like this:
public class SwitchClass {
public static void main(String[] args) {
String loanType = "Commercial";
double interestRate;
switch (loanType) {
case "Residential":
interestRate = 0.055;
break;
case "Commercial":
interestRate = 0.062;
break;
case "Investment":
interestRate = 0.059;
break;
default:
interestRate = 0;
}
System.out.println(loanType + " loans have an annual interest rate of "
+ interestRate * 100 + "%.");
}
}
10. Save the class by clicking the disk icon or selecting File, then Save.
11. Run the application by clicking the green play icon or selecting Run, and then Run.
How It Works
Now take a look at how it works.
1. The application begins by executing the main method, which in this case is the only method.
2. The first statement initializes a String , named loanType , with the value of "Commercial" .
Search WWH ::




Custom Search