Task: to make console to display a message "Happy birthday!". Number of messages equals the number which user inputs to console.
Condition: program should break the cycle, when number of messages equals 5 or more.
The problem: my code works correct without "if" condition. When I add condition if (age == 5), and user's input is "6" or more, console shows just 1 message!
First of all I thought about mistakes in my code, but I didn't find it. Then I copied code 1 in 1 from the learning video (I learn C# with video course), but the code still works incorrect, when in video it works perfectly!
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Learning
{
class Program
{
static void Main(string[] args)
{
int age;
Console.Write("Please, input your age: ");
age = Convert.ToInt32(Console.ReadLine());
while (age-- > 0)
{
Console.WriteLine("Happy Birthday!");
if (age == 5)
{
break;
}
}
}
}
}
When I add condition if (age == 5), and user's input is "6" or more, console shows just 1 message!of course because age do not count the number of times you written the message--does.When I add condition if (age == 5), and user's input is "6" or more, console shows just 1 message!That is not true. If you enter 7, you will see it twice.