我有一个FizzBuzz分配,其计数从1到1000 如果一个数字可被3整除 如果它可以被5整除 如果两者都可以被fizzbuzz
我已经设法完成了该项目,我的问题是我试图最后分别返回Fizz,Buzz和Fizzbuzz的总数。返回声明对我不起作用,请帮忙吗? 我的代码:
using System;
namespace FizzBuzz
{
public class Class1
{
public static void Main(string[] args)
{
for (int i = 1; i <= 1000; i++)
{
//Declaration of Fizz and Buzz as Boolean
bool fizz = i % 3 == 0;
bool buzz = i % 5 == 0;
//Fizzbuzz declared to be conditional if its both
bool fizzbuzz = fizz && buzz;
if (fizzbuzz)
{
Console.WriteLine("FizzBuzz");
}
else if (fizz)
{
Console.WriteLine("Fizz");
}
else if (i % 5 == 0)
{
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
}
}
}
答案 0 :(得分:0)
尝试一下:
using System;
namespace FizzBuzz
{
public class Class1
{
public static void Main(string[] args)
{
int fizzCount = 0;
int buzzCount = 0;
int fizzbuzzCount = 0;
for (int i = 1; i <= 1000; i++)
{
//Declaration of Fizz and Buzz as Boolean
bool fizz = i % 3 == 0;
bool buzz = i % 5 == 0;
//Fizzbuzz declared to be conditional if its both
bool fizzbuzz = fizz && buzz;
if (fizzbuzz)
{
fizzbuzzCount++;
Console.WriteLine("FizzBuzz");
}
else if (fizz)
{
fizzCount++;
Console.WriteLine("Fizz");
}
else if (buzz)
{
buzzCount++;
Console.WriteLine("Buzz");
}
else
{
Console.WriteLine(i);
}
}
Console.WriteLine("Fizz count: " + fizzCount.ToString());
Console.WriteLine("Buzz count: " + buzzCount.ToString());
Console.WriteLine("FizzBuzz count: " + fizzbuzzCount.ToString());
}
}
}