In: Computer Science
2)
Write a program(C#) that prompts the user for her home planet. Based on the
user's input the program will display the following:
Input: earth
Message: earth. You are an Earthling and you have 10 fingers
Input: VENUS
Message: VENUS. You are a Venusian and you have 12 fingers
Input: Mars
Message: Mars. You are a Martian and you have 8 fingers
any other input
Message: I am sorry I don't know of that planet
You may use either the ToUpper() or ToLower() methods
You MUST USE ONLY A SWITCH statement to solve this problem
[For full marks you need to accept all permutations of earth, venus
and mars]
Just take a look at the code, i have written code for your full marks, that means this code will recognize all the permutations of EARTH, MARS, VENUS
i have used hashset , for storing 3 planets (for faster LOOKUP)
take a look at IDE code with proper indentation
Now take a look at all types of inputs
here is the code:
using System;
using System.Collections.Generic;
class HomePlanet {
static void Main() {
// Type your username and press enter
Console.WriteLine("Enter Your home planet:");
// Create a string variable and get user input from the keyboard
and store it in the variable
string myPlanet = Console.ReadLine();
myPlanet =myPlanet.ToUpper();
HashSet<char> earth_hash = new HashSet<char>();
earth_hash.Add('E');
earth_hash.Add('A');
earth_hash.Add('R');
earth_hash.Add('T');
earth_hash.Add('H');
HashSet<char> venus_hash = new HashSet<char>();
venus_hash.Add('V');
venus_hash.Add('E');
venus_hash.Add('N');
venus_hash.Add('U');
venus_hash.Add('S');
HashSet<char> mars_hash = new HashSet<char>();
mars_hash.Add('M');
mars_hash.Add('A');
mars_hash.Add('R');
mars_hash.Add('S');
int which_planet = 0;
if (myPlanet.Contains("T")){
bool flag =false;
//Console.WriteLine(myPlanet);
foreach (char c in myPlanet)
{
flag =true;
if(!earth_hash.Contains(char.ToUpper(c))){
flag= false;
break;
}
}
if (flag && myPlanet.Length == earth_hash.Count){
which_planet = 1;
}
}
else if(myPlanet.Contains("V")){
//Console.WriteLine("insisde venus");
bool flag =false;
foreach (char c in myPlanet)
{ flag =true;
if(!venus_hash.Contains(char.ToUpper(c))){
flag =false;
break;
}
}
if (flag && myPlanet.Length == venus_hash.Count){
which_planet = 2;
}
}
else{
bool flag =false;
foreach (char c in myPlanet)
{ flag =true;
if(!mars_hash.Contains(char.ToUpper(c))){
flag =false;
break;
}
}
if (flag && myPlanet.Length == mars_hash.Count)
which_planet = 3;
}
switch(which_planet)
{
case 1:
Console.WriteLine("You are an Earthling and you have 10
fingers");
break;
case 2:
// code block
Console.WriteLine("You are a Venusian and you have 12
fingers");
break;
case 3:
Console.WriteLine("You are a Martian and you have 8
fingers");
break;
default:
// code block
Console.WriteLine(" I am sorry I don't know of that planet");
break;
}
}
}
HOPE I HELPED YOU.