2013-08-20 127 views
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); 
      } 
     } 
    } 
} 

}

+1

我注意到你還沒有接受任何答案 - 如果這個問題的答案之一(或你發佈的其他答案)幫助你解決了你的問題,你可以點擊左邊的複選標記將其標記爲已接受文本(正下方的投票按鈕)。 – Michelle

回答

6

break語句將「循環」出循環。

或者,您可以將布爾值(triLoop)設置爲false

+0

這很漂亮。如果我想提示任何用戶看他是否想繼續,該怎麼辦?我會只是一個「Y」或「N」虛假或真實? – Zoro

+0

@Azzamean你可以 - 你可以讓邏輯做你想做的事情。 –

4

人照顧解釋循環如何停止

while循環,只要循環中的條件爲真運行。爲了打破它,你需要將while循環中的表達式設置爲false。

當您將triLoop設置爲false時,它將停止。您應該閱讀documentation

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++; 
} 
4

triLoop設置爲false。或使用break;

1

利用這個循環中,你正在使用while循環

while (triLoop) 
     { 

     } 

這個循環運行,而triLoop變量是true

您需要將其設置爲false停止循環

break; 
+1

不要忘記,如果您在發佈後意識到另一個答案(更好或者相同)已經發布,那麼刪除您的帖子是不需要花費的,以避免混亂的問題和不添加任何內容的答案。 – Michelle

1

while迴路內某處

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

while (triLoop) 
{ 
    //your code 
    // on some condition 
    break;   
} 
2

其他答案已經解釋的條件是如何工作的,但如果你要問用戶是否要繼續,你可以添加這個到年底你的循環:

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

然後,如果用戶鍵入「Y」,則條件將評估爲false,並且循環將結束。

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; 

     } 
+1

或者您可以更改int.Parse並使用像「Y」或「N」這樣的字符作爲標誌值。 –

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。