Skip to content

Commit 520460d

Browse files
committed
Added double.TryParse to EnterNumbers method
1 parent 8bcd294 commit 520460d

12 files changed

Lines changed: 16 additions & 6 deletions

File tree

Calculator/Calculator/Program.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,20 @@ public static double[] EnterNumbers(int numberOfNumbers)
6161

6262
for (int i = 0; i < numberOfNumbers; i++)
6363
{
64-
Console.WriteLine("Enter a number: ");
64+
Console.WriteLine("Please enter a number: ");
6565
string userInput2 = Console.ReadLine();
66-
double number = double.Parse(userInput2);
67-
numbers[i] = number;
66+
double number;
67+
68+
if (double.TryParse(userInput2, out number))
69+
{
70+
numbers[i] = number;
71+
}
72+
else
73+
{
74+
Console.WriteLine($"{userInput2} is not a number.");
75+
i -= 1;
76+
}
77+
6878
}
6979
return numbers;
7080
}
512 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
40 Bytes
Binary file not shown.

Calculator/Calculator/obj/Debug/net9.0/Calculator.AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[assembly: System.Reflection.AssemblyCompanyAttribute("Calculator")]
1414
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
1515
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
16-
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+5aa882f6c7d08a07a5e4599ecef875543487c939")]
16+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+8bcd29400d7e6302fe9b2d3c2fbf225962465a48")]
1717
[assembly: System.Reflection.AssemblyProductAttribute("Calculator")]
1818
[assembly: System.Reflection.AssemblyTitleAttribute("Calculator")]
1919
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
547919f79659e7ee2be0a469cdc3734ed917e3df72dbd1443f2486612bfa8762
1+
778e648fe7c041199042766ce68d644c3a96059404463770103ef176c5b863ed
512 Bytes
Binary file not shown.
40 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"documents":{"C:\\Users\\hanna\\RiderProjects\\CSharpPreReading\\*":"https://raw.githubusercontent.com/hduns/CSharpPreReading/5aa882f6c7d08a07a5e4599ecef875543487c939/*"}}
1+
{"documents":{"C:\\Users\\hanna\\RiderProjects\\CSharpPreReading\\*":"https://raw.githubusercontent.com/hduns/CSharpPreReading/8bcd29400d7e6302fe9b2d3c2fbf225962465a48/*"}}
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)