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
PrintSmiley
in the main.cs file- The method should take in one
int
parameter (numSmileys
) - The method should return
void
(nothing)
- The method should take in one
- In the body of the
PrintSmiley
method, create awhile
loop- The
while
loop should executenumSmileys
times
- The
- In the body of the
while
loop, 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
Main
method, 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
if
statement to check if the number they entered is positive- If it is positive, call the
PrintSmiley
method 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");
}