已閱讀5頁,還剩27頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
-WORD格式-可編輯-C#應(yīng)用開發(fā)技術(shù)習(xí)題1.用 enum定義字節(jié)類型的方位常量, 打印出某一方位并將此方位值轉(zhuǎn)化為字節(jié)類型, 字符串類型值。分析輸出結(jié)果的原因。 回答以下問題:Enum的缺省類型是什么?直接輸出myDirection 和(byte)myDirection 有何區(qū)別。class Variablesenum orientation : bytenorth = 1,south = 2,east = 3,west = 4static void Main(string args)byte directionByte;string directionString;orientation myDirection = orientation.north;Console.WriteLine(myDirection = 0, myDirection);directionByte = (byte)myDirection;directionString = Convert.ToString(myDirection);Console.WriteLine(byte equivalent = 0, directionByte);Console.WriteLine(string equivalent = 0, directionString);Console.ReadLine();2建立使用關(guān)系運(yùn)算符和邏輯運(yùn)算符的程序文件。Main 方法中實(shí)例代碼如下static void Main(string args)Console.WriteLine(Enter an integer:);int myInt = Convert.ToInt32(Console.ReadLine();Console.WriteLine(Integer less than 10? 0, myInt 10);Console.WriteLine(Integer between 0 and 5? 0,(0 = myInt) & (myInt = 5);Console.WriteLine(Bitwise AND of Integer and 10 = 0, myInt & 10);Console.ReadLine();編譯運(yùn)行該程序。并嘗試myInt 輸入不同范圍整數(shù),非10和10時(shí)的輸出差異。3 .從鍵盤輸入兩個(gè)數(shù)進(jìn)行比較, 并定義一個(gè)字符串變量, 當(dāng)數(shù) 1小于數(shù) 2時(shí),字符串變量為 “ less than ”,當(dāng)當(dāng)數(shù) 1等小于數(shù) 2時(shí)字符串變量為 “ equalto ”,當(dāng)數(shù) 1大于數(shù) 2時(shí)字符串變量為 “ greater than ”。static void Main(string args)-string comparison;Console.WriteLine(Enter a number:);double var1 = Convert.ToDouble(Console.ReadLine();Console.WriteLine(Enter another number:);double var2 = Convert.ToDouble(Console.ReadLine();if (var1 var2)comparison = less than;elseif (var1 = var2)comparison = equal to;elsecomparison = greater than;Console.WriteLine(The first number is 0 the second number., comparison);Console.ReadLine();4.定義三個(gè)常量字符串“ karli,angelina” ,ploppy ,并從鍵盤輸入一個(gè)名字,當(dāng)名字與 “ karli”相同時(shí)輸出我們的名字相同,當(dāng)和angelina 名字相同時(shí)輸出你的名字太性感了,當(dāng)和ploppy 相同時(shí)輸出這名字真傻。static void Main(string args)const string myName = karli;const string sexyName = angelina;const string sillyName = ploppy;string name;Console.WriteLine(What is your name?);name = Console.ReadLine();switch (name.ToLower()case myName:Console.WriteLine(You have the same name as me!);break;case sexyName:Console.WriteLine(My, what a sexy name you have!);break;case sillyName:Console.WriteLine(Thats a very silly name.);break;Console.WriteLine(Hello 0!, name);Console.ReadLine();- 2 -5 for 循環(huán)語句練習(xí)(1) 程序功能要求:按5 度的增量打印出一個(gè)從攝氏溫度到華氏溫度的轉(zhuǎn)換表。static void Main(string args)double Fa,Cel;Cel =0;for(Cel=0;Cel100;Cel+=5)Fa = Cel * 9/5;Console.WriteLine(Fa);Console.ReadLine();(2)自行改造以上程序。6. while循環(huán)語句練習(xí)( 1)程序功能要求:運(yùn)行程序后從鍵盤輸入數(shù)字1/2/3 后,可顯示抽獎(jiǎng)得到的獎(jiǎng)品:恭喜你得了一輛汽車;不錯(cuò)啊,一臺(tái)筆記本電腦;沒白來,一個(gè)MP3 ;如果輸入其它數(shù)字或字符顯示 “沒有獎(jiǎng)品給你! ”。示例代碼如下:int choice;choice = Convert.ToInt32(Console.ReadLine();while (choice = 1)Console.WriteLine( 恭喜你得了一輛汽車);break;while (choice = 2)Console.WriteLine( 不錯(cuò)啊,一臺(tái)筆記本電腦);break;while (choice = 3)Console.WriteLine( 沒白來,一個(gè)MP3);break;while (choice != 1 & choice != 2 & choice != 3)Console.WriteLine( 沒有獎(jiǎng)品給你);break;( 2)改造以上程序?qū)崿F(xiàn)此功能;嘗試將 choice=1 或 2或 3中的 “=”改為一個(gè) “=”,看效果如何?并分析錯(cuò)誤。- 3 -7 do while 循環(huán)語句練習(xí)程序功能要求: 輸入你現(xiàn)有的存款和當(dāng)前的年利率及你期望將來得到的存款,計(jì)算出存款多少年后才可以變成你期望的存款額。注意,若為一年輸出year為 year,若為多年輸出year為years。參考代碼如下:static void Main(string args)double balance, interestRate, targetBalance; Console.WriteLine(What is your current balance?); balance = Convert.ToDouble(Console.ReadLine(); Console.WriteLine(What is your current annual interest rate (in %)?); interestRate = 1 + Convert.ToDouble(Console.ReadLine() / 100.0; Console.WriteLine(What balance would you like to have?); targetBalance = Convert.ToDouble(Console.ReadLine();int totalYears = 0;dobalance *= interestRate;+totalYears;while (balance targetBalance);Console.WriteLine(In 0 year1 youll have a balance of 2., totalYears, totalYears = 1 ? : s, balance);8使用 if.else 語句編寫以下程序( 1)程序功能要求:使用 if.else 語句構(gòu)造多分支,判斷某一年是否為閏年。閏年的條件是符合下面二者之一:能被 4 整除,但不能被 100 整除;能被 4 整除,又能被 100 整除。9使用 switch 語句編寫以下程序在不同溫度時(shí)顯示不同的解釋說明:有點(diǎn)冷,多穿衣服;正合適,出去玩吧;太熱了,開空調(diào)。10. 用 do while 語句實(shí)現(xiàn)程序功能:求 1 2 +100 之和,并將求和表達(dá)式與所求的和顯示出來。11. 定義一個(gè)圓類,計(jì)算圓的面積和周長public class circlepublic static void Main()double radium, delimeter, square;const double pai = 3.1415926;radium = Convert.ToInt32(Console.ReadLine();delimeter = 2 * pai * radium;- 4 -square = pai * pai * radium;Console.WriteLine(delimeter=0,square=1, delimeter, square);Console.ReadLine();或者:public class circledoubledelimeter, square;const double pai = 3.1415926;public void calculate(double rad)delimeter = 2 * pai * rad;square = pai * pai * rad;Console.WriteLine(delimeter=0,square=1,delimeter,square);public static void Main()double radium;circle cir = new circle();radium = Convert.ToInt32(Console.ReadLine();cir.calculate(radium);Console.ReadLine();請(qǐng)比較以上兩個(gè)程序,看起來后一個(gè)程序把問題復(fù)雜化了,是不是不如第一個(gè)程序好,它從設(shè)計(jì)思想上有什么優(yōu)勢么?12. 程序要求如下:其中有 3 個(gè)數(shù)據(jù)成員有學(xué)號(hào)、姓名、年齡,以及若干成員函數(shù)。同時(shí)編寫主函數(shù)使用這個(gè)類, 實(shí)現(xiàn)對(duì)學(xué)生數(shù)據(jù)的賦值和輸出。 要求: 使用成員函數(shù)實(shí)現(xiàn)對(duì)數(shù)據(jù)的輸出;使用構(gòu)造函數(shù)實(shí)現(xiàn)對(duì)數(shù)據(jù)的輸入。參考代碼如下:public class studentsstringid,name;int age;public students(string id,string name,int age )this.id = id; = name;- 5 -this.age = age;public void Display()Console.WriteLine(id=0,name=1,age=2,id,name,age);public static void Main()/string id, name;/int age;students stu = new students(0001,zhangsan,16);stu.Display();Console.ReadLine();以上程序使用了構(gòu)造方法,請(qǐng)回答關(guān)鍵字this 有何作用,你能將成員函數(shù)Display 修改成別的代碼也實(shí)現(xiàn)響應(yīng)的功能么?13. 編寫帳戶類, 對(duì)每一賬號(hào)賦值帳戶并設(shè)置初始化存款為0.00 元,設(shè)計(jì)一變量統(tǒng)計(jì)賬號(hào)生成的數(shù)目。public class BankAccountstatic int totalAccountNumber=0;string BankAccountId;double initialDepositAmount = 0.00;public BankAccount(string myId)this.BankAccountId = myId;this.initialDepositAmount = 0.00;totalAccountNumber+;public void displayid()Console.WriteLine(mbaid=0,initialDepositAmount=1,this.BankAccountId,this.initialDepositAmount);public static void display()Console.WriteLine(totalAccountNumber=0, totalAccountNumber);- 6 -public class Testerpublic static void Main()BankAccount mba = new BankAccount(37000001);BankAccount mba2 = new BankAccount(3700002);BankAccount mba3 = new BankAccount();BankAccount mba4 = new BankAccount(3700004);/ Console.WriteLine(mba2ID=0, mba2.BankAccountId); mba2.displayid();BankAccount.display();Console.ReadLine();請(qǐng)回答問題:( 1)按你自己的算法修改以上程序,比如可只輸出生成的賬戶數(shù)。( 2)把注釋去掉后會(huì)怎樣,為什么?( 3)為什么 display 用類名直接引用,可以用對(duì)象來引用么?嘗試輸出結(jié)果。( 4)類的靜態(tài)變量和非靜態(tài)變量的引用區(qū)別。判斷一下語句的正確性:靜態(tài)方法只能使用靜態(tài)變量,不能使用實(shí)例變量。因?yàn)閷?duì)象實(shí)例化之前,實(shí)例變量不可用。這個(gè)觀點(diǎn)真確么?()類的靜態(tài)變量只有一個(gè)版本,所有實(shí)例對(duì)象引用的都是同一個(gè)版本。()對(duì)象實(shí)例化后,每個(gè)實(shí)例變量都被制作了一個(gè)副本,它們之間互不影響。()14. 程序首先給整型變量x 和y 賦初值 3,5,然后使用傳值調(diào)用方式調(diào)用方法對(duì)x 和 y 做乘方并及輸出 x 和 y 的乘方值,最后輸出x和 y得值。再將此方法給為對(duì)象調(diào)用加ref修飾查看輸出結(jié)果差異。參考代碼如下:public class Power/ public void MyPower(ref int x, ref int y)public void MyPower(int x, int y)x = 1; y = 2;Console.WriteLine(x=0,y=1, x, y);Console.WriteLine(x*x=0,y*y=1,x*x,y*y);public class Testerpublic static void Main()int x, y;- 7 -x = 3; y = 5;Power mp = new Power();/ mp.MyPower(ref x,ref y); mp.MyPower(x,y); Console.WriteLine(x=0,y=1,x,y); Console.ReadLine();思考:( 1)將響應(yīng)的注釋修改再調(diào)試查看結(jié)果,分析原因。( 2)將 Main 中 x 和 y 賦初值去掉,結(jié)果會(huì)怎樣?如果 Main 中加 ref,類 Power 的方法中參數(shù)前不加 ref 又會(huì)有何變化?說明了什么?3)如果不想對(duì)x 作無用的初始化,直接作參數(shù)傳遞,怎么實(shí)現(xiàn)?15. 編寫一個(gè)學(xué)生和教師數(shù)據(jù)輸入和顯示程序,學(xué)生數(shù)據(jù)有編號(hào)、姓名、班級(jí)和成績,教師數(shù)據(jù)有編號(hào)、姓名、職稱和部門。要求將編號(hào)、姓名輸入和顯示設(shè)計(jì)成一個(gè)類person,并作為學(xué)生數(shù)據(jù)操作類student 和教師類數(shù)據(jù)操作類teacher 的基類。參考代碼如下:public class personstring ID, name;public void personin(string id,string name)this.ID = id; = name;public void displayin()Console.WriteLine( 編號(hào): 0,ID);Console.WriteLine( 名字: 0, name);public class student : personstring classname;int grads;public student(string classname, int grads)this.classname = classname;this.grads = grads;public void displays()Console.WriteLine( 班級(jí): 0, 成績: 1,classname,grads);- 8 -public class teacher : personstring title,department;public teacher(string title, string department)this.title = title;this.department = department;public void displayt()Console.WriteLine( 職稱: 0, 部門: :1, title, department);public class Testerstatic void Main()student su = new student(0601,69);su.personin(s00001,Tom);su.displayin();su.displays();teacher tc = new teacher(lecture, IM);tc.personin(t0001,LiLi);tc.displayin();tc.displayt();Console.ReadLine();將以上程序嘗試改成通過調(diào)用基類構(gòu)造方法的方式來初始化編號(hào)和姓名,并總結(jié)調(diào)用基類構(gòu)造方法的應(yīng)用要點(diǎn)。參考代碼如下:public class personstring ID, name;public person(string id,string name)this.ID = id; = name;public void displayin()Console.WriteLine( 編號(hào): 0,ID);- 9 -Console.WriteLine( 名字: 0, name);public class student : personstring classname;int grads;public student(string sid,string sname, string classname, int grads):base(sid,sname)this.classname = classname;this.grads = grads;public void displays()Console.WriteLine( 班級(jí): 0, 成績: 1,classname,grads);public class teacher : personstring title, department;public teacher(string tid, string tname,string title, string department):base(tid,tname)this.title = title;this.department = department;public void displayt()Console.WriteLine( 職稱: 0, 部門: :1, title, department);public class Testerstatic void Main()student su = new student(s00001,Tom,0601,69);su.displayin();su.displays();teacher tc = new teacher(t0001,LiLi,lecture, IM);tc.displayin();tc.displayt();Console.ReadLine();- 10 -再嘗試將 displayin 方法歸并到基類構(gòu)造方法中去,在主函數(shù)中注釋掉對(duì)本方法的調(diào)用,可以實(shí)現(xiàn)相同的效果么?16. 編寫一個(gè)程序計(jì)算出球、圓柱和圓錐的表面積和體積。要求:定義一個(gè)基類圓,至少含有一個(gè)數(shù)據(jù)成員半徑;定義基類的派生類球、圓柱、圓錐,都含有求體積函數(shù),可以都在構(gòu)造函數(shù)中實(shí)現(xiàn), 也可以將求體積和輸出寫在一個(gè)函數(shù)中, 或者寫在兩個(gè)函數(shù)中, 請(qǐng)比較使用。定義主函數(shù),求球、圓柱、圓錐的和體積。class MyCircledouble r;public MyCircle(double r)this.r = r;class MyBall : MyCircledouble volumn;public MyBall(double r): base(r)volumn = Math.PI * r * r * r;Console.WriteLine( 球的體積是:0,volumn);class MyCylinder:MyCircledoublevolumn;public MyCylinder(double r,double h): base(r)public void calvolomn(double r,double h)volumn = Math.PI * r * r * h;public void print()Console.WriteLine( 圓柱體的體積是:0, volumn);- 11 -class MyCone:MyCircledouble h,volumn;public MyCone(double r, double h): base(r)this.h = h;public void calvolumn(double r,double h)volumn = Math.PI * r * r * h / 3;Console.WriteLine( 圓錐體的體積是:0, volumn);public class Testerpublic static void Main()Console.Write( 請(qǐng)輸入球半徑:);double r=Convert.ToDouble(Console.ReadLine();MyBall mb = new MyBall(r);Console.Write( 請(qǐng)輸入圓柱體高度:);double h = Convert.ToDouble(Console.ReadLine();MyCylinder mc = new MyCylinder(r,h);mc.calvolomn(r,h);mc.print();Console.Write( 請(qǐng)輸入圓錐體高度:);double h2 = Convert.ToDouble(Console.ReadLine();MyCone mo = new MyCone(r, h2);Console.WriteLine(h2=0,r=1,h2,r);mo.calvolumn(r,h2);Console.ReadLine();17. 多態(tài)程序練習(xí):基類 shape類是一個(gè)表示形狀的抽象類, area( )為求圖形面積的函數(shù)。請(qǐng)從 shape類派生三角形類 (triangle) 、圓類( circles)、并給出具體的求面積函數(shù),并在主函數(shù)中多態(tài)地實(shí)現(xiàn)調(diào)用。using System;namespace Variables- 12 -/public abstract class shape/ public abstract void MyArea();/public class shapepublic virtual void MyArea()Console.WriteLine(no use);public class circle : shapedouble r,carea;public circle(double r)this.r = r;publicoverride void MyArea()carea = Math.PI*r * r;Console.WriteLine( 該圖形的面積為0,carea);public class triangle : shapedouble tarea,hemiline,h;public triangle(double hemiline,double h)this.hemiline = hemiline;this.h = h;public override void MyArea()tarea = hemiline * h / 2;Console.WriteLine(hemiline=0,h=1,hemiline,h);Console.WriteLine( 該圖形的面積為0, tarea);public class Testerpublic static void Main()- 13 -shape MyShape;double r = Convert.ToDouble(Console.ReadLine();MyShape = new circle(r);MyShape.MyArea();double h = Convert.ToDouble(Console.ReadLine();double hemiline = Convert.ToDouble(Console.ReadLine();MyShape = new triangle(hemiline, h);MyShape.MyArea();Console.ReadLine();思考:(1) 將類 shape 定義為抽象類含有 MyArea 抽象方法再調(diào)試程序,查看效果,并回答抽象方法是否可以含 主體?;如果將基類中虛方法關(guān)鍵字virtual 去掉程序會(huì)怎樣?(2) 將 shape MyShape;和 MyShape = new circle(r); MyShape = new triangle(hemiline, h);三行改成 shape MyShape = new circle(r); shape MyShape = new triangle(hemiline, h); 行不行,為什么?18. 寫一個(gè)動(dòng)物基類,具有動(dòng)物的名稱變量,叫虛方法,寫出子類貓、狗、牛的叫方法,以多態(tài)的方式實(shí)現(xiàn)程序;19.程序功能要求, 創(chuàng)建三個(gè)結(jié)構(gòu)體, MyCircle,MyCylinder,MyCone 分別表示圓形、 圓柱體和圓錐體, MyCircle 包含一個(gè) int 類型的成員 r 表示半徑, MyCylinder 和 MyCone 各自包含一個(gè)MyCircel 類型的成員表示圓柱體和圓錐體的底面,成員h和 volumn (都為整型)分別表示圓柱體和圓錐體的高和體積。寫出結(jié)構(gòu)體和程序的主方法求圓柱體和圓錐體的體積。struct MyCirclepublic int r;struct MyCylinderpublic MyCircle c;public int h;publicint volumn;struct MyConepublic MyCircle c;public int h;public int volumn;public class Tester- 14 -public static void Main()Console.Write( 請(qǐng)輸入底面半徑:);MyCircle c = new MyCircle();c.r = int.Parse(Console.ReadLine();Console.Write( 請(qǐng)輸入圓柱體高度:);MyCylinder cy = new MyCylinder();cy.h = int.Parse(Console.ReadLine();cy.c = c;Console.Write( 請(qǐng)輸入圓錐體高度:);MyCone co = new MyCone();co.h = int.Parse(Console.ReadLine();co.c = c;/計(jì)算圓柱體體積double x = Math.PI * cy.c.r * cy.c.r;double y = x * cy.h;cy.volumn = (int)y;/ 計(jì)算圓錐體體積double x2 = Math.PI * co.c.r
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 生產(chǎn)經(jīng)營單位安全生產(chǎn)事故應(yīng)急預(yù)案編制
- 壓力性損傷預(yù)防管理制度
- 公共營養(yǎng)師三級(jí)考試歷年真題題庫及答案詳解
- 重癥護(hù)理培訓(xùn)基礎(chǔ)試題及答案
- 增強(qiáng)現(xiàn)實(shí)廣播融合-洞察與解讀
- 傳感器陣列識(shí)別算法-洞察與解讀
- 弱電智能化工程師專業(yè)技能考核指南試題
- 2025年電梯安全知識(shí)培訓(xùn)效果評(píng)估試卷
- 品牌營銷方案實(shí)戰(zhàn)檢驗(yàn)試題及真題
- 2026年云計(jì)算運(yùn)維工程師技術(shù)資格考核試題沖刺卷
- 2026年標(biāo)準(zhǔn)版離婚協(xié)議書(有財(cái)產(chǎn))
- 養(yǎng)老院電氣火災(zāi)培訓(xùn)課件
- 中國家庭財(cái)富與消費(fèi)報(bào)告2025年第三季度
- 馬年猜猜樂(馬的成語)打印版
- 合肥新鑫人力資源服務(wù)有限公司介紹企業(yè)發(fā)展分析報(bào)告
- 2025年金融控股公司行業(yè)分析報(bào)告及未來發(fā)展趨勢預(yù)測
- 質(zhì)量控制計(jì)劃模板全行業(yè)適用
- 數(shù)字交互視角下普寧英歌舞傳承創(chuàng)新研究
- (標(biāo)準(zhǔn))檔口轉(zhuǎn)讓合同協(xié)議書模版
- 杭州物業(yè)外賣管理辦法
- 紅外線治療的操作流程講課件
評(píng)論
0/150
提交評(píng)論