使用while语句中断循环

时间:2017-11-04 04:23:17

标签: c# desktop-application

我正在编写一个C#程序,如果使用number <= 20循环number > 20 while,则number > 26和“false”打印“true”,但程序会继续执行。

如果程序达到一定数量,我想打破程序,例如public static void Main() { Console.WriteLine("Please enter a number"); int numnber = Convert.ToInt32(Console.ReadLine()); while (numnber <= 20) { Console.WriteLine("True"); Console.ReadLine(); int number1 = numnber++; while (numnber > 20) { Console.WriteLine("False"); Console.ReadLine(); } } }

该计划的代码是:

 public Dictionary<string, AudioSource> AudioDictionary = new Dictionary<string, AudioSource>() ;
 private List<AudioSource> resAudioSource = new List<AudioSource>();
 private const string ResourcePath = "Audio/";

 private void Awake()
 {
     #region instance
     if (instance == null)
     {
         instance = this;
         DontDestroyOnLoad(gameObject);
     }
     else
     {
         Destroy(gameObject);
     }
     #endregion

     AudioClip[] resAudio = Resources.LoadAll<AudioClip>(ResourcePath);
     AudioSource temp;

     for (int audioNum = 0; audioNum < resAudio.Length; audioNum++)
     {
         temp = gameObject.AddComponent<AudioSource>();
         Debug.Log(resAudio[audioNum].name);
         AudioDictionary.Add(resAudio[audioNum].name, temp);
     }
 }

4 个答案:

答案 0 :(得分:0)

你可以使用break;试试这个:

        Console.WriteLine("Please enter a number");
        int numnber = Convert.ToInt32(Console.ReadLine());

        while (numnber <= 20)
        {
            Console.WriteLine("True");
            Console.ReadLine();
            int number1 = numnber++;

            while (numnber > 20)
            {
                Console.WriteLine("False");
                Console.ReadLine();
            }
            break;
        }

但是这个编程并不是很好,因为你毕竟打破循环只是你可以像这样使用if else ..

        Console.WriteLine("Please enter a number");
        int numnber = Convert.ToInt32(Console.ReadLine());

        if(numnber <= 20)
        {
            Console.WriteLine("True");
        }
        else
        {
            Console.WriteLine("False");
        }

答案 1 :(得分:0)

根据您的描述,您的主要问题应该是

  

程序继续执行

然而,正如@King King提到的那样,它不会达到26。 它卡在内部while内,应由if条件替换。

最后,如果您必须执行号码检查至少一次,您可以使用do while

do
{
    if (numnber <= 20)
    {
        Console.WriteLine("True");
    }
    else if (numnber > 20)
    {
        Console.WriteLine("False");
    }

    numnber++;

    Console.WriteLine("number: {0}", numnber);

} while (numnber < 26);

Console.ReadLine();

答案 2 :(得分:0)

谢谢大家,但解决问题的方法是。代码如下:

public static void Main()
{
    int number;
    do
    {
        Console.WriteLine("Please enter a number");
        number = Convert.ToInt32(Console.ReadLine());

        do
        {

            Console.WriteLine("True value. True value is supplied");
            Console.ReadLine();
            number++;

            while (number >= 21 && number < 27) 
            {
                Console.WriteLine("False value. False value is supplied");
                Console.ReadLine();
                number++;
            } 

        } while (number <= 20);
    } while (number > 27);
}

答案 3 :(得分:0)

/*Your question isn't clear enough. if your case is to print true or false for given condition until the number is less the user entered value you can use this... You can change the while loops condition according to your criteria*/

        public static void Main()
        {
            Console.WriteLine("Please enter a number");
            int numnber = Convert.ToInt32(Console.ReadLine());
            while (numnber < 26)
            {
                if (numnber <= 20)
                {
                    Console.WriteLine("true");


                }
                else
                {
                    Console.WriteLine("false");
                }
                numnber++;
            }
        }