对 FizzBuzz 程序没有返回预期结果感到沮丧,第一次编程评估
Frustrated with FizzBuzz program not returning intended results, first programming assesment
尽管将代码分成 'buzz' 和 'fizz' 部分,但我还是不明白我哪里出错了,它仍然为每个数字增加一个增量。
static void Main(string[] args)
{
//Open file
//=========
string path = "FizzBuzzText.txt";
FileInfo txtFile = new FileInfo(path);
//Does text file exist
//====================
if (txtFile.Exists)
{
string line;
string inputString = "";
int input = 0;
int num = 0;
bool Fizz;
bool Buzz;
bool FizzBuzz;
bool Prime;
FizzBuzz fb = new FizzBuzz();
StreamReader file = new StreamReader(path); //Read File
inputString = Console.ReadLine();
//Read File line by line
//=======================
while ((line = file.ReadLine()) != null){
num = Convert.ToInt32(line); //Convert to Int
Console.WriteLine(num);
Fizz = fb.IsFizz(num); //Is it a fizz
Buzz = fb.IsBuzz(num); //Is it a Buzz
FizzBuzz = fb.IsFizzBuzz(num); //Is it a FizzBuzz
Prime = fb.IsPrime(num); //Is the number a prime
Console.WriteLine("IsFizz " + fb.IsFizz(input)); //Shows if IsFizz is true or false
Console.WriteLine("IsBuzz " + fb.IsBuzz(input)); //Shows if IsBuzz is true or false
Console.WriteLine("IsFizzBuzz " + fb.IsFizzBuzz(input)); //Shows if IsFizzBuzz is true or false
Console.WriteLine("IsPrime " + fb.IsPrime(input)); //Shows if IsPrime is true or false
Console.WriteLine("Fizz Count total is " + fb.TotalFizz()); //Shows the total amount of Fizz counters
Console.WriteLine("Buzz Count total is " + fb.TotalBuzz()); //Shows the total amount of Buzz counters
Console.WriteLine("FizzBuzz Count total is " + fb.TotalFizzBuzz()); //Shows the total amount of FizzBuzz counters
Console.WriteLine("Prime Count total is " + fb.TotalPrime()); //Shows the total amount of Prime counters
}
Console.ReadKey();
}
else{
Console.WriteLine("File Does not exist!");
Console.ReadKey();
}
}
//=================
//Fizz Buzz Methods
//=================
public bool IsFizz(int input)
{
if (input % 9 == 0)
{
FizzCount++; //Add 1 to fizzCount
}
return true;
}
public bool IsBuzz(int input)
{
if (input % 13 == 0)
{
BuzzCount++; //Add 1 to buzzCount
}
return true;
}
public bool IsFizzBuzz(int input)
{
if ((input % 13 == 0) && (input % 9 == 0))
{
FizzBuzzCount++; //Add 1 to fizzbuzzCount
}
return true;
}
public bool IsPrime(int input) //It is only divisible by itself and one.
{
int i = 2;
while (i * i <= input)
{
if ((input % i) == 0)
return false;
else i++;
}
PrimeCount++; //Add 1 to primeCount, prime count defined as int value
return true;
}
public void BeginTesting()
{
FizzCount = 0; //Resets fizzCount
BuzzCount = 0; //Resets buzzCount
FizzBuzzCount = 0; //Resets fizzbuzzCount
PrimeCount = 0; //Resets primeCount
}
public int TotalFizz()
{
return FizzCount;
}
public int TotalBuzz()
{
return BuzzCount;
}
public int TotalFizzBuzz()
{
return FizzBuzzCount;
}
public int TotalPrime()
{
return PrimeCount;
}
}
}
您的 FizzBuzz 方法始终 return 为真,return 语句需要位于 if 代码块内。
将您的方法更改为以下内容:
public bool IsFizz(int input)
{
if (input % 9 == 0)
{
FizzCount++; //Add 1 to fizzCount
return true;
}
else
return false;
}
另请注意,您的控制台消息正在再次调用这些方法,这将在 while 块的每个循环中将计数器迭代 2 而不是 1。将 Console.WriteLine("IsFizz " + fb.IsFizz(input));
更改为 Console.WriteLine("IsFizz " + Fizz);
,因为 Fizz 已经解决,因此您可以直接读取它的值。
你正在做两件事,当它们放在一起时,会导致你的 FizzCount
(和相关)值每次都增加。
首先,您调用 IsFizz
两次 - 一次在 input
上,一次在 num
上。因此,您有两次机会在循环中每次 运行 增加 FizzCount
值。
其次,input
绝不会设置为 0
以外的任何值。
这意味着每次if (input % 9 == 0)
returns true
并增加FizzCount
值。
尽管将代码分成 'buzz' 和 'fizz' 部分,但我还是不明白我哪里出错了,它仍然为每个数字增加一个增量。
static void Main(string[] args)
{
//Open file
//=========
string path = "FizzBuzzText.txt";
FileInfo txtFile = new FileInfo(path);
//Does text file exist
//====================
if (txtFile.Exists)
{
string line;
string inputString = "";
int input = 0;
int num = 0;
bool Fizz;
bool Buzz;
bool FizzBuzz;
bool Prime;
FizzBuzz fb = new FizzBuzz();
StreamReader file = new StreamReader(path); //Read File
inputString = Console.ReadLine();
//Read File line by line
//=======================
while ((line = file.ReadLine()) != null){
num = Convert.ToInt32(line); //Convert to Int
Console.WriteLine(num);
Fizz = fb.IsFizz(num); //Is it a fizz
Buzz = fb.IsBuzz(num); //Is it a Buzz
FizzBuzz = fb.IsFizzBuzz(num); //Is it a FizzBuzz
Prime = fb.IsPrime(num); //Is the number a prime
Console.WriteLine("IsFizz " + fb.IsFizz(input)); //Shows if IsFizz is true or false
Console.WriteLine("IsBuzz " + fb.IsBuzz(input)); //Shows if IsBuzz is true or false
Console.WriteLine("IsFizzBuzz " + fb.IsFizzBuzz(input)); //Shows if IsFizzBuzz is true or false
Console.WriteLine("IsPrime " + fb.IsPrime(input)); //Shows if IsPrime is true or false
Console.WriteLine("Fizz Count total is " + fb.TotalFizz()); //Shows the total amount of Fizz counters
Console.WriteLine("Buzz Count total is " + fb.TotalBuzz()); //Shows the total amount of Buzz counters
Console.WriteLine("FizzBuzz Count total is " + fb.TotalFizzBuzz()); //Shows the total amount of FizzBuzz counters
Console.WriteLine("Prime Count total is " + fb.TotalPrime()); //Shows the total amount of Prime counters
}
Console.ReadKey();
}
else{
Console.WriteLine("File Does not exist!");
Console.ReadKey();
}
}
//=================
//Fizz Buzz Methods
//=================
public bool IsFizz(int input)
{
if (input % 9 == 0)
{
FizzCount++; //Add 1 to fizzCount
}
return true;
}
public bool IsBuzz(int input)
{
if (input % 13 == 0)
{
BuzzCount++; //Add 1 to buzzCount
}
return true;
}
public bool IsFizzBuzz(int input)
{
if ((input % 13 == 0) && (input % 9 == 0))
{
FizzBuzzCount++; //Add 1 to fizzbuzzCount
}
return true;
}
public bool IsPrime(int input) //It is only divisible by itself and one.
{
int i = 2;
while (i * i <= input)
{
if ((input % i) == 0)
return false;
else i++;
}
PrimeCount++; //Add 1 to primeCount, prime count defined as int value
return true;
}
public void BeginTesting()
{
FizzCount = 0; //Resets fizzCount
BuzzCount = 0; //Resets buzzCount
FizzBuzzCount = 0; //Resets fizzbuzzCount
PrimeCount = 0; //Resets primeCount
}
public int TotalFizz()
{
return FizzCount;
}
public int TotalBuzz()
{
return BuzzCount;
}
public int TotalFizzBuzz()
{
return FizzBuzzCount;
}
public int TotalPrime()
{
return PrimeCount;
}
}
}
您的 FizzBuzz 方法始终 return 为真,return 语句需要位于 if 代码块内。
将您的方法更改为以下内容:
public bool IsFizz(int input)
{
if (input % 9 == 0)
{
FizzCount++; //Add 1 to fizzCount
return true;
}
else
return false;
}
另请注意,您的控制台消息正在再次调用这些方法,这将在 while 块的每个循环中将计数器迭代 2 而不是 1。将 Console.WriteLine("IsFizz " + fb.IsFizz(input));
更改为 Console.WriteLine("IsFizz " + Fizz);
,因为 Fizz 已经解决,因此您可以直接读取它的值。
你正在做两件事,当它们放在一起时,会导致你的 FizzCount
(和相关)值每次都增加。
首先,您调用 IsFizz
两次 - 一次在 input
上,一次在 num
上。因此,您有两次机会在循环中每次 运行 增加 FizzCount
值。
其次,input
绝不会设置为 0
以外的任何值。
这意味着每次if (input % 9 == 0)
returns true
并增加FizzCount
值。