C#新手第一帖
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiniDvd
{
class DVDMgr
{
请大神指教,我这代码有没有什么毛病,还能不能简化一点!
//创建对象
DVDSet set = new DVDSet();
#region 初始化DVD信息
public void Initial()
{
set.name = "罗马假日";
set.state = 0;
set.time = "2010-7-1";
set.count = 1;
set.name = "风声鹤唳";
set.state = 1;
set.name = "浪漫满屋";
set.state = 1;
}
#endregion
#region 菜单显示和切换
public void StarMenu()
{
Console.WriteLine("欢迎使用迷你DVD管理器");
Console.WriteLine("---------------------------------------------------------");
Console.WriteLine("0:借出排行榜");
Console.WriteLine("1:新增DVD");
Console.WriteLine("2:查看DVD");
Console.WriteLine("3:删除DVD");
Console.WriteLine("4:借出DVD");
Console.WriteLine("5:归还DVD");
Console.WriteLine("6:退出");
Console.WriteLine("---------------------------------------------------------");
Console.Write("请选择:");
int chose = int.Parse(Console.ReadLine());
bool tuichu=false;
switch (chose)
{
case 0:
Console.WriteLine("--->排行榜");
Console.WriteLine("*******************************");
PaiHang();//调用排行榜的方法
break;
case 1:
Console.WriteLine("--->新增DVD");
Console.WriteLine("*******************************");
Add();//调用新增DVD方法
break;
case 2:
Console.WriteLine("--->查看DVD");
Console.WriteLine("*******************************");
Search();//调用查看DVD的方法
break;
case 3:
Console.WriteLine("--->删除DVD");
Console.WriteLine("*******************************");
ShanChu();//调用删除DVD的方法
break;
case 4:
Console.WriteLine("--->借出DVD");
Console.WriteLine("*******************************");
Jie();
break;
case 5:
Console.WriteLine("--->归还DVD");
Console.WriteLine("*******************************");
ReturnDvd();
break;
case 6:
Console.WriteLine("感谢您的使用!");
tuichu = true;
break;
}
if(!tuichu)
ReturnMain();
}
#endregion
#region 返回菜单方法
public void ReturnMain()
{
Console.WriteLine("*******************************");
Console.Write("输入0返回:");
int fanhui = int.Parse(Console.ReadLine());
if (fanhui == 0)
{
StarMenu();
}
else
{
Console.WriteLine("输入错误,程序结束!");
}
}
#endregion
#region 查看DVD
public void Search()
{
Console.WriteLine("序号\t\t状态\t\t名称\t\t借出日期");
for (int i = 0; i < set.name.Length; i++)
{
if (set.name != null)
{
string state = "";
if (set.state == 0) //判断DVD是否借出
{
state = "已借出";
}
else
{
state = "可借";
}
Console.WriteLine("{0}\t\t{1}\t\t{2}\t{3}", i + 1, state, set.name, set.time);
}
}
}
#endregion
#region 新增DVD
public void Add()
{
Console.Write("请输入DVD名称:");
string name = Console.ReadLine();
bool add_dvd = false;//false表示添加失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name == null)
{
set.name = name;
Console.WriteLine("新增《{0}》成功!",name);
set.state = 1;
add_dvd = true;
break;
}
}
if (!add_dvd)
{
Console.WriteLine("新增DVD失败!");
}
}
#endregion
#region 删除DVD
public void ShanChu()
{
Console.Write("请输入名称:");
string name = Console.ReadLine();
bool shanchu_dvd = false; //false 表示删除失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name==name && set.state == 1)
{
Console.WriteLine("删除成功!");
shanchu_dvd = true;
int num = i;
for (int j = i; i < set.name.Length; j++)
{
if (set.name == null)
{
break;
}
set.name = set.name;
set.state = set.state;
set.time = set.time;
num++;
}
set.name = null;
set.state = 0;
set.time = null;
set.count = 0;
break;
}
}
if (!shanchu_dvd)
{
Console.WriteLine("删除失败,《{0}》不存在或者已经被借出", name);
}
}
#endregion
#region 借出DVD
public void Jie()
{
Console.Write("请输入DVD名称:");
string name = Console.ReadLine();
Console.Write("请输入借出日期<年-月-日>");
string time = Console.ReadLine();
bool jie_dvd = false; //false表示借出失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name == name && set.state == 1)
{
Console.WriteLine("借出《{0}》成功!",name);
jie_dvd = true;
set.state = 0;
set.time = time;
set.count++;
break;
}
else if (set.name == name && set.state == 0)
{
Console.WriteLine("借出《{0}》失败,该DVD已经被借出!",name);
jie_dvd = true;
break;
}
}
if (!jie_dvd)
{
Console.WriteLine("借出《{0}》失败,不存在该DVD",name);
}
}
#endregion
#region 归还DVD
public void ReturnDvd()
{
Console.Write("请输入DVD名称:");
string name = Console.ReadLine();
Console.Write("请输入归还日期(年-月-日):");
string time = Console.ReadLine();
bool guihuan_dvd = false; //false表示归还失败
for (int i = 0; i < set.name.Length; i++)
{
if (set.name == name && set.state == 0)
{
Console.WriteLine("归还《{0}》成功!",name);
DateTime time_jie = DateTime.Parse(set.time);
DateTime time_guihuan = DateTime.Parse(time);
int days = (time_guihuan - time_jie).Days; //计算天数差
Console.WriteLine("借出日期为{0}",set.time);
Console.WriteLine("归还日期为{0}",time);
Console.WriteLine("应付租金:{0}元(2元/天)",days*2);
set.state = 1;
set.time = null;
guihuan_dvd = true;
break;
}
else if (set.name == name && set.state == 1)
{
Console.WriteLine("归还《{0}》失败,DVD没有被借出",name);
guihuan_dvd = true;
break;
}
}
if (!guihuan_dvd)
{
Console.WriteLine("归还《{0}》失败,DVD不存在!",name);
}
}
#endregion
#region 排行榜
public void PaiHang()
{
Console.WriteLine("次数\t\t名称");
for (int i = 0; i < set.count.Length - 1; i++) //外层循环控制轮数
{
for (int j = 0; j <set.count.Length- 1 - i; j++)
{
if (set.count < set.count)
{
int temp_count = set.count;
set.count = set.count;
set.count = temp_count;
string temp_name = set.name;
set.name = set.name;
set.name = temp_name;
int temp_state = set.state;
set.state = set.state;
set.state = temp_state;
string temp_time = set.time;
set.time = set.time;
set.time = temp_time;
}
}
}
for (int i = 0; i < set.name.Length; i++)
{
if (set.name != null)
{
Console.WriteLine("{0}\t\t{1}",set.count,set.name);
}
}
}
#endregion
}
}
本帖最后由 RikuMyo 于 2018-8-19 12:17 编辑
wuaixql 发表于 2018-7-22 21:15
C#相对JAVA而言比较简单(个人认为) ,看你自己的能力,逻辑好的话可以学JAVA,逻辑能力相对差点学C#比 ...
满嘴胡言,居然说c#比java简单,还逻辑差学c#, 你有接触过c#吗?麻烦学过再来评断好么?还有学什么编程语言都需要较强的逻辑性,说话前请认真思考后再说。 wuaixql 发表于 2018-7-22 21:15
C#相对JAVA而言比较简单(个人认为) ,看你自己的能力,逻辑好的话可以学JAVA,逻辑能力相对差点学C#比 ...
你这么一说我还有点紧张,我们这学期开课就是java,不过我们上学期学的是C# 第一次前排 膜拜,刚开始接触C# 初始化,构造函数初始化好点吧 我如果输入字母会抛异常吗??? 请问楼主,C#有什么证考吗?国家级的 哩个名字好难改 发表于 2017-11-18 18:02
初始化,构造函数初始化好点吧
关键不会。。。 随遇而安8 发表于 2017-11-18 23:26
请问楼主,C#有什么证考吗?国家级的
具体我也不太清楚 不过我敢肯定肯定有的 一念天堂地狱 发表于 2017-11-18 12:36
膜拜,刚开始接触C#
学过JAVA么? wuaixql 发表于 2017-11-19 01:17
学过JAVA么?
学过了,咋了