Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
  
  
Lab 2B: Switch Statements 
 
Introduction: 
 
Nested if-else statements can be very slow if the number of conditions becomes large. For this 
reason, Java invented the "switch statement" to quickly jump to the desired code to execute 
based on the value of a single integer or character variable. Floating point variables and strings 
will not work. The syntax of the switch statement is shown below: 
 
switch (variable) 
{ 
   case value1: 
      code to be executed if variable == value1 
      break;   
   case value2: 
      code to be executed if variable == value2 
      break;  
   case value3: 
      code to be executed if variable == value3 
      break;  
   default: 
      code to be executed if no match found 
      break;  
} 
 
First we have a variable whose value we want to use to select different actions. The contents of 
this variable is then compared with the values for each case in the switch statement. If there is a 
match, the block of code associated with that case is executed. We use the "break" statement to 
jump to the end of the switch statement. If you omit the "break" the program will start executing 
the code in the following case, which is seldom what you want. 
 
Instructions: 
 
Consider the Java program below. It reads an integer value between [1..13] from the user and 
prints out the corresponding playing card name where 1=Ace, 2=Two, ... 10=Ten, 11=Jack, 
12=Queen, and 13=King. 
 
  
import java.util.Scanner; 
public class Main 
{ 
    public static void main(String[] args) 
    { 
        Scanner scnr = new Scanner(System.in); 
         
        // Read card number 
        System.out.print( 
   "Enter card number between [1..13] for [Ace..King]: "); 
        int day = scnr.nextInt(); 
         
        // Print name of card 
        if (day == 1) 
            System.out.println("Ace"); 
   else if (day == 2) 
            System.out.println("Two"); 
   else if (day == 3) 
            System.out.println("Three");  
        else if (day == 4) 
            System.out.println("Four");  
        else if (day == 5) 
            System.out.println("Five");  
        else if (day == 6) 
            System.out.println("Six");  
        else if (day == 7) 
            System.out.println("Seven");  
        else if (day == 8) 
            System.out.println("Eight");  
        else if (day == 9) 
            System.out.println("Nine");  
        else if (day == 10) 
            System.out.println("Ten");  
        else if (day == 11) 
            System.out.println("Jack");  
        else if (day == 12) 
            System.out.println("Queen");  
        else if (day == 13) 
            System.out.println("King");  
        else 
            System.out.println("Error"); 
    } 
} 
 
  
Step 1: Your task is to use a switch statement to implement a new version of this program. Start 
by creating a program called "Card.java" and cut and paste the code above into this file. 
 
Step 2: Compile and run the program. Test it with a number of input values between [1..13] to 
verify that it is working correctly. What happens if the user types in a value outside this range? 
 
Step 3: Make a copy of your day.cpp program called "Card2.java". Now replace the nested if-
else code with the corresponding switch statement. Remember to put the "break" statements at 
the bottom of each switch "case". 
 
Step 4: Recompile and debug your program until it is working correctly for the values [1..13] 
and for input values outside this range. Remember to use the switch "default" at the bottom of 
the switch statement to handle the error cases. 
 
Step 5: Once you think your program is working correctly, upload your final program and a 
copy of your program output into Blackboard for grading.