1

开始学习循环。似乎这些事情会永远持续下去,因为我没有告诉它停止。问题是我不知道如何告诉它停止。我假设有一个声明,例如 != 但我真的不知道。

有人愿意解释循环如何停止吗?

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ConsoleApplication326
 {
class Program
{
    static void Main(string[] args)
    {
        bool triLoop = true;
        while (triLoop)
        {
            Console.WriteLine("Please enter the first integer...");
            int firstInt = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the second integer...");
            int secondInt = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the third integer...");
            int thirdInt = int.Parse(Console.ReadLine());

            if ((firstInt + secondInt > thirdInt) && (secondInt + thirdInt > firstInt) && (firstInt + thirdInt > secondInt))
            {
                Console.WriteLine("The numbers {0}, {1}, and {2} CAN represent sides of the same triangle.", firstInt, secondInt, thirdInt);
            }

            else
            {
                Console.WriteLine("The numbers {0}, {1}, and {2} CANNOT represent the sides of the same triangle.", firstInt, secondInt, thirdInt);
            }
        }
    }
}

}

4

8 回答 8

6

break语句将“跳出”循环。

或者,您可以将布尔值 ( triLoop) 设置为false.

于 2013-08-20T16:53:07.620 回答
4

任何人都愿意解释循环如何停止

只要循环中的条件为真,while 循环就会运行。为了打破它,您需要将 while 循环中的表达式设置为 false。

triLoop 当您设置为 false时它将停止。您应该阅读文档

while(triLoop)
{
   if(somecondition)
     triLoop = false;   //loop will not run after this
}

一个基本的例子。这个循环将运行到 5 点。

int n = 1;
while (n < 6) 
{
     Console.WriteLine("Current value of n is {0}", n);
     n++;
}
于 2013-08-20T16:53:01.497 回答
4

设置triLoopfalse。或使用break;.

于 2013-08-20T16:53:09.653 回答
2

有多个答案。

break; //exits the loop and continues with the code
return; //stops the loop and doesn't proceed with the rest of the code

在您的情况下,您还可以将 triloop 设置为 false。

于 2013-08-20T17:01:57.100 回答
2

其他答案已经解释了条件是如何工作的,但是如果您想询问用户是否要继续,您可以将其添加到循环的末尾:

Console.WriteLine("Would you like to continue? (Y/N)");
if(Console.ReadLine() == "Y")
    triLoop = false;

然后,如果用户键入“Y”,则条件将评估为 false,并且循环将结束。

于 2013-08-20T16:59:56.607 回答
1

尝试这个:

        while (triLoop)
        {
            Console.WriteLine("Please enter the first integer...");
            int firstInt = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the second integer...");
            int secondInt = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the third integer...");
            int thirdInt = int.Parse(Console.ReadLine());

            if ((firstInt + secondInt > thirdInt) && (secondInt + thirdInt > firstInt) && (firstInt + thirdInt > secondInt))
            {
                Console.WriteLine("The numbers {0}, {1}, and {2} CAN represent sides of the same triangle.", firstInt, secondInt, thirdInt);
            }

            else
            {
                Console.WriteLine("The numbers {0}, {1}, and {2} CANNOT represent the sides of the same triangle.", firstInt, secondInt, thirdInt);
            }
            Console.WriteLine("press 0 if you want to continue...");
            int flag = int.Parse(Console.ReadLine());
            if(flag!=0) break;

        }
于 2013-08-20T17:01:56.353 回答
1

当您使用while循环时

while (triLoop)
        {

        }

此循环在triLoop变量为时运行true

您需要将其设置为循环false内的某个位置while

喜欢

while (triLoop)
{
    //your code
    // on some condition
    triLoop = false;  
}

或者

while (triLoop)
{
    //your code
    // on some condition
    break;        
}
于 2013-08-20T16:59:50.720 回答
1

在循环中使用它来停止循环

break;
于 2013-08-20T16:54:32.710 回答