看板 C_Sharp 關於我們 聯絡資訊
只抓了程式碼的部分 我想要做像是可以輸入字首就抓到名稱的程式 現在只是試寫就一直都無法成功執行了,C#是最近才開始用的所以我想應該有很多語法 問題希望大家可以告訴我大概要怎麼作比較好? namespace ConsoleApplication1 { class stars { public string name; public string skey; } class Program { stars star1 = new stars(); star1.name = "劉德華"; star1.skey = "ㄌㄉㄏ"; // 我想問這裡有沒有辦法把三行縮成 stats star1 = new stars("劉德華","ㄌㄉㄏ"); // 這邊的兩行的等於他都說有問題 類別、結構或介面成員宣告中無效的語彙基元'=' static void Main(string[] args) { string enterName; for (;;) { xm = Console.ReadKey(); if (string.StartsWith(star1.skey, enterName)) { Console.WriteLine("{0}", star1.name); } else { Console.WriteLine("none"); } } //主要就是輸入 索引例如我輸入ㄌㄉㄏ 跟劉德華的KEY比對發現是一樣的話 就印出找到 //的姓名 這邊我是希望就算我只輸入ㄌㄉ也能達到同樣效果所以採取StartsWith的函式 } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 175.45.25.41
LaPass:請補stars的建構子 10/24 16:25
iterator:C# 3.0 後支援 Object Initializers 10/24 17:31
iterator:stars star1=new stars(){name="劉德華",skey="ㄌㄉㄏ"}; 10/24 17:32
alinmoai:謝謝解決掉一個問題了 10/24 17:39