Question:
java programming SWITCH statement?
anonymous
2013-12-27 10:21:00 UTC
Write a program which will print a message on the screen welcoming the user to the program, and which will then ask him or her to choose one of three different letters:A, B, or C; and to type in his or her first name and age. The program should then print out the information which has been entered.Modify your program so that after the sentence ‘You have made choice..’,the user gets an
additional message, depending on what letter is typed in.For instance,if they
choose ‘A’, your message might be:‘Your prize is a pet aardvark!’ If they choose ‘B’, your message might be ‘Your prize is a pet bear!’and similarly for ‘C’ Modify so that the user can type in any letter from ‘A’ to ‘F’,For this program, you may wish to use the SWITCH statement.
A sample run might look like this: (User input in bold.)
Welcome to the Interrogator.
Choose one of the following letters: A,B, or C: B
Enter your first name: Amir
Enter your age:23

My answer: import java.util.Scanner;

public class Interrogator {
public static void main(String[] args) {
System.out.println("Welcome to Interrogator");
String option = read("Choose one of the following letters: A, B or C: ", "^(?i)[a-c]$");
String name = read("Enter your first name: ", null);
int age = Integer.parseInt(read("Enter your age: ", "^[0-9]{1,3}$"));

System.out.println("Thank you!");
print(option, name, age);
printMessageBy(option);
System.exit(0);
}

private static String read(String message, String constraint) {
boolean done = false;
String result = "";
do {
System.out.print(message);
Scanner keyboard = new Scanner(System.in);

result = keyboard.nextLine();
if (constraint == null || result.matches(constraint)) {
done = true;
} else {
System.out.println("Error: invalid value was entered.");
}

} while (!done);
return result;
}

private static void print(String option, String name, int age) {
System.out.printf("Your name is %s.%n", name);
System.out.printf("You are %d years old.%n", age);
System.out.printf("You have made choice %s.%n", option);
}

private static void printMessageBy(String option) {
if ("A".equalsIgnoreCase(option)) {
System.out.println("Your prize is a pet aardvark!");
} else if ("B".equalsIgnoreCase(option)) {
System.out.println("Your prize is a pet bear!");
} else if ("C".equalsIgnoreCase(option)) {
System.out.println("Your prize is a C!");
}
}
}
Three answers:
Sreehari
2013-12-27 11:16:23 UTC
import java.util.Scanner;

public class Interrogator {



public static void main(String... args){

System.out.println("Welcome to Interrogator");

System.out.println("Choose one of the following letters: A,B, or C: ");

Scanner scanner = new Scanner(System.in);

char option = scanner.next().toUpperCase().charAt(0);

System.out.println("Enter your first Name:");

String name = scanner.next();

System.out.println("Enter your age: ");

int age = scanner.nextInt();



System.out.printf("Your name is %s.%n", name);

System.out.printf("You are %d years old.%n", age);

System.out.printf("You have made choice %s.%n", option);



switch(option){

case 'A':

System.out.println("Your prize is a pet aardvark!");

break;

case 'B':

System.out.println("Your prize is a pet bear!");

break;

case 'C':

System.out.println("Your prize is a C!");

break;



default:

System.out.println("Invalid option");



}

}



}
brilliant_moves
2013-12-27 20:01:26 UTC
option = option.toUpperCase();

switch (option) {

case "A": System.out.println("Your prize is a pet aardvark!"); break;

case "B": System.out.println("Your prize is a pet bear!"); break;

case "C": System.out.println("Your prize is a pet chimp!"); break;

case "D": System.out.println("Your prize is a pet deer!"); break;

case "E": System.out.println("Your prize is a pet elephant"); break;

case "F": System.out.println("Your prize is a pet frog!"); break;

default: System.out.println("No prize for incorrect answer!");

} // end switch
?
2013-12-27 18:35:05 UTC
#include



int main ()

{

Scanner in = new Scanner(System.in);



System.out.println("Enter a char");

char = in.nextLine();

char grade =



switch(grade)

{

case 'A' :

System.out.println("What Ever u want");

break;

case 'B' :

System.out.println("What Ever u want");

break

case 'C' :

System.out.println("What Ever u want");

break;

case 'D' :



System.out.println("What Ever u want");

break;

case 'F' :

System.out.println("What Ever u want");

break;

default :

System.out.println("What Ever u want");

}

System.out.println("What Ever u want");

return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...