返回列表 發帖

C# 7 308 陣列搜尋

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

1. 題目說明:
請新增一個主控台應用程式,加入C:\ANS.CSF\CS03資料夾中的CSD03.cs進行編寫。依下列題意進行作答:在陣列中尋找資料,並輸出找到的索引值,使輸出值符合題意要求。檔案名稱請另存新檔為CSA03.cs,儲存於C:\ANS.CSF\CS03資料夾,再進行評分。

2. 設計說明:
請撰寫程式,讓使用者輸入一個學生成績,輸出此成績在scores清單{ 100, 100, 95, 95, 92, 91, 90, 100, 88, 88, 87, 87, 90, 91, 85, 80, 81, 82, 82, 89 }出現的次數,若輸入文字,請輸出【error】。

3. 輸入輸出:
輸入說明
學生分數

輸出說明
在清單中出現的次數(輸出最後一行後不自動換行)

範例輸入1
95
範例輸出1
2

範例輸入2
60
範例輸出2
0

4. 評分項目:
(1) 符合設計說明輸出正確格式配分        20
May

  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 List<int> scores = new List<int> { 100, 100, 95, 95, 92, 91, 90, 100, 88, 88, 87, 87, 90, 91, 85, 80, 81, 82, 82, 89 };
  11.         static void Main(string[] args)
  12.         {
  13.             try
  14.             {
  15.                 int score = Convert.ToInt32(Console.ReadLine());

  16.                 List<int> finds = scores.FindAll(delegate (int s) {
  17.                     return s.Equals(score);
  18.                 });
  19.                 Console.Write(finds.Count.ToString());
  20.             }
  21.             catch
  22.             {
  23.                 Console.Write("error");
  24.             }
  25.             Console.ReadKey();
  26.         }

  27.   
  28.     }
  29. }
複製代碼
May

TOP

  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 List<int> scores = new List<int> { 100, 100, 95, 95, 92, 91, 90, 100, 88, 88, 87, 87, 90, 91, 85, 80, 81, 82, 82, 89 };

  9.     static void Main()
  10.     {
  11.         try
  12.         {
  13.             int socre=Convert.ToInt32(Console.ReadLine());
  14.             //查找列表中與score相同的成績必製作一張列表出來
  15.             List<int> finds = scores.FindAll(delegate (int s)
  16.             {
  17.                 return s.Equals(socre);
  18.             });
  19.             Console.WriteLine(finds.Count.ToString());
  20.         }catch
  21.         {
  22.             Console.WriteLine("error");
  23.         }
  24.         Console.ReadKey();

  25.    

  26.     }

  27. }
複製代碼
istak.teach2@gmail.com

TOP

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

  7. namespace A
  8. {
  9.     class Program//負責一部分工作的人
  10.     {
  11.         static List<int> scores = new List<int> { 100, 100, 95, 95, 92, 91, 90, 100, 88, 88, 87, 87, 90, 91, 85, 80, 81, 82, 82, 89 };

  12.         static void Main()
  13.         {
  14.             try
  15.             {
  16.                 int score = Convert.ToInt32(Console.ReadLine());
  17.                 int count = 0;
  18.                 foreach (int i in scores)
  19.                 {
  20.                     if (i == score)
  21.                         count++;
  22.                 }
  23.                 Console.WriteLine(count);
  24.             }
  25.             catch
  26.             {
  27.                 Console.WriteLine("error");
  28.             }
  29.             Console.ReadKey();

  30.         }
  31.     }
  32. }
複製代碼
istak.teach2@gmail.com

TOP

返回列表