It just displays this instead of the error warning I made:
Here is how I want it to be:
And here is my code:
Console.Write("Masukkan Tinggi Segitiga: ");
int tinggi = Convert.ToInt32(Console.ReadLine());
if (tinggi > 0)
{
for (int x = 1; x <= tinggi; x++)
{
for (int y = 1; y <= x; y++)
{
Console.Write("*");
}
Console.WriteLine("");
}
}
else if (tinggi <= 0)
{
Console.WriteLine("Input tidak valid! Harus berupa angka & bilangan bulat");
}
2
Answers
The reason why it throws an exception is that you passed in an invalid number string and try to parse it. (i.e. "a" is not a number.)
Using
int.TryParse()
and use the returned value to control your condition to output the error message you want:Output:
you could try: