2016-10-24 64 views
-1
class Program 
    { 
     static void Main(string[] args) 
     { 
      int sum = 0; 
      int count = 0; 

      Console.WriteLine("Please enter all the numbers you would like to add. When finished, enter -1"); 

      string numbers = Console.ReadLine(); 

      while (numbers != "-1") 
      { 
       sum += int.Parse(numbers); 
       count++; 
       Console.WriteLine(sum); 
       numbers = Console.ReadLine(); 

      } 

     } 
    } 

當我運行它時,它會立即添加數字。我需要它,所以當用戶輸入-1時,我不能關閉它。任何幫助將不勝感激。如何向哨兵循環添加用戶輸入並顯示輸入?

+0

我不明白你的問題,你的問題。 – mybirthname

+0

我需要它,所以當用戶輸入-1時,它不會讓它關閉。 –

回答

-1

在這裏你去:

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     int sum = 0; 
     int count = 0; 
     int num = 0; 

     Console.WriteLine("Please enter all the numbers you would like to add. When finished, enter -1"); 

     do 
     { 
      string buffer = Console.ReadLine(); 

      if (Int32.TryParse(buffer, out num) && num != -1) 
      { 
       sum += num; 
       count++; 
       Console.WriteLine(String.Format("Entered: [{0}], Running total over [{1}] numbers is: [{2}].", num, count, sum)); 
      } 
     } 
     while (num != -1); 
    } 
} 
+0

Downvoted?請解釋一下 –

+0

我沒有降低你的行動 – Itsallgravy

+0

我確實向你表示感謝,並感謝你的幫助。非常感謝。 – Itsallgravy