基础知识复习+练习(带*为选做)
- 编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,接受输入后判断其等级并显示出来。判断依据如下:等级={优 (90~100分);良 (80~89分);中 (60~69分);差 (0~59分);}
- 编程输出九九乘法表。
- 定义长度50的数组,随机给数组赋值,并可以让用户输入一个数字n,按一行n个数输出数组 int[] array = new int[50]; Random r = new Random(); r.Next();
- 编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回(命名规范 骆驼命名) name s.SubString(0,1) s.SubString(1);
- 编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成大小然后返回(命名规范 帕斯卡)
- 声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。扩展(*):不使用第三个变量如何交换?
- 用方法来实现:将上题封装一个方法来做。提示:方法有两个参数n1,n2,在方法中将n1与n2进行交换,使用ref。(*)
- 请用户输入一个字符串,计算字符串中的字符个数,并输出。
- 用方法来实现:计算两个数的最大值。思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:params)。
- 用方法来实现:计算1-100之间的所有整数的和。
- 用方法来实现:计算1-100之间的所有奇数的和。
- 用方法来实现:判断一个给定的整数是否为“质数”。
- 用方法来实现:计算1-100之间的所有质数(素数)的和。
- 用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
- 用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。
- 请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
- 为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。
- 有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:请统计出该字符中“咳嗽”二字的出现次数,以及每次“咳嗽”出现的索引位置。
- 将字符串" hello world,你 好世界 ! "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你好世界 !"。
- 制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。效果如图:
-
题目内容同上题,再增加一个显示姓“王”的同学的个数,此处不考虑复姓问题。效果如图:
- 请将字符串数组{ "中国", "美国", "巴西", "澳大利亚", "加拿大" }中的内容反转。然后输出反转后的数组。不能用数组的Reverse()方法。
- 创建一个Person类,属性:姓名、性别、年龄;方法:SayHi() 。再创建一个Employee类继承Person类,扩展属性Salary,重写SayHi方法。
-
案例:使用WinForm窗体,制作一个简易计算器,默认为“请选择”。要求具有+、-、*、/功能,当用户点击“等于”按钮时,如果输入的为非数字则提示用户。效果如图:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 练习1_3 { class Program { static void Main(string[] args) { //Console.WriteLine("请输入你的考试成绩"); //int score = Convert.ToInt32(Console.ReadLine()); //for (int i = 1; i <= 9; i++) //{ // for (int j = 1; j <=9 ; j++) // { // Console.Write("{0}*{1}={2}\t", i, j, i * j); // } // Console.WriteLine(); //} //Console.ReadKey(); //定义长度50的数组,随机给数组赋值,并可以让用户输入一个数字n,按一行n个数输出数组 //int[] array = new int[50]; Random r = new Random(); r.Next(); //int[] nums = new int[50]; //Random r = new Random(); //for (int i = 0; i < nums.Length; i++) //{ // int rNumber = r.Next(0, 10); // nums[i] = rNumber; //} //Console.WriteLine("请输入一个数字"); //int n = Convert.ToInt32(Console.ReadLine()); //for (int i = 0; i < nums.Length; i++) //{ // Console.Write(nums[i]+"\t"); // if ((i + 1) % n == 0) // { // Console.WriteLine(); // } //} //Console.ReadKey(); //编写一个函数,接收一个字符串,把用户输入的字符串中的第一个字母转换成小写然后返回(命名规范 骆驼命名) //name s.SubString(0,1) s.SubString(1); //string s = "AAbcd"; //string sNew = ProcessStr(s); //Console.WriteLine(sNew); //Console.ReadKey(); int n1 = 10; int n2 = 20; //n1 = n1 - n2;//n1=-10 n2=20 //n2 = n1 + n2;//n1=-10 n2=10 //n1 = n2 - n1;// //int temp = n1; //n1 = n2; //n2 = temp; Change(ref n1, ref n2); Console.WriteLine(n1); Console.WriteLine(n2); Console.ReadKey(); } public static void Change(ref int n1,ref int n2) { int temp = n1; n1 = n2; n2 = temp; } public static string ProcessStr(string str) { string s = str.Substring(0, 1).ToLower(); return s + str.Substring(1); } public static string GetLevel(int score) { string level = null; switch (score / 10) { case 10: case 9: level = "优"; break; case 8: level = "良"; break; case 7: level = "中"; break; case 6: level = "差"; break; default: level = "不及格"; break; } #region if else-if的写法 //if (score >= 90) //{ // level = "优"; //} //else if (score >= 80) //{ // level = "良"; //} //else if (score >= 70) //{ // level = "中"; //} //else if (score >= 60) //{ // level = "差"; //} //else //{ // level = "不及格"; //} #endregion return level; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 练习8_9 { class Program { static void Main(string[] args) { //请用户输入一个字符串,计算字符串中的字符个数,并输出。 //string s = "abcdef"; //Console.WriteLine(s.Length); //Console.ReadKey(); //用方法来实现:计算两个数的最大值。 //思考:方法的参数?返回值?扩展(*):计算任意多个数间的最大值(提示:params)。 //Console.WriteLine("请输入第一个数字"); //int n1 = Convert.ToInt32(Console.ReadLine()); //Console.WriteLine("请输入第二个数字"); //int n2 = Convert.ToInt32(Console.ReadLine()); //int max = GetMax(n1, n2); //Console.WriteLine(max); int[] nums = { 1, 2, 3, 4, 5 }; int max = GetMax(1,2,3,4,5,6); Console.WriteLine(max); Console.ReadKey(); } public static int GetMax(params int[] nums) { int max = nums[0]; for (int i = 0; i < nums.Length; i++) { if (nums[i] > max) { max = nums[i]; } } return max; } public static int GetMax(int n1, int n2) { return n1 > n2 ? n1 : n2; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 练习10_14 { class Program { static void Main(string[] args) { //while (true) //{ // Console.WriteLine("请输入一个数字"); // int n = Convert.ToInt32(Console.ReadLine()); // bool b = IsPrime(n); // Console.WriteLine(b); // Console.ReadKey(); //} //用方法来实现:计算1-100之间的所有质数(素数)的和。 int sum = GetPrimeSum(); Console.WriteLine(sum); Console.ReadKey(); } public static int GetPrimeSum() { int sum = 0; for (int i = 2; i <= 100; i++) { bool b = true; for (int j = 2; j < i; j++) { if (i % j == 0) { b = false; break; } } if (b) { sum += i; } } return sum; } public static bool IsPrime(int n) { if (n < 2) { return false; } else//>=2 { for (int i = 2; i < n; i++) { if (n % i == 0) { return false; } } return true; } } public static int GetSum() { int sum = 0; for (int i =2; i < 101; i+=2) { sum += i; } return sum; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 练习15_18 { class Program { static void Main(string[] args) { //用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。 //string[] names = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" }; //string max = GetLongest(names); //Console.WriteLine(max); //Console.ReadKey(); //用方法来实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。 //要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。 // int[] numbers = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }; // double avg = GetAvg(numbers); // avg = Convert.ToDouble(avg.ToString("0.00")); // Console.WriteLine(avg); //// Console.WriteLine("{0:0.00}", avg); // Console.ReadKey(); ////请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。 //int[] nums = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }; //Array.Sort(nums);//升序 //Array.Reverse(nums);//翻转 //for (int i = 0; i < nums.Length - 1; i++) //{ // for (int j = 0; j < nums.Length-1-i; j++) // { // if (nums[j] > nums[j + 1]) // { // int temp = nums[j]; // nums[j] = nums[j + 1]; // nums[j + 1] = temp; // } // } //} //for (int i = 0; i < nums.Length; i++) //{ // Console.WriteLine(nums[i]); //} // Console.ReadKey(); //为教师编写一个程序,该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。 //int[] nums = new int[30]; //Random r = new Random(); //for (int i = 0; i < nums.Length; i++) //{ // nums[i] = r.Next(1, 101); //} //double sum = 0; //for (int i = 0; i < nums.Length; i++) //{ // sum += nums[i]; //} //double avg = sum / nums.Length; //avg = Convert.ToDouble(avg.ToString("0.00")); //Console.WriteLine(avg); //Console.ReadKey(); //string[] s=new String; } public static double GetAvg(int[] nums) { double sum = 0; for (int i = 0; i < nums.Length; i++) { sum += nums[i]; } return sum / nums.Length; } public static string GetLongest(string[] names) { string max = names[0]; for (int i = 0; i < names.Length; i++) { if (names[i].Length > max.Length) { max = names[i]; } } return max; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 练习19_22 { class Program { static void Main(string[] args) { //string str = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"; //int index = str.IndexOf("咳嗽"); //int i = 1; //Console.WriteLine("第1次出现咳嗽的位置是{0}", index); //while (index != -1) //{ // i++; // index = str.IndexOf("咳嗽", index + 1); // if (index == -1) // { // break; // } // Console.WriteLine("第{0}次找到咳嗽的位置是{1}",i,index); //} //Console.ReadKey(); //将字符串" hello world,你 好 世界 ! " //两端空格去掉,并且将其中的所有其他空格都替换成一个空格, //输出结果为:"hello world,你 好 世界 !"。 //string s = " hello world,你 好 世界 ! "; //s = s.Trim(); //string[] sNew = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //string strNew = string.Join(" ", sNew); //Console.Write(strNew); //Console.ReadKey(); //存储用户输入的姓名 //List<string> list = new List<string>(); //while (true) //{ // Console.WriteLine("请输入学员姓名,输入quit退出"); // string name = Console.ReadLine(); // if (name != "quit") // { // list.Add(name); // } // else // { // break; // } //} //int i = 0; //Console.WriteLine("您刚才一共录入了{0}个人的成绩,分别是:",list.Count); //foreach (string item in list) //{ // if (item[0] == '王') // { // i++; // } // Console.WriteLine(item); //} //Console.WriteLine("姓王的同学的个数一共有{0}个",i); //Console.ReadKey(); //请将字符串数组{ "中国", "美国", "巴西", "澳大利亚", "加拿大" }中的内容反转。然后输出反转后的数组。不能用数组的Reverse()方法。 //string[] names = { "中国", "美国", "巴西", "澳大利亚", "加拿大" }; //for (int i = 0; i < names.Length / 2; i++) //{ // string temp = names[i]; // names[i] = names[names.Length - 1 - i]; // names[names.Length - 1 - i] = temp; //} //foreach (var item in names) // Console.WriteLine(item); //if(...) //{.....} //创建一个Person类,属性:姓名、性别、年龄;方法:SayHi() 。 //再创建一个Employee类继承Person类,扩展属性Salary,重写SayHi方法。 Console.ReadKey(); } } public class Person { public string Name { get; set; } public char Gender { get; set; } public int Age { get; set; } public virtual void SayHi() { Console.WriteLine("父类打招呼"); } } public class Employee : Person { public double Salary { get; set; } public override void SayHi() { Console.WriteLine("子类重写父类"); } } }
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { comboBox1.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { try { int n1 = Convert.ToInt32(textBox1.Text.Trim()); int n2 = Convert.ToInt32(textBox2.Text.Trim()); string oper = comboBox1.SelectedItem.ToString(); switch (oper) { case "+": label1.Text = (n1 + n2).ToString(); break; case "-": label1.Text = (n1 - n2).ToString(); break; case "*": label1.Text = (n1 * n2).ToString(); break; case "/": label1.Text = (n1 / n2).ToString(); break; default: MessageBox.Show("请选择正确的操作符"); break; } } catch { MessageBox.Show("请输入正确的数字"); } } }