0

感谢您在最后几个 q 中的所有帮助!我想我只剩下一个问题了。出于某种原因,它不会打印 countOfX 变量的值?据我所见,它应该可以工作,所以显然我缺少一些东西!

希望至少有人能指出我正确的方向。任何帮助是极大的赞赏。

using System

namespace Part1Skeleton
{
class FizzBuzz
{
    int countOfFizz;
    int countOfBuzz;
    int countOfFizzBuzz;
    int countOfPrime;

    public static void Main()
    {
        int input = 1;
        //string enter;

        FizzBuzz fb = new FizzBuzz();
        fb.BeginTesting();

        while (input <= 20)
        {
            //Console.WriteLine("Please enter a number: ");
            //enter = Console.ReadLine();
            //input = int.Parse(enter);

            fb.BeginTesting();
            fb.IsFizz(input);
            fb.IsBuzz(input);
            fb.IsFizzBuzz(input);
            fb.IsPrime(input);

            input++;
        }
        fb.TotalFizz();
        fb.TotalBuzz();
        fb.TotalFizzBuzz();
        fb.TotalPrime();
    }

    public bool IsFizz(int input)
    {
        if (input % 9 == 0 && input != 0)
        {
            Console.WriteLine("Fizz");
            countOfFizz++;
            return true;
        }
        return false;
    }

    public bool IsBuzz(int input)
    {
        if (input % 13 == 0 && input != 0)
        {
            Console.WriteLine("Buzz");
            countOfBuzz++;
            return true;
        }
        return false;
    }

    public bool IsFizzBuzz(int input)
    {
        if (input % 9 == 0 && input % 13 == 0 && input != 0)
        {
            Console.WriteLine("FizzBuzz");
            countOfFizzBuzz++;
            return true;
        }
        return false;
    }

    public bool IsPrime(int input)
    {
        if (input == 0 || input == 1)
        {
            Console.WriteLine(input);
            return false;
        }
        else if (input % 9 == 0 || input % 13 == 0)
        {
            return false;
        }
        else
        {
            for (int i = 2; i < input; i++)
            {
                if (input % i == 0 && i != input)
                {
                    Console.WriteLine(input);
                    return false;
                }
            }
        }
        Console.WriteLine("Prime");
        countOfPrime++;
        return true;
    }

    public void BeginTesting()
    {
        countOfFizz = 0;
        countOfBuzz = 0;
        countOfFizzBuzz = 0;
        countOfPrime = 0;
    }

    public int TotalFizz()
    {
        Console.WriteLine("Number of Fizz: ");
        Console.Write(countOfFizz);
        return countOfFizz;
    }

    public int TotalBuzz()
    {
        Console.WriteLine("Number of Buzz: ");
        Console.Write(countOfBuzz);
        return countOfBuzz;
    }

    public int TotalFizzBuzz()
    {
        Console.WriteLine("Number of FizzBuzz: ");
        Console.Write(countOfFizzBuzz);
        return countOfFizzBuzz;
    }

    public int TotalPrime()
    {
        Console.WriteLine("Number of Prime: ");
        Console.Write(countOfPrime);
        return countOfPrime;
    }

}

}

4

1 回答 1

1

为什么要在 while 循环中创建 FizzBu​​zz 的新实例?只需在 while 循环开始之前创建一个实例并在整个程序中使用它。

试试这个主要代码

public static void Main()
    {
        int input = 1;
        //string enter;

       FizzBuzz fb = new FizzBuzz(); // create a single instance
       fb.BeginTesting(); // start testing

        while (input <= 20)
        {
            //Console.WriteLine("Please enter a number: ");
            //enter = Console.ReadLine();
            //input = int.Parse(enter);
            fb.IsFizz(input);
            fb.IsBuzz(input);
            fb.IsFizzBuzz(input);                
            fb.IsPrime(input);

            input++;
        }

        fb.TotalFizz();            
        fb.TotalBuzz();            
        fb.TotalFizzBuzz();            
        fb.TotalPrime();
    }
于 2013-10-27T12:17:57.450 回答