0

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

    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

4 回答 4

0

这条线

input = int.Parse(inputString);

应该在循环内。

于 2013-10-24T10:09:30.233 回答
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);  
}
}
于 2013-10-24T10:43:29.930 回答
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);
}
于 2013-10-24T10:11:05.677 回答
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 

    }   
}  
于 2013-10-24T10:26:50.493 回答