Skip to content

Commit 5c93a7f

Browse files
committed
FizzBuzz
- Fixed the program so the user can specify a range of numbers to start and end at - Updated FizzBuzzLogic Method so that the it no iterates through the range given, store the result of each in a string before returning the string and outputting it to the console
1 parent 98ee424 commit 5c93a7f

File tree

11 files changed

+46
-45
lines changed

11 files changed

+46
-45
lines changed

.vs/VSWorkspaceState.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"\\FizzBuzz\\FizzBuzz",
66
"\\FizzBuzz\\FizzBuzz\\FizzBuzz"
77
],
8-
"SelectedNode": "\\FizzBuzz\\FizzBuzz\\FizzBuzz.sln",
8+
"SelectedNode": "\\FizzBuzz\\FizzBuzz\\FizzBuzz\\FizzBuzz.cs",
99
"PreviewInSolutionExplorer": false
1010
}

.vs/slnx.sqlite

0 Bytes
Binary file not shown.
1.5 KB
Binary file not shown.

FizzBuzz/FizzBuzz/FizzBuzz/FizzBuzz.cs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FizzBuzz
22
// by Steven Partlow
3-
// DATE: 02/06/2023
3+
// DATE: 06/06/2023
44

55
// FIZZ BUZZ Class Defintion
66

@@ -12,40 +12,21 @@ namespace FizzBuzz
1212
class FizzBuzz // A class for the main FizzBuzz application logic
1313
{
1414
// Definition for method FizzBuzzLogic, the number entered by the user in the main program is passed in, the method does not return anything
15-
public static void FizzBuzzLogic(double userNum)
15+
public static string FizzBuzzLogic(int N, int M)
1616
{
17+
string Sequence = "\n"; // We use the string Sequence to start the result of our calculations as we go, we start on a new line
1718

18-
// FizzBuzz LOGIC
19-
if (userNum % 3 == 0 && userNum % 5 == 0) // If the the value the user entered is divisable by three and five then
19+
for (int i = N; i <= M; i++) // We iterate from N upto and including M with i representing the current value
2020
{
21-
Console.WriteLine("\nFizzBuzz"); // Output 'FizzBuzz' to the console
22-
Console.WriteLine("\nThe application will now exit, Press any KEY to continue.\n"); // Output this text to the console
23-
Console.ReadKey(); // Peuse the application until the user presses a key
24-
} // End IF
21+
if (i % 3 == 0 && i % 5 == 0) Sequence += "FizzBuzz"; // If i is divisable by both 3 and 5 with no remainder we add "FizzBuzz" to the sequence
22+
else if (i % 3 == 0) Sequence += "Fizz"; // If i is divisable by 3 with no remainder we add "Fizz" to the sequence
23+
else if (i % 5 == 0) Sequence += "Buzz"; // If i is divisable by 5 with no remainder we add "Buzz" to the sequence
24+
else Sequence += i; // If it is not divisable by either with just add the current value of i to the sequence
2525

26-
// Fizz LOGIC
27-
else if (userNum % 3 == 0) // If the the value the user entered is divisable by three then
28-
{
29-
Console.WriteLine("\nFizz"); // Output 'Fizz' to the console
30-
Console.WriteLine("\nThe application will now exit, Press any KEY to continue.\n"); // Output this text to the console
31-
Console.ReadKey(); // Peuse the application until the user presses a key
32-
} // End ELSE IF
26+
if (i < M) Sequence += ", "; // If we are not equal to M yet, we add a comma and a space to sequence
27+
} // End FOR
3328

34-
// Buzz LOGIC
35-
else if (userNum % 5 == 0) // If the the value the user entered is divisable by five then
36-
{
37-
Console.WriteLine("\nBuzz"); // Output 'Buzz' to the console
38-
Console.WriteLine("\nThe application will now exit, Press any KEY to continue.\n"); // Output this text to the console
39-
Console.ReadKey(); // Peuse the application until the user presses a key
40-
} // End ELSE IF
41-
42-
// LOGIC for any other outcome
43-
else // If none of the the three condition above are met
44-
{
45-
Console.WriteLine("\nSorry not Fizz or Buzz :("); // Output this text to the console
46-
Console.WriteLine("\nThe application will now exit, Press any KEY to continue.\n"); // Output this text to the console
47-
Console.ReadKey(); // Peuse the application until the user presses a key
48-
} // End ELSE
29+
return Sequence; // Now that the FORR loop has ended we return the value of sequence
4930

5031
} // End FizzBuzzLogic METHOD
5132

FizzBuzz/FizzBuzz/FizzBuzz/Program.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// FizzBuzz
22
// by Steven Partlow
3-
// DATE: 02/06/2023
3+
// DATE: 06/06/2023
44

5-
/* A C# version of the FizzBuzz problem, uses a TRY / CATCH block to exceptions, check that the number entered is a whole number with no
6-
decimal places , then checks for FizzBuzz (divisable by 3 and 5), Fizz (divisable by 3) and Buzz (divisable by 5) before a general message
7-
if none of those three apply, then the program ends */
5+
/* A C# version of the FizzBuzz problem, uses a TRY / CATCH block to exceptions, check that the numbers entered are whole numbers with no
6+
decimal places , then checks for FizzBuzz (divisable by 3 and 5), Fizz (divisable by 3) and Buzz (divisable by 5), or if none of those apply,
7+
adding each result to a string before outputting the result */
88

99
using System;
10+
using FizzBuzz;
1011

1112
namespace FizzBuzz
1213
{
@@ -19,25 +20,41 @@ static void Main(string[] args)
1920
while (true)
2021
{
2122

22-
double userNum; // Define variable userNum as a double data type
23+
double startNum; // Define variable startNum as a double data type, this represents the start of the range
24+
double endNum; // Define variable endNum as a double data type, this represents the end of the range
2325
try // Try this code, get the user to enter a whole number and catch any errors
2426
{
2527
// Output the following text to the console
2628
Console.WriteLine("FizzBuzz");
2729
Console.WriteLine("--------\n");
2830

29-
Console.Write("Please enter a WHOLE number: "); // Ask the user to enter a whole number
30-
userNum = Convert.ToDouble(Console.ReadLine()); // Take the inputted user string, convert to type int and store in variable userNum
31+
Console.Write("Please enter the NUMBER for START of the RANGE: "); // Ask the user to enter a whole number
32+
startNum = Convert.ToDouble(Console.ReadLine()); // Take the inputted user string, convert to type int and store in variable startNum
3133

32-
bool isInt = userNum == (int)userNum; // Check if the number entered is a whole number with no decimal places
33-
if (isInt == false) // If it is not a whole number
34+
bool startInt = startNum == (int)startNum; // Check if the number entered is a whole number with no decimal places
35+
if (startInt == false) // If it is not a whole number
3436
{
3537
Console.WriteLine("\nPlease enter a WHOLE, no decimal places, Please try again!.\n"); // Output this text to the console
3638
continue; // Restart the loop and get the user enter a number again
3739
} // End IF
38-
else if (userNum == 0)
40+
else if (startNum == 0)
3941
{
40-
Console.WriteLine("\nPlease do not enter a ZERO, Thank You!.\n"); // Output this text to the console
42+
Console.WriteLine("\nPlease do not enter a ZERO, enter as least 1, Thank You!.\n"); // Output this text to the console
43+
continue; // Restart the loop and get the user enter a number again
44+
} // End ELSE IF
45+
46+
Console.Write("Please enter the NUMBER for the END of the RANGE: "); // Ask the user to enter a whole number
47+
endNum = Convert.ToDouble(Console.ReadLine()); // Take the inputted user string, convert to type int and store in variable startNum
48+
49+
bool endInt = endNum == (int)endNum; // Check if the number entered is a whole number with no decimal places
50+
if (endInt == false) // If it is not a whole number
51+
{
52+
Console.WriteLine("\nPlease enter a WHOLE, no decimal places, Please try again!.\n"); // Output this text to the console
53+
continue; // Restart the loop and get the user enter a number again
54+
} // End IF
55+
else if (endNum == 0)
56+
{
57+
Console.WriteLine("\nPlease do not enter a ZERO, enter as least 1, Thank You!.\n"); // Output this text to the console
4158
continue; // Restart the loop and get the user enter a number again
4259
} // End ELSE IF
4360

@@ -63,8 +80,11 @@ static void Main(string[] args)
6380

6481
// End of CATCH Block
6582

66-
FizzBuzz.FizzBuzzLogic(userNum); // Execute the FizzBuzzLogic method of the FizzBuzz class passing in the value of userNum
67-
break; // As we have successfully validated the user input and run the FizzBuzz logic, we can break the loop and end the application
83+
// Execute the FizzBuzzLogic method of the FizzBuzz class passing in both startNum and endNum as converted integers, store the returned string in sequence
84+
string sequence = FizzBuzz.FizzBuzzLogic(Convert.ToInt32(startNum), Convert.ToInt32(endNum));
85+
86+
Console.WriteLine(sequence); // Output the value of sequence to the console
87+
break; // As we have successfully validated the user input and run the FizzBuzz logic, and displayed the result, we can break the loop and end the application
6888

6989
} //End WHILE
7090

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)