[英]breaking while loop and doesn't print the result c#
我是 c# 的初學者,我正在接受 codeforces 和 SPOJ 的培訓以學習如何解決問題,我的問題是當循環中斷時不打印所有輸入它只是結束程序而不打印
請有人告訴我我寫的這兩個代碼有什么錯誤>
輸入:1,2,88,42,99 Output:1,2,88
class Program
{
static void Main(string[] args)
{
Console.WriteLine("please enter an integer number in two digits");
int numbers;
int[] array_num = new int[100];
string nu = "";
int i =1;
while(i<100)
{
numbers = int.Parse(Console.ReadLine());
nu += numbers;
array_num[i] = numbers;
if(array_num[i]<array_num[i-1])
{
Console.WriteLine("the numbers is " +nu+ "");
break;
}
i++;
}
}
}
我在for循環中做同樣的問題
static void Main(string[] args)
{
Console.WriteLine("please enter an integer number in two digits");
int numbers;
int[] array_num = new int[50];
string nu = "";
for (int i = 1; i <= array_num.Length; i++)
{
numbers = int.Parse(Console.ReadLine());
nu += numbers;
array_num[i] = numbers;
if (array_num[i] < array_num[i - 1])
{
Console.WriteLine("the number is " + nu + "");
break;
}
}
}
什么錯誤?
回答“......問題是當循環中斷時不會打印所有輸入它只是結束程序......” :
那是因為在你中斷循環之后沒有更多的代碼可以執行 - 所以控制台關閉。 您可以在Main
方法的末尾添加Console.ReadKey()
或Console.ReadLine()
以在循環中斷后保持控制台打開。 Console.ReadKey()
會等到您按下任意鍵, Console.ReadLine()
會等到您輸入內容並按 Enter(或直接按 Enter)。 或者以這兩種方式,控制台將保持打開狀態,直到您手動將其關閉。
static void Main(string[] args)
{
// ...
for (int i = 1; i <= array_num.Length; i++)
{
// ...
if (array_num[i] < array_num[i - 1])
{
Console.WriteLine("the number is " + nu + "");
break;
}
}
Console.ReadKey(); // Here Console will wait until you press any key and will stay opened
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.