今天作了幾個小小的練習,和你們分享一下.數組
1.用*打印出等腰三角形,代碼以下:函數
static void Main(string[] args) { int n = 5; for (int i = 1; i <= n; i++)//n是指總行數 { for (int j=1;j<n-i;j++) {//打印空格數 Console.Write(" "); } for (int k = 1; k <= 2*i - 1; k++)//打印* { Console.Write("*"); } Console.WriteLine(); } }
因爲等腰三角形是1,3,5,7....,找規律就行了優化
2. 編寫一個程序,輸入一個整數,判斷其爲幾位數(例如100是三位,-99是兩位)spa
static void Main(string[] args) { string a = Console.ReadLine(); int count = 0; for (int i = 0; i < a.Length; i++)//轉成字符數組 { Console.WriteLine(a[i]); if (a[i] != '+' && a[i] != '-')//判斷符號 { count++; } } Console.WriteLine("是:{0}", count); }
3.編寫一個通用的人員類(Person),該類具備姓名(Name)、年齡(Age)、性別(Sex)、等屬性,而後對Person類繼承獲得一個學生類(Student),該類可以計算學生的5門成績的平均成績,最後在main函數中對Student類功能進行驗證;blog
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp6 { class Program { class People { public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } } class Student : People { public double Ave(params int[] nums) {//可變參數 return nums.Average();//求平局值 } } static void Main(string[] args) { Student s = new Student(); s.Name = "叢翊風"; s.Age = 21; s.Sex = "男"; int[] a = { 98, 89, 67, 75, 100 }; Console.WriteLine(s.Name + " 的年齡是:" + s.Age + " 性別是:" + s.Sex + " 成績是:" + s.Ave(a)); Console.Read(); } } }
4.寫一個方法,從鍵盤上輸入三個數,用三元運算符(?:)把最大數找出來繼承
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp6 { class Program { static int Max(int a,int b,int c) { return (a > b) ? ( a > c ? a : c ):( b > c ? b : c ); } static void Main(string[] args) { string a = Console.ReadLine(); string b = Console.ReadLine(); string c = Console.ReadLine(); int a1 = int.Parse(a); int b1 = int.Parse(b); int c1 = int.Parse(c); Console.WriteLine(Max(a1, b1, c1)); Console.Read(); } } }
以上這些題,確定有不一樣的解決方法,個人代碼也是很是的很差,可是是初學嗎,之後會優化的,但願你們多多補充get