2017-10-19 119 views
-2

嗨,我想知道是否有人可以解釋如何使這種方法顯示運行計數和平均值,而不是隻要用戶輸入完數據就顯示出來?Display running total

public void InScreen() 
{ 
    int count = 0; 
    double total = 0.0; 
    double average = 0.0; 
    double number; 

    Console.WriteLine("Enter the set of scores (enter 0 to indicate end of set)"); 

    number = double.Parse(Console.ReadLine()); 

    while(number != 0) 
    { 
     total += number; 
     count++; 
     number = double.Parse(Console.ReadLine()); 
    } 

    if (count != 0) 
     average = total/count; 

    Console.Beep(20000, 2000); 
    Console.WriteLine("The user has entered {0} scores.", count); 
    Console.WriteLine("The sum of scores entered = {0}", total); 
    Console.WriteLine("The average of scores entered = {0}", average); 
} 
+5

請接管你如何展示你的代碼更加小心。沒有縮進,它真的很難閱讀。接下來,你有什麼*嘗試*顯示運行計數和平均值?提示:你會在'while'循環中寫出輸出... –

+3

所以,只需在while循環中移動你的彙總代碼 – musefan

+0

謝謝對不起回合,更容易然後我認爲它會是 –

回答

0

只要試試這個

static void Main(string[] args) 
    { 
     try 
     { 
      StringBuilder runningtotal = new StringBuilder(); 

      int count = 0; 
      double total = 0.0; 
      double average = 0.0; 
      double number; 

      Console.WriteLine("Enter the set of scores (enter 0 to indicate end of set)"); 

      number = double.Parse(Console.ReadLine()); 
      runningtotal.Append(number.ToString()); 
      while (number != 0) 
      { 

       total += number; 
       count++; 
       number = double.Parse(Console.ReadLine()); 
       if (number!=0) 
       { 
        runningtotal.Append("+" + number.ToString()); 
       } 

      } 
      if (count != 0) 
       average = total/count; 
      Console.Beep(20000, 2000); 
      Console.WriteLine("The user has entered {0} scores.", count); 
      Console.WriteLine("The sum of scores entered = {0}", total); 
      Console.WriteLine("The average of scores entered = {0}", average); 
      Console.WriteLine(runningtotal); 
      string[] inputs = runningtotal.ToString().Split('+'); 
      Console.WriteLine("Running total"); 
      int temp=0; 
      for (int i = 0; i <inputs.Length; i++) 
      { 
       if (temp==0) 
       { 
        Console.WriteLine("{0} = {1}",inputs[i],inputs[i]); 
        temp = Convert.ToInt32(inputs[i]); 
       } 
       else 
       { 
        Console.WriteLine("{0} = {1}", inputs[i], Convert.ToInt32(inputs[i]) + temp); 
        temp = Convert.ToInt32(inputs[i]) + temp; 
       } 



      } 

      Console.ReadLine(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      Console.ReadLine(); 
     } 

     Console.ReadLine(); 
    } 

輸出截圖

enter image description here

Here