Question

In: Computer Science

2) Write a program(C#) that prompts the user for her home planet. Based on the user's...

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]

Solutions

Expert Solution

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.


Related Solutions

Write a program(C#) that prompts the user for her home planet. Based on the user's input...
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()...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
Write a program in C that prompts the user for a number of seconds and then...
Write a program in C that prompts the user for a number of seconds and then converts it to h:m:s format. Example: 5000 seconds should display as 1:23:20 (1 hour, 23 minutes, 20 seconds.) Test with several values between about 100 seconds and 10,000 seconds. use unint and remainders for this and keep it as simple as possible.
C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and a base in the range 2 <= base <= 16. Write a function multibaseOutput() that displays the number in the specified base. The program terminates when the user enters a number of 0 and a base 0. Run: Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 155 16     155 base 16 is 9B Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT