2017-04-11 77 views
-3

使用系统;为什么while循环仅打印C#中的第一个元素?

namespace Loop 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int[] Numbers = new int[3]; 

      Numbers[0] = 101; 
      Numbers[1] = 102; 
      Numbers[2] = 103; 

      int i = 0; 
      while (i < Numbers.Length) 
      { 
       Console.WriteLine("Numbers are: "+ Numbers[i]); 
       i++; 
       Console.ReadKey(); 
      } 
     } 
    } 
} 

enter image description here

+1

'Console.ReadKey();' - 只需按下它 – Lanorkin

+0

它在此处等待用户输入Console.ReadKey(); –

+5

那么你打印101后按下了一个键?它适用于我...... –

回答

1

删除

Console.ReadKey(); 

否则会等待用户输入

1

请删除

Console.ReadKey(); 

因为Readkey()满足等待输入。如果您调试程序,你会意识到这一点是如果你要等待,阅读控制台Console.ReadKey();必须从while循环

+0

有没有必要尝试@通知问题提问者。当有人写*答案*时,它们会自动得到通知,这就是你所做的。 –

+0

谢谢@Damien_The_Unbeliever –

1
namespace Loop 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int[] Numbers = new int[3]; 

      Numbers[0] = 101; 
      Numbers[1] = 102; 
      Numbers[2] = 103; 

      int i = 0; 
      while (i < Numbers.Length) 
      { 
       Console.WriteLine("Numbers are: "+ Numbers[i]); 
       i++; 

      } 
      Console.ReadKey(); 
     } 
    } 
} 

。 Console.ReadKey();这应该是在循环之外。

0

我得到了解决在外面发生了什么事

+0

这似乎重复现有的答案。你应该注意那些你认为对你有帮助的答案,并且*接受*其中的一个答案。除非你声称这个答案与其他答案有所不同。 –

+0

请参阅[我应该怎么做,当有人回答我的问题](http://stackoverflow.com/help/someone-answers) –

相关问题