作者LaPass (LaPass)
看板C_Sharp
標題Re: [問題] C#新手連compile都不過的語法請教
時間Mon Oct 24 16:32:36 2011
namespace ConsoleApplication1
{
class stars {
public string name;
public string skey;
}
class Program
{
stars star1 = new stars();
star1.name = "劉德華";
//這兩行只准出現在function裡面
star1.skey = "ㄌㄉㄏ";
//
// 我想問這裡有沒有辦法把三行縮成 stats star1 = new stars("劉德華","ㄌㄉㄏ");
想這樣做的話請補上建構子:
public stars(string _name,string _skey)
{/*指定name、skey的值*/}
// 這邊的兩行的等於他都說有問題 類別、結構或介面成員宣告中無效的語彙基元'='
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的函式
有陣子沒寫C#了
你翻翻看MSDN看看說明怎麼寫,我覺得用StartsWith()比對好像怪怪的
//
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 175.45.25.41
推 LaPass:請補stars的建構子 10/24 16:25
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.33.58.232
→ alinmoai:加入建構子以後又有產生另外一個錯誤 10/24 17:40
→ alinmoai:他說stars 不是標記成abstract extern 或 partial 因此必 10/24 17:41
→ alinmoai:須宣告主體 請問這段是甚麼意思@@? 10/24 17:41
→ LaPass:你大概是寫成類似這種東西 stars(); 10/24 22:32
→ LaPass:我覺得你最好先看看msdn關於class的那一章.... 10/24 22:33
→ LaPass:因為包含上一篇在內,都是C#中class的基本觀念。 10/24 22:34
推 alinmoai:謝謝阿 10/25 13:50