patterncsharpMinor
Parameterized FizzBuzz
Viewed 0 times
parameterizedfizzbuzzstackoverflow
Problem
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
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
Solution
Some quick remarks
- variables should be named using
camelCasecasing
- avoid declaring multiple variables on the same line to improve readability.
- don't trust the users of your app! Never trust them! If you think, hey they can read and follow instructions you will get into great pain. Always check that inputs are valid. That beeing said you should use
int.TryParse()instead ofConvert.ToInt32()
- method arguments should be named using
camelCasecasing as well.
- let your variables have some room to breathe (see the
%operator)
- don't use abbreviations for naming stuff. In a few months you won't know what they are about.
- comments should explain why something is done in the way it is. Let the code speak for itself what is done by using names as descriptive as possible.
Context
StackExchange Code Review Q#119654, answer score: 4
Revisions (0)
No revisions yet.