C# Primer Plus编程练习10.17

博主分享第一个正规C#程序,是汽车游戏模拟。包含汽车类和容器类,未用异常处理等高级内容,未用ArrayList。还提到构造函数重载、get访问器、成员修改等问题,main方法用了switch和do - while语句用于测试类功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gifusing System;
None.gif
None.gif
None.gif
None.gif
namespace ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
class Car                                                //单个汽车   
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
private int position;                                //私有成员
InBlock.gif

InBlock.gif        
public Car()                                        //默认构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
this.position=0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public Car(int position)                            //带参构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
this.position=position;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int Position                                    //只读属性
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return(position);}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Move(int distance)                //移动位置,永久改变this.position值
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            this.position
+=distance;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
InBlock.gif
InBlock.gif    
class CarGame                                            //包含5个car的容器
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        
private Car[] cars;
InBlock.gif        
InBlock.gif        
public CarGame()                                    //构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            cars
=new Car[5];
InBlock.gif            
int newPosition;
InBlock.gif            
for(int i=0;i<5;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"请输入第{0}辆车的位置:",i+1);
InBlock.gif                newPosition
=Convert.ToInt32(Console.ReadLine());
InBlock.gif                cars[i]
=new Car(newPosition);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int this [int index]                            //索引    
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if((index<0)||(index>=cars.Length))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(
"index error!");
InBlock.gif                    
return 0;                                //return语句不能丢,否则会出错
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else
InBlock.gif                    
return cars[index].Position;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void CarMove(int index,int distance)            //调用元素的成员函数
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            cars[index].Move(distance);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Browse()                                //遍历cars的position成员
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
for(int i=0;i<5;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"第{0}辆车的位置是:{1}",i+1,cars[i].Position);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif   
InBlock.gif    
InBlock.gif
InBlock.gif
InBlock.gif    
class Test
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
public static int Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加代码以启动应用程序
InBlock.gif            
//
InBlock.gif
            CarGame myCarGame;
InBlock.gif            Console.WriteLine(
"5辆车,初始化其位置");
InBlock.gif            myCarGame
=new CarGame();
InBlock.gif            Console.WriteLine(
"遍历其位置");
InBlock.gif            myCarGame.Browse();
InBlock.gif
InBlock.gif            Console.WriteLine(
"***********************************************************************");
InBlock.gif
InBlock.gif            
do
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"选择任务:\nM或m,回车:移动车的位置;\nS或s,回车:检索制定车的位置\nB或b,回车:遍历所有车此刻的位置。");
InBlock.gif                
string temps=(Console.ReadLine()).ToUpper();
InBlock.gif            
InBlock.gif                
switch(temps)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
case "M":
InBlock.gif                        Console.WriteLine(
"移动车的位置");
InBlock.gif                        
do
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
int index;
InBlock.gif                            
do
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                Console.WriteLine(
"在1至5辆车中选择需要移动位置的");
InBlock.gif                                index
=Convert.ToInt32(Console.ReadLine())-1;
InBlock.gif                                
if(index>=0&&index<=4)
InBlock.gif                                    
break;
InBlock.gif                                Console.WriteLine(
"出错!请重新输入");
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
while(true);
InBlock.gif                            Console.WriteLine(
"输入需要移动的值,输入正整数向前移动,负整数向后移动,其他出错");
InBlock.gif                            
int distance=Convert.ToInt32(Console.ReadLine());
InBlock.gif                            myCarGame.CarMove(index,distance);
InBlock.gif                            Console.WriteLine(
"移动后,该辆车现在的位置是:"+myCarGame[index]);
InBlock.gif                            Console.WriteLine(
"完成移动任务了吗?按Y或y并回车完成,其他键并回车继续");
InBlock.gif                            
string temp=(Console.ReadLine()).ToUpper();
InBlock.gif                            
if(temp=="Y")
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
while(true);
InBlock.gif                        
break;
InBlock.gif                    
case "S":
InBlock.gif                        Console.WriteLine(
"检索某辆车的位置");
InBlock.gif                        
do
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
int index;
InBlock.gif                            
do
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                Console.WriteLine(
"在1至5辆车中选择需要检索位置的");
InBlock.gif                                index
=Convert.ToInt32(Console.ReadLine())-1;
InBlock.gif                                
if(index>=0&&index<=4)
InBlock.gif                                    
break;
InBlock.gif                                Console.WriteLine(
"出错!请重新输入");
ExpandedSubBlockEnd.gif                            }

InBlock.gif                            
while(true);
InBlock.gif                            Console.WriteLine(
"第{0}辆车现在的位置是:{1}",index,myCarGame[index]);
InBlock.gif                            Console.WriteLine(
"完成检索任务了吗?按Y或y并回车完成,其他键并回车继续");
InBlock.gif                            
string temp=(Console.ReadLine()).ToUpper();
InBlock.gif                            
if(temp=="Y")
InBlock.gif                                
break;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
while(true);
InBlock.gif                        
break;
InBlock.gif                    
case "B":
InBlock.gif                        Console.WriteLine(
"遍历车的位置");
InBlock.gif                        myCarGame.Browse();
InBlock.gif                        
break;
InBlock.gif                    
default:
InBlock.gif                        Console.WriteLine(
"没有这个选项,请重新选择!");
InBlock.gif                        
break;
ExpandedSubBlockEnd.gif                }

InBlock.gif                Console.WriteLine(
"\n\n\n所有的任务都完成了吗?按Y或y并回车退出,其他键并回车继续");
InBlock.gif                
string temper=(Console.ReadLine()).ToUpper();
InBlock.gif                
if(temper=="Y")
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
while(true);
InBlock.gif
InBlock.gif
InBlock.gif            
//
InBlock.gif            
//结束程序
InBlock.gif            
//
InBlock.gif
            Console.WriteLine("\nThanks for using!\nPress Enter to cancle");
InBlock.gif            Console.ReadLine();
InBlock.gif
InBlock.gif            
return(0);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


  这是我第一个正规的C#程序,因为是三、初学,还没有用异常处理以及其他更高级的东西,纯粹是练习来着。emidea.gif
  这是个汽车游戏模拟,Car是汽车类,CarGame是容器类,里面包含5个Car。其实用ArrayList更合适,但为了练习,我没有使用。
  构造函数重载用this更好,我在另一个程序里练习了。get访问器里每一个子句都要有return,不然会出错。在方法里,成员也可能被修改,比如Car.Move方法,我想可能是没有建立其拷贝。main方法里用了switch语句和do-while语句,为了是程序更“合理”,但我不喜欢这么长的main,如果没必要的话以后就不会这样了,只是拿来测试类的功能。

转载于:https://www.cnblogs.com/BlueskyGreenearth/archive/2005/03/23/124471.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值