我不知道自己哪里出错了,尽管将代码分成了“嗡嗡声”和“嗡嗡声”。和' 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;
}
}
}
答案 0 :(得分:0)
你的FizzBuzz方法总是返回true,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已经解决,因此您只需阅读其值。
答案 1 :(得分:0)
您正在做两件事,这些事情在放在一起时会导致FizzCount
(及相关)值每次都增加。
首先,您要拨打IsFizz
两次 - 一次在input
上,一次在num
上。因此,您有两次机会在循环中增加FizzCount
值。
其次,input
永远不会设置为0
以外的其他任何内容。
这意味着if (input % 9 == 0)
每次都会返回true
并增加FizzCount
值。