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