我在这个循环中做错了什么

本文关键字:错了 什么 循环 | 更新日期: 2023-09-27 18:36:38

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

    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();
        }   
    }   
}

我在这个循环中做错了什么

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);
}

此行

input = int.Parse(inputString);

应该在循环内。

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 
    }   
}  
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);  
}
}