为什么我的程序不会打印countOfX值?

时间:2013-10-27 12:09:28

标签: c# fizzbuzz

感谢你在最后几个问题上的所有帮助!我想我只剩下一个问题了。由于某种原因,它不会打印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;
    }

}

}

1 个答案:

答案 0 :(得分: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();
    }