實驗一 Visual Studio 2022內建開發環境使用及C#語言基本文法應用練習
一、實驗目的
- 熟悉內建開發環境Microsoft Visual Studio 2019的基本指令及功能鍵,熟悉常用功能菜單指令。
- 學習C#程式開發過程(編輯、編譯、生成、運作和調試等),掌握使用Visual Studio 環境開發與調試程式的方法與技巧。
- 掌握C#語言基本程式結構的特點,掌握程式控制語句的使用方法。
-
掌握C#常用類的使用,重點掌握字元串類、日期時間類和随機數類的使用。
二、實驗内容
1.編寫一個控制台程式,輸入一個學生的成績,輸出其等級(優:>=90;良:>=80;中:>=70;及格:>=60;不及格:<60)。
說明:若輸入的成績超出0-100時,輸出:“輸入成績錯誤”。
源程式一:
namespace Experiment_1
{
internal class Program
{
//這是整個作業的調用入口
static void Main(string[] args)
{
//作業一
results r=new results();
r.MyResults();//輸出相應的資訊
}
}
}
源程式二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_1
{
internal class results
{
public void MyResults()
{
Console.WriteLine("請輸入一個學生的成績");
int score = int.Parse(Console.ReadLine());
if (score > 100 || score < 0)
{
Console.WriteLine("輸入成績錯誤");
}
else if (score >= 90)
{
Console.WriteLine("優");
}
else if (score >= 80)
{
Console.WriteLine("良");
}
else if (score >= 70) { Console.WriteLine("中"); }
else if (score >= 60) { Console.WriteLine("及格"); }
else
{
Console.WriteLine("不及格");
}
}
}
}
實驗結果:
鍵盤輸入98 程式輸出 優

2.Sting類練習。
編寫一個控制台程式,接收一個長度大于3的字元串(且字元串中包含逗号“,”),完成下列功能,運作效果如圖1-1所示。
(1)輸出字元串的長度。
(2)輸出字元串中第一個出現字母a的位置(若輸入的字元串沒有字母a,則輸出“未找到字母a”)。
(3)在字元串的第3個字元後面插入子串“hello”,輸出新字元串。
(4)将字元串hello替換為“me”,輸出新字元串。
(5)以字元逗号“,”為分隔符,将字元串分離,并輸出分離後的字元串。
圖1-1 String類練習
源程式一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_1
{
internal class Program
{
//這是整個作業的調用入口
static void Main(string[] args)
{
//作業一
//results r=new results();
//r.MyResults();
//作業二
StringEx stringEx=new StringEx();
stringEx.MyStringEx();
}
}
}
源程式二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_1
{
internal class StringEx
{
public void MyStringEx()
{
// 編寫一個控制台程式,接收一個長度大于3的字元串(且字元串中包含逗号“,”),
// 完成下列功能,運作效果如圖1 - 1所示。
Console.WriteLine("請輸入長度大于3的字元串");
string str= Console.ReadLine();
Console.WriteLine("str字元串長度為: "+str.Length); //(1)輸出字元串的長度。
//(2)輸出字元串中第一個出現字母a的位置(若輸入的字元串沒有字母a,則輸出“未找到字母a”)。
if(str.IndexOf('a') > 0)
{
Console.WriteLine("字元a出現的位置是"+(str.IndexOf('a')+1));
}
else
{
Console.WriteLine("未找到字母a");
}
//(3)在字元串的第3個字元後面插入子串“hello”,輸出新字元串。
Console.WriteLine("第3個字元後面插入後的結果:" + str.Insert(2, "hello"));
//(4)将字元串hello替換為“me”,輸出新字元串。
string newstr = str.Insert(2, "hello").Replace("hello", "me");
Console.WriteLine("将字元串hello替換為“me”的結果:" + newstr);
//(5)以字元逗号“,”為分隔符,将字元串分離,并輸出分離後的字元串。
string[] newstr2 = str.Split(',');
Console.Write("分離後的字元串: ");
foreach (string a in newstr2)
{
Console.Write( a+" ");
}
Console.WriteLine("");
}
}
}
實驗結果:
3.DateTime類練習。
編寫一個控制台程式, 将目前日期和時間按要求輸出,如圖1-2所示。
提示:此題實作時采用日期時間類的Now屬性的常用方法,各方法格式為:
DateTime.Now.方法名稱(參數清單)
日期時間類的Now屬性的常用屬性,格式為:DateTime.Now.屬性名稱
源程式一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_1
{
internal class Program
{
//這是整個作業的調用入口
static void Main(string[] args)
{
//作業一
//results r=new results();
//r.MyResults();
//作業二
//StringEx stringEx=new StringEx();
//stringEx.MyStringEx();
//作業三
MyDateTime mt=new MyDateTime();
mt.ShowMyDateTime();
}
}
}
源程式二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_1
{
internal class MyDateTime
{
public void ShowMyDateTime()
{
DateTime date1 = DateTime.Now;//2022/8/23 20:29:16
int year = date1.Year;//年
int month = date1.Month;//月
int day = date1.Day;//日
Console.WriteLine("目前日期:{0}年{1}月{2}日", year, month, day);
Console.WriteLine("目前日期:{0}年{1}月{2}日", year, month, day);
Console.WriteLine("目前日期:{0}/{1}/{2}", year, month, day);
Console.WriteLine("目前日期時間" + date1);
Console.WriteLine("目前年份:{0}年", year);
Console.WriteLine("目前月份:{0}月", month);
DateTime date2 = date1.AddDays(10);
Console.WriteLine("10天後的日期:"+ date2);
}
}
}
實驗結果:
4.設計控制台程式,聲明一個學生結構類型Stud,包含學号,姓名和出生日期成員;定義Stud結構的兩個學生變量S1和S2并指派,求他們出生在星期幾及他們出生相差的天數,如圖1-3所示。
圖1-3 運作效果圖
源程式一:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_1
{
internal class Program
{
//這是整個作業的調用入口
static void Main(string[] args)
{
//作業一
//results r=new results();
//r.MyResults();
//作業二
//StringEx stringEx=new StringEx();
//stringEx.MyStringEx();
//作業三
//MyDateTime mt=new MyDateTime();
//mt.ShowMyDateTime();
//作業四
Stud stud1 = new Stud(001, "張三", new DateTime(2022, 8, 23));
Stud stud2 = new Stud(002, "李四", new DateTime(2022, 9, 1));
stud1.GetWeek();//得到星期幾
stud2.birthday();//出生日期
stud2.GetWeek();
stud1.ChaDay(stud2.GetDate());
}
}
}
源程式二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Experiment_1
{
internal class Stud
{
//設計控制台程式,聲明一個學生結構類型Stud,包含學号,姓名和出生日期成員;
// 定義Stud結構的兩個學生變量S1和S2并指派,
// 求他們出生在星期幾及他們出生相差的天數,
int id;//學号
string name;//姓名
DateTime date;//出生日期
public Stud(){}
public Stud(int id,string name, DateTime date) {
this.id = id;
this.name = name;
this.date = date;
}
public void GetWeek()
{
//求他們出生在星期幾
Console.WriteLine(this.name+" 出生在 "+this.date.DayOfWeek);
}
public void ChaDay(DateTime date)
{
//輸出相差的天數
Console.WriteLine("他們出生相差的天數"+ Math.Abs(this.date.Subtract(date).Days)+"天");
}
public DateTime GetDate()
{
return this.date;
}
public void birthday()
{
int year = this.date.Year;
int month = this.date.Month;
int day = this.date.Day;
Console.WriteLine(this.name+"的生日:{0}年{1}月{2}日",year,month,day);
}
}
}