Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package act;
- import java.util.Scanner;
- public class SwitchStatAct
- { //1
- //Write a program that uses a switch statement to display "one" if the user
- //has entered 1, "two" if the user has entered 2, and "three" if the user has
- //entered 3. If a number other than 1, 2, or 3 is entered, the program should display an error message. Ensure your program begins by prompting the user to enter 1, 2, or 3.
- public static void main(String[] args)
- {
- Scanner keyboard = new Scanner(System.in);
- int num;
- System.out.println("enter 1, 2, or 3:");
- num = keyboard.nextInt();
- switch (num)
- {
- case 1:
- System.out.println("one");
- break;
- case 2:
- System.out.println("two");
- break;
- case 3:
- System.out.println("three");
- break;
- default:
- System.out.println("you must enter 1, 2 or 3.");
- break;
- }
- keyboard.close();
- /*
- * #2
- Scanner keyboard = new Scanner(System.in);
- String inp;
- char in;
- System.out.println("enter A, B, or C:");
- inp = keyboard.nextInt();
- in = inp.charAt(0);
- switch (in)
- {
- case 'A':
- System.out.println("You selected A.");
- break;
- case 'B':
- System.out.println("You selected B.");
- break;
- case 'C':
- System.out.println("You selected C.");
- break;
- case 'D':
- System.out.println("You selected D.");
- break;
- default:
- System.out.println("Not good with letters, eh?");
- break;
- }
- ----------------------------------------------------
- #3
- you cannot convert this to a switch statement because cases must be exact answers not
- including > or < symbols
- #4
- same reason as above, cases cannot include < or > symbols must be exact numbers
- #5
- That is serious.
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement