1. 編寫(xiě)一個(gè)控制臺(tái)應(yīng)用程序,完成下列功能。
1) 創(chuàng)建一個(gè)類(lèi),用無(wú)參數(shù)的構(gòu)造函數(shù)輸出該類(lèi)的類(lèi)名。
2) 增加一個(gè)重載的構(gòu)造函數(shù),帶有一個(gè)string類(lèi)型的參數(shù),在此構(gòu)造函數(shù)中將傳遞的字符串打印出來(lái)。
3) 在main方法中創(chuàng)建屬于這個(gè)類(lèi)的一個(gè)對(duì)象,不傳遞參數(shù)。
4) 在main方法中創(chuàng)建屬于這個(gè)類(lèi)的另一個(gè)對(duì)象,傳遞一個(gè)字符串“this is a string.”。
5) 在main方法中聲明類(lèi)型為這個(gè)類(lèi)的一個(gè)具有5個(gè)對(duì)象的數(shù)組,但不要實(shí)際創(chuàng)建分配到數(shù)組里的對(duì)象。
6) 寫(xiě)出運(yùn)行程序應(yīng)該輸出的結(jié)果。
【解答】
using system;
class test1
{
public test1()
{
console.writeline(this);
}
public test1(string str)
{
console.writeline(str);
}
public static void main()
{
test1 t1 = new test1();
test1 t2 = new test1("this is a string.");
test1[] t3 = new test1[5];
}
}
輸出結(jié)果:
test1
this is a string.
2. 編寫(xiě)一個(gè)控制臺(tái)應(yīng)用程序,定義一個(gè)類(lèi)myclass,類(lèi)中包含有public、private以及protected數(shù)據(jù)成員及方法。然后定義一個(gè)從myclass類(lèi)繼承的類(lèi)mymain,將main方法放在mymain中,在main方法中創(chuàng)建myclass類(lèi)的一個(gè)對(duì)象,并分別訪問(wèn)類(lèi)中的數(shù)據(jù)成員及方法。要求注明在試圖訪問(wèn)所有類(lèi)成員時(shí)哪些語(yǔ)句會(huì)產(chǎn)生編譯錯(cuò)誤。
【解答】
using system;
class myclass
{
public int i;
private int j;
protected int k;
public void method1()
{
console.writeline("public method.");
}
private void method2()
{
console.writeline("private method.");
}
protected void method3()
{
console.writeline("protected method.");
}
}
class mymain : myclass
{
public static void main()
{
myclass t = new myclass();
console.writeline("i={0}", t.i);
console.writeline("j={0}", t.j); //會(huì)出現(xiàn)編譯錯(cuò)誤,私有成員不允許在其它類(lèi)中訪問(wèn)
console.writeline("k={0}", t.k); //會(huì)出現(xiàn)編譯錯(cuò)誤,應(yīng)該創(chuàng)建mymain的對(duì)象,然
//后通過(guò)mymain的對(duì)象訪問(wèn)
t.method1();
t.method2(); //會(huì)出現(xiàn)編譯錯(cuò)誤,私有的方法不允許在其它類(lèi)中調(diào)用
t.method3(); //會(huì)出現(xiàn)編譯錯(cuò)誤,應(yīng)該創(chuàng)建mymain的對(duì)象,然后通過(guò)mymain的
//對(duì)象調(diào)用該方法
}
}
3. 創(chuàng)建一個(gè)類(lèi)包含有protected數(shù)據(jù)。在相同的文件里創(chuàng)建第二個(gè)類(lèi),用一個(gè)方法操縱第一個(gè)類(lèi)里的protected數(shù)據(jù)。
【解答】
using system;
class class1
{
protected int i = 5;
protected void mymethod()
{
console.writeline("protected method.");
}
}
class class2 : class1
{
private void newmethod()
{
console.writeline(this.i);
this.i += 10;
console.writeline(this.i);
}
public static void main()
{
class2 t = new class2();
t.newmethod();
}
}
4. 分別寫(xiě)出下列語(yǔ)句執(zhí)行的結(jié)果。
1) console.writeline("{0}--{0:p}good",12.34f);
2) console.writeline("{0}--{0:####}good",0);
3) console.writeline("{0}--{0:00000}good",456);
【解答】
12.34--1,234.00%good
0--good
456--00456good
5. 編寫(xiě)一個(gè)控制臺(tái)應(yīng)用程序,計(jì)算
要求精度為10-8。
【解答】
using system;
class test5
{
public static void main()
{
int n = 50;
double x = 3;
double s = 0;
double a = 1;
for (int i = 1; i <= n; i++)
{
a *= i;
s += math.pow(-1, i + 1) * math.pow(x, i) / a;
}
console.writeline("n={0},s={1:0.00000000}", n, s);
}
}
6. 編寫(xiě)一個(gè)控制臺(tái)應(yīng)用程序,接收一個(gè)長(zhǎng)度大于3的字符串,完成下列功能
1) 輸出字符串的長(zhǎng)度。
2) 輸出字符串中第一個(gè)出現(xiàn)字母a的位置。
3) 在字符串的第3個(gè)字符后面插入子串“hello”,輸出新字符串。
4) 將字符串“hello”替換為“me”,輸出新字符串。
5) 以字符“m”為分隔符,將字符串分離,并輸出分離后的字符串。
【解答】
using system;
class test6
{
public static void main()
{
string str = "";
while (str.length <= 3)
{
console.write("請(qǐng)輸入一個(gè)長(zhǎng)度大于3的字符串:");
str = console.readline();
}
//(1)
console.writeline("字符串的長(zhǎng)度為:{0}", str.length);
//(2)
int i = str.indexof('a');
if (i > -1)
{
console.writeline("第一個(gè)出現(xiàn)字母a的位置是:{0}", i);
}
else
{
console.writeline("字符串中不包含字母a。");
}
//(3)
string str1 = str.insert(3, "hello"); //在第3個(gè)(初始序號(hào)為)字符前插入hello
console.writeline("插入hello后的結(jié)果為:{0}", str1);
//(4)
string str2 = str1.replace("hello", "me");
console.writeline("將hello替換為me后的結(jié)果為:{0}", str2);
//(5)
string[] arr = str2.split('m');
console.writeline("以m為分隔符分離后的字符串有:");
for (int j = 0; j < arr.length; j++)
{
console.writeline(arr[j]);
}
}
}
本文來(lái)源于網(wǎng)頁(yè)設(shè)計(jì)愛(ài)好者web開(kāi)發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問(wèn)。