標題:
C# 7 206 關鍵字出現次數
[打印本頁]
作者:
may
時間:
2024-1-8 21:53
標題:
C# 7 206 關鍵字出現次數
TQC+ 物件導向程式語言
最新一次更新時間:2024-01-05 14:41:34
1. 題目說明:
請新增一個主控台應用程式,加入C:\ANS.CSF\CS02資料夾中的CSD02.cs進行編寫。依下列題意進行作答:在指定字串中尋找關鍵字,計算此字串出現次數後輸出,使輸出值符合題意要求。檔案名稱請另存新檔為CSA02.cs,儲存於C:\ANS.CSF\CS02資料夾,再進行評分。
2. 設計說明:
請撰寫程式,讓使用者輸入一個字串,尋找並計算此字串於常數dreams短文中出現的次數,輸出次數。
3. 輸入輸出:
輸入說明
一個字串
輸出說明
字串出現次數(輸出最後一行後不自動換行)
範例輸入1
to do
範例輸出1
2
範例輸入2
when yu miss
範例輸出2
0
4. 評分項目:
(1) 符合設計說明輸出正確格式 配分 10
作者:
may
時間:
2024-1-8 23:17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSA02
{
class CSA02
{
const string dreams = "There are moments in life when you miss someone so much that " +
"you just want to pick them from your dreams and hug them for real! Dream what " +
"you want to dream;go where you want to go;be what you want to be,because you have " +
"only one life and one chance to do all the things you want to do";
static void Main(string[] args)
{
try
{
string x1 = Console.ReadLine();
int times = 0;
int pos = dreams.IndexOf(x1);
while (pos > -1)
{
times += 1;
pos = dreams.IndexOf(x1,pos + 1);
}
Console.Write(times.ToString());
}
catch
{
Console.Write("error");
}
Console.ReadKey();
}
}
}
複製代碼
作者:
李泳霖
時間:
2024-1-29 21:10
using ConsoleApp1;
using System;//程式庫呼叫
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using ABC.qq;
class Program//負責一部分工作的人
{
const string dreams = "There are moments in life when you miss someone so much that " +
"you just want to pick them from your dreams and hug them for real! Dream what " +
"you want to dream;go where you want to go;be what you want to be,because you have " +
"only one life and one chance to do all the things you want to do";
static void Main()//method ..Entry Point 程式進入點
{
try
{
string x1 = Console.ReadLine();
int times = 0;
int pos = dreams.IndexOf(x1);
while (pos > -1)
{
times++;
pos = dreams.IndexOf(x1,pos+1);//往下一個搜尋
}
Console.WriteLine(times);
}catch
{
Console.WriteLine("error");
}
}
}
/*
* ArgumentException是一個自訂「合法參數」的例外,
* ArgumentException類別所接受的合法參數,是由程式設計師自行設定的。
*
* FormatException當參數格式不符合呼叫的方法的參數規範時引發的異常.
*/
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2