Lesson+10

The //switch// Statement and //char// The //switch// statement is useful when we have an integer variable that can be one of several quantities Ex:

//this code should be placed inside the main method of a class// //System.out.println("Make your arithmetic selection from the choices below;/n");// //System.out.println(' 1. Addition");// //System.out.println(' 2. Subtraction");// //System.out.println(' 3. Multiplication");// //System.out.println(' 4. Division/n");//

//Sytem.out.print(" Your choice? ")//

//Scanner kbReader = new Scanner(System.in);// //int choice = kbReader.nextInt;//

//Sytem.out.print("\nEnter first operand." );// //double op1 = kbReader.nextDouble;// //System.out.print("\nEnter second operadn." )// //double op2 - kbReader.nextDouble;//

//Sytem.out.println("");// //switch (choice)// //{// //case 1;// addition System.out.println(op1+" plus "+op2+" = "+(op1+op2)); break; case 2; //subtraction// //System.out.println(op1+" minus "+op2+" = "+(op1-op2));// //break;// //case 3;// multiplication System.out.println(op1+" ytimes "+op2+" = "+(op1*op2)); break; case 4; //division System.out.println(op1+" divided by "+op2+" = "+(op1 / op2)); break; default: System.out.println("Hey dummy, enter only a 1,2,3, or 4!");

          

 A new data type… //char //: Before we look further at the //switch // statement, we must look at a new data type, //char // .  This stands for character. Following is a typical way to declare and initialize a character: char ch = ‘h’; Notice that a character is always enclosed in single quotes. Characters can be anything, even numbers or symbols: char x = ‘6’; char pp = ‘@’; <span style="font-family: TimesNewRoman,BoldItalic;"> int <span style="font-family: TimesNewRoman,Bold;">and //<span style="font-family: TimesNewRoman,BoldItalic;">char //<span style="font-family: TimesNewRoman,Bold;">are permissible types: <span style="font-family: TimesNewRoman,Italic;"> switch statements primarily switch on **<span style="font-family: TimesNewRoman,Bold;">integers ** or **<span style="font-family: TimesNewRoman,Bold;">characters ** ( //<span style="font-family: TimesNewRoman,Italic;">short // and //<span style="font-family: TimesNewRoman,Italic;">byte // types can   also be used, but rarely are). Modify the example on the previous page to switch on a <span style="font-family: TimesNewRoman,Italic;"> char instead of //<span style="font-family: TimesNewRoman,Italic;">int // . See the next page for the necessary modifications: 10-3 System.out.println(“Make your arithmetic selection from the choices below:\n”); System.out.println(“ A. Addition”); System.out.println(“ S. Subtraction”); System.out.println(“ M. Multiplication”); System.out.println(“ D. Division\n”); System.out.print(“ Your choice? ”); Scanner kbReader = new Scanner(System.in); String choice = kbReader.nextLine; //char ch = choice; //You would think this would work…but it doesn’t. char ch = choice.charAt(0); //you just learned another String method. System.out.print(“\nEnter first operand. ” ); double op1 = kbReader.nextDouble; System.out.print(“\nEnter second operand .” ); double op2 = kbReader.nextDouble; System.out.println(“ ”); switch (ch) { case ‘A’: //addition case ‘a’: //Notice we are providing for both capital A and little a. System.out.println(op1 + “ plus ” + op2 + “ = ” + (op1 + op2) ); break; case ‘S’: //subtraction case ‘s’: System.out.println(op1 + “ minus ” + op2 + “ = ” + (op1 - op2) ); break; case ‘M’: //multiplication case ‘m’: System.out.println(op1 + “ times ” + op2 + “ = ” + (op1 * op2) ); break; case ‘D’: //division case ‘d’: System.out.println(op1 + “ divided by ” + op2 + “ = ” + (op1 / op2) ); break; default: System.out.println(“Hey dummy, enter only a A, S, M, or D!”);