Using switch-case statement write a code that takes a number from user and print
ID: 3792570 • Letter: U
Question
Using switch-case statement write a code that takes a number from user and prints out the following Write a code that takes the size of their foot from user and prints out suggested slipper size.~ 6 (inclusive) Small Above 6~8 (inclusive) Medium Above 8 Large number. Write a code that takes 2 real numbers in one shot and print smaller number. (If they are the same, you can print any of the two.) Write a code that takes two person's genders from a user. (Assume user will only input either M or F where M is for male and F is for female.) If both are male, then output "They can be roommates in Smith Hall" Otherwise if both are female, them output "They can be roommates in Simmons Hall". For all other cases output "They cannot be roommates."Explanation / Answer
//Program 8
import java.util.Scanner;
public class Conditionals {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number:");
int num = input.nextInt(); // Reading vakueus using Scanner object with nextInt()
switch(num){ // Switch cases for 1, 2 3, and others
case 1: System.out.println("Double Double burger");
break;
case 2: System.out.println("Cheese Burger");
break;
case 3: System.out.println("Hambugger");
break;
default: System.out.println("Invalid Choice");
break;
}
}
}
Output:
Enter the number:
3
Hambugger
Enter the number:
2
Cheese Burger
Enter the number:
5
Invalid Choice
//Program 9
import java.util.Scanner;
public class Foot {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the Foot Size:");
int size = input.nextInt(); //nextInt() method fro integer
if(size>=0 && size<6){ //Condition for size of foot wear
System.out.println("Small");
} else if(size>=6 && size<=8){
System.out.println("Medium");
} else{
System.out.println("Large");
}
}
}
Output:
Enter the Foot Size:
4
Small
Enter the Foot Size:
7
Medium
Enter the Foot Size:
9
Large
//Program 10
import java.util.Scanner;
public class SmallReal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the two real number:");
double num1 = input.nextDouble(); // Reading double real values
double num2 = input.nextDouble();
if(num1>num2){
System.out.println("Smallest is:"+num2);
} else if(num1==num2){
System.out.println("Values are Same:"+num1);
} else {
System.out.println("Smallest is:"+num1);
}
}
}
Output
Enter the two real number:
5
9.8
Smallest is:5.0
Enter the two real number:
5.6
5.6
Values are Same:5.6
//Program 11
import java.util.Scanner;
public class Genders {
public static void main(String[] args) {
char mate1,mate2;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Genders:");
mate1 = input.next().charAt(0); // First character in String is Character i.e Male and Female with M and F
mate2 = input.next().charAt(0);
if(mate1=='M' && mate2=='M'){
System.out.println("They can be roommates in Smithhall");
} else if(mate1=='F' && mate2=='F'){
System.out.println("They can be roommates in Simmons hall");
} else {
System.out.println("They cannot be roommates");
}
}
}
Output
Enter the Genders:
M
M
They can be roommates in Smithhall
Enter the Genders:
F
F
They can be roommates in Simmons hall
Enter the Genders:
M
F
They cannot be roommates
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.