Print Smileys: Code-Along
Create a program that will print some smiley faces based on a user's input. Start from a new C# Repl project named "Print Smileys" and follow the instructions below.
The code for the final program is available, but try to complete the exercise before checking the solution.
Part 1 - Defining the smiley face method
The smiley face method should print a certain number of smiley faces based on a parameter.
- Define a new method named
PrintSmileyin the main.cs file- The method should take in one
intparameter (numSmileys) - The method should return
void(nothing)
- The method should take in one
- In the body of the
PrintSmileymethod, create awhileloop- The
whileloop should executenumSmileystimes
- The
- In the body of the
whileloop, print a smiley face (:))
public static void PrintSmileys(int numSmileys)
{
int i = 0;
while (i < numSmileys)
{
Console.WriteLine(":)");
i++;
}
}
Part 2 - User input
The main program should ask the user how many smileys to print. If they enter a number less than zero, the program should display an error message.
- In the
Mainmethod, display a message asking the user how many smileys they would like - Convert their input into an
int, and store it in a variable - Use an
ifstatement to check if the number they entered is positive- If it is positive, call the
PrintSmileymethod with the number as an argument - If it is not positive, display a message saying "Please enter a positive number"
- If it is positive, call the
Console.WriteLine("How many smileys would you like?");
int num = Convert.ToInt32(Console.ReadLine());
if (num > 0)
{
PrintSmileys(num);
}
else
{
Console.WriteLine("Please enter a positive number");
}