Board logo

標題: C# 7 301 建立陣列 [打印本頁]

作者: may    時間: 2024-1-8 22:09     標題: C# 7 301 建立陣列

TQC+ 物件導向程式語言
最新一次更新時間:2024-01-05 14:47:06

1. 題目說明:
請新增一個主控台應用程式,加入C:\ANS.CSF\CS03資料夾中的CSD03.cs進行編寫。依下列題意進行作答:輸入五個數字,建立陣列及計算總和、平均值,使輸出值符合題意要求。檔案名稱請另存新檔為CSA03.cs,儲存於C:\ANS.CSF\CS03資料夾,再進行評分。

2. 設計說明:
專案中已提供一個名為compute()的方法,接收Main()傳遞的numbers陣列,使用者在Main()中輸入五個數字存入已宣告的numbers陣列,再將此陣列傳遞給compute()方法。
在compute()方法中計算陣列內大於50的元素總和及平均值,並輸出大於50的元素總和及平均。若輸入文字,請轉換為0;若輸入小數點,請無條件捨去至整數。
輸出的平均數如有小數點,請無條件捨去至整數。

3. 輸入輸出:
輸入說明
五個數字

輸出說明
計算大於50的元素總和及平均值(無條件捨去至整數)

範例輸入1
56.5
12
100
100
1
範例輸出1
256
85

範例輸入2
nick
0
12.35
50.75
60.12
範例輸出2
60
60

4. 評分項目:
(1) 符合設計說明輸出正確格式配分        20
作者: may    時間: 2024-1-9 09:21

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace CSA03
  7. {
  8.     class CSA03
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] numbers = new int[5];
  13.             // TODO
  14.             float number;
  15.             for (int i = 0; i < numbers.Length; i++)
  16.             {
  17.                 float.TryParse(Console.ReadLine(), out number);
  18.                 numbers[i] = (int)number;
  19.             }
  20.             compute(numbers);
  21.         }

  22.         // ==================================================
  23.         // Please write your code in the specified Method
  24.         // Do NOT change the method name, type of parameter
  25.         // ==================================================
  26.         static void compute(int[] numbers)
  27.         {
  28.             int sum = 0; int times = 0;
  29.             //TODO
  30.             for (int i = 0; i < numbers.Length; i++)
  31.             {
  32.                 if (numbers[i] > 50)
  33.                 {
  34.                     sum += numbers[i];
  35.                     times++;
  36.                 }
  37.             }

  38.             Console.WriteLine(sum.ToString());
  39.             if (times > 0)
  40.             {
  41.                 Console.WriteLine((sum / times).ToString());
  42.             }
  43.             else
  44.             {
  45.                 Console.WriteLine("0");
  46.             }
  47.             Console.ReadKey();
  48.         }


  49.     }
  50. }
複製代碼

作者: 李泳霖    時間: 2024-1-29 22:23

  1. using ConsoleApp1;
  2. using System;//程式庫呼叫
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq.Expressions;
  5. using ABC.qq;


  6. class Program//負責一部分工作的人
  7. {
  8.     static void Main()//method ..Entry Point 程式進入點
  9.     {
  10.         int[] numbers = new int[5];
  11.         float number;
  12.         for(int i=0;i<numbers.Length; i++)
  13.         {
  14.             float.TryParse(Console.ReadLine(), out number);
  15.             numbers[i] = (int)number;
  16.         }
  17.         compute(numbers);
  18.     }
  19.     static void compute(int[] numbers)
  20.     {
  21.         int sum = 0, times = 0;
  22.         for(int i=0;i< numbers.Length;i++)
  23.         {
  24.             if (numbers[i] >50)
  25.             {
  26.                 sum += numbers[i];
  27.                 times++;
  28.             }
  29.         }
  30.         Console.WriteLine(sum);
  31.         if(times > 0 )
  32.         {
  33.             Console.WriteLine(sum / times);
  34.         }else
  35.         {
  36.             Console.WriteLine("0");
  37.         }
  38.     }
  39. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2