这个循环我做错了什么?

时间:2013-10-24 10:07:41

标签: c# loops fizzbuzz

这个代码应该在它运行时完全循环,没有它完全运行的循环,但是当我添加我的循环时,第一部分工作正常,但是当你输入另一个数字时,它显示与之前完全相同的结果它永远不会改变,也不会在它应该结束时结束。这似乎是一个愚蠢的事情,但我是新手。我使用了错误的循环吗?

    using System;

    class FizzBuzz
    {
    //Global variables
    int fizzCount;
    int buzzCount;
    int fizzbuzzCount;
    int primeCount;


    public bool IsFizz(int input)
    {
        if (input % 9 ==0)
        {
            fizzCount++; //Add 1 to fizzCount
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool IsBuzz(int input)
    {
        if (input % 13 ==0)
        {
            buzzCount++; //Add 1 to buzzCount
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool IsFizzBuzz(int input) 
    {
        if ((input % 13 ==0) && (input % 9 ==0))
        {
            fizzbuzzCount++; //Add 1 to fizzbuzzCount
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool IsPrime(int input)
    {
        for(int i = 2; i < input; i++)
        {
            if (input % i ==0)
            {
                return false;
            }
        }

        primeCount++; //Add 1 to primeCount
        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;
    }

    public static void Main()
    {       
        Console.WriteLine("Please enter a number ");
        int input;
        string inputString;
        inputString = Console.ReadLine();
        input = int.Parse(inputString);
        while(input !=0) 
        {


            FizzBuzz fizzbuzz = new FizzBuzz();

            Console.WriteLine("IsFizz " + fizzbuzz.IsFizz(input)); //Shows if IsFizz is true or false
            Console.WriteLine("IsBuzz " + fizzbuzz.IsBuzz(input)); //Shows if IsBuzz is true or false
            Console.WriteLine("IsFizzBuzz " + fizzbuzz.IsFizzBuzz(input)); //Shows if IsFizzBuzz is true or false
            Console.WriteLine("IsPrime " + fizzbuzz.IsPrime(input)); //Shows if IsPrime is true or false
            Console.WriteLine("Fizz Count total is " + fizzbuzz.TotalFizz()); //Shows the total amount of Fizz counters 
            Console.WriteLine("Buzz Count total is " + fizzbuzz.TotalBuzz()); //Shows the total amount of Buzz counters 
            Console.WriteLine("FizzBuzz Count total is " + fizzbuzz.TotalFizzBuzz()); //Shows the total amount of FizzBuzz counters 
            Console.WriteLine("Prime Count total is " + fizzbuzz.TotalPrime());
            Console.WriteLine("Please enter another number or press 0 to exit");//Shows the total amount of Prime counters 
            inputString = Console.ReadLine();

        }   
    }   
}

4 个答案:

答案 0 :(得分:0)

这一行

input = int.Parse(inputString);

应该在循环内。

答案 1 :(得分:0)

inputString转换为int并将其设置为input变量。

while(input !=0) 
{
    FizzBuzz fizzbuzz = new FizzBuzz();

    Console.WriteLine("IsFizz " + fizzbuzz.IsFizz(input)); //Shows if IsFizz is true or false
    Console.WriteLine("IsBuzz " + fizzbuzz.IsBuzz(input)); //Shows if IsBuzz is true or false
    Console.WriteLine("IsFizzBuzz " + fizzbuzz.IsFizzBuzz(input)); //Shows if IsFizzBuzz is true or false
    Console.WriteLine("IsPrime " + fizzbuzz.IsPrime(input)); //Shows if IsPrime is true or false
    Console.WriteLine("Fizz Count total is " + fizzbuzz.TotalFizz()); //Shows the total amount of Fizz counters 
    Console.WriteLine("Buzz Count total is " + fizzbuzz.TotalBuzz()); //Shows the total amount of Buzz counters 
    Console.WriteLine("FizzBuzz Count total is " + fizzbuzz.TotalFizzBuzz()); //Shows the total amount of FizzBuzz counters 
    Console.WriteLine("Prime Count total is " + fizzbuzz.TotalPrime());
    Console.WriteLine("Please enter another number or press 0 to exit");//Shows the total amount of Prime counters 
    inputString = Console.ReadLine();

    // try to convert, if is not succedde, keep the original value (0).
    int.TryParse(inputString, out input);
}

答案 2 :(得分:0)

Console.ReadLine放在循环的开头,因此您只需要一次。 此外,当用户键入除数字以外的任何内容时,请使用int.TryParse退出。

public static void Main()
{       
    Console.WriteLine("Please enter a number ");
    int input;
    string inputString;

    while(input !=0) 
    {
        inputString = Console.ReadLine();
        if (!int.TryParse(inputString, out input)) break;

        FizzBuzz fizzbuzz = new FizzBuzz();

        Console.WriteLine("IsFizz " + fizzbuzz.IsFizz(input)); //Shows if IsFizz is true or false
        Console.WriteLine("IsBuzz " + fizzbuzz.IsBuzz(input)); //Shows if IsBuzz is true or false
        Console.WriteLine("IsFizzBuzz " + fizzbuzz.IsFizzBuzz(input)); //Shows if IsFizzBuzz is true or false
        Console.WriteLine("IsPrime " + fizzbuzz.IsPrime(input)); //Shows if IsPrime is true or false
        Console.WriteLine("Fizz Count total is " + fizzbuzz.TotalFizz()); //Shows the total amount of Fizz counters 
        Console.WriteLine("Buzz Count total is " + fizzbuzz.TotalBuzz()); //Shows the total amount of Buzz counters 
        Console.WriteLine("FizzBuzz Count total is " + fizzbuzz.TotalFizzBuzz()); //Shows the total amount of FizzBuzz counters 
        Console.WriteLine("Prime Count total is " + fizzbuzz.TotalPrime());
        Console.WriteLine("Please enter another number or press 0 to exit");//Shows the total amount of Prime counters 

    }   
}  

答案 3 :(得分:0)

public static void Main()
{
Console.WriteLine("Please enter a number ");
string inputString;
inputString = Console.ReadLine();
int input;
input = int.Parse(inputString);    
while(input !=0)
{
FizzBuzz fizzbuzz = new FizzBuzz();
Console.WriteLine("IsFizz {0}", fizzbuzz.IsFizz(input)); 
Console.WriteLine("IsBuzz {0}", fizzbuzz.IsBuzz(input)); 
Console.WriteLine("IsFizzBuzz {0}", fizzbuzz.IsFizzBuzz(input));
Console.WriteLine("IsPrime {0}", fizzbuzz.IsPrime(input)); 
Console.WriteLine("Fizz Count total is {0}", fizzbuzz.TotalFizz());
Console.WriteLine("Buzz Count total is {0}", fizzbuzz.TotalBuzz());
Console.WriteLine("FizzBuzz Count total is {0}", fizzbuzz.TotalFizzBuzz()); 
Console.WriteLine("Prime Count total is {0}", fizzbuzz.TotalPrime());
Console.WriteLine("Please enter another number or press 0 to exit");
inputString = Console.ReadLine();
input = int.Parse(inputString);  
}
}