This is a FizzBuzz program, designed to take parameter inputs for start and ending numbers, as well as a "fizz" number and a "buzz" number. The program then outputs the numbers one per line. If the number is divisible by the "fizz" number (normally 3), "fizz" is printed instead, the same with the "buzz" number. If the number is divisible by both, "fizzbuzz" is the output.
I am primarily looking for advice with my choice of logic, syntax, and use of methods with regards to complexity and efficient code. However, all advice is appreciated.
using System;
class FizzBuzzProgram
{
static void Main()
{
//initialize menu variables
bool menuvalidinput = false;
string menuchoice;
/*get decision if user wants to fizzbuzz, looping
until a valid input is provided*/
do {
Console.WriteLine("Would you like to fizzbuzz? [Y]/[N]");
menuchoice = Console.ReadLine();
menuchoice = menuchoice.ToLower(); //convert to lowercase
if (menuchoice == "y" | menuchoice == "n"){
menuvalidinput = true; //accept input
} else {
Console.WriteLine("Sorry, input was not valid.");
}
} while (menuvalidinput == false);
//end of input loop
//interpret the result of either "y" or "n"
if (menuchoice == "y"){
FizzBuzzWithInput();
} else {
Console.WriteLine("That's ok, have a nice day!");
}
}
static void FizzBuzzWithInput()
{
//initialize fizzbuzz input (FBinput*) variables
int FBinputS, FBinputE, FBinputF, FBinputB;
bool FBvalidinput = false;
/*take input for start and end values,
if start is larger than end ask again*/
do {
Console.WriteLine("What number would you like to start from?");
FBinputS = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What number would you like to end at?");
FBinputE = Convert.ToInt32(Console.ReadLine());
if (FBinputS > FBinputE){ //check of start and end values
Console.WriteLine("You chose a starting number larger than your ending number;");
Console.WriteLine("please input new values.");
} else {
FBvalidinput = true; //accept input
}
} while (FBvalidinput == false);
//end validation loop
//take fizz and buzz inputs, classically 3 and 5 respectively
Console.WriteLine("What number would you like to fizz?");
FBinputF = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("What number would you like to buzz?");
FBinputB = Convert.ToInt32(Console.ReadLine());
//call the actual FizzBuzz method
RunFizzBuzz(FBinputS, FBinputE, FBinputF, FBinputB);
}
static void RunFizzBuzz(int FBstart, int FBend, int FBfizz, int FBbuzz)
{
//initialize the display string
string c;
Console.WriteLine("\nFizzbuzz time!\n");
//fizzbuzz loop
for (int i = FBstart; i <= FBend; i++){
//set c to blank string
c = "";
if(i%FBfizz == 0){
c = c + "fizz";
}
if(i%FBbuzz == 0){
c = c + "buzz";
}
if(c.Length == 0){ /*"if the number is not fizz or buzz"
aka: if(i%FBfizz!=0 & i%FBbuzz!=0)*/
c = Convert.ToString(i);
}
//print display string of number i
Console.WriteLine(c);
}
//end fizzbuzz loop
Console.WriteLine("\nFizzbuzz complete!");
Console.ReadLine();
}
}