C # Basics Little Exercise

Source: Internet
Author: User

1. Accept the input numbers via the console, place the numbers in a one-dimensional array, reverse the data processing, and print the reversed to the console application

Method One

Steps:

1. Adding data variables for receiving console input

2. Convert the received string into a one-dimensional array

3. Create a new StringBuffer object to hold the inverted one-dimensional array

4. Use a For loop to add a one-dimensional array in reverse to the StringBuffer object

5. Convert the StringBuffer object through the ToString () method to a string and print

 /// <summary>        ///The input numbers are accepted via the console, placed in a one-dimensional array and implemented as an array for rollover data processing, and then the flipped data is displayed in the console. /// </summary>        /// <param name= "Numbers" ></param>         Public Static voidZhuanhuan (stringnumbers) {            //converts the obtained number into a one-dimensional array            Char[] Array =numbers. ToString ().            ToCharArray (); //Building a StringBuilder stringStringBuilder SB =NewStringBuilder (); //inserts the array in reverse to the constructed string             for(intI=array. length;i>=0; i--) {sb.            Append (array); }            //converts a StringBuffer object into a string and prints it through the ToString () methodConsole.WriteLine ("the inverted array is: {0}", sb.        ToString ()); } 
View Code

Method Two bubble sort

Steps:

1. Adding data variables for receiving console input

2. Convert the received string into a one-dimensional array

3. Iterating through an array

4. Sorting

2. Define an employee class employee, which includes a static domain Totalsalary, implements the static constructors and instance constructors of the class, and realizes the function of outputting all employees ' salaries.

Code implementation:

//Employee ClassclassEmployee { Public Static DoubleTotalsaraly {Get;Set; }  Public DoubleSalary {Get;Set; }  Public stringEmployeeName {Get;Set; }  PublicEmployee (stringEmpName,Doublesalary) {             This. EmployeeName =EmpName;  This. Salary =salary; Employee.totalsaraly+=salary; }        StaticEmployee () {totalsaraly =0 D;}  Public Static DoubleGettotalsalarys () {returntotalsaraly; }         Public Doublegetsalary () {return  This.        Salary; }         Public Override stringToString () {returnString.Format ("the salary for {0} is: {1}. ", This. EmployeeName, This.        Salary); }    }//Main Methodemployee[] emp =Newemployee[3];emp[0] =NewEmployee ("Zhang San",5600.25); emp[1] =NewEmployee ("John Doe",8500.00); emp[2] =NewEmployee ("Harry",15000.88); Console.WriteLine ("the employee's total salary is: {0}\n", Employee.gettotalsalarys ()); Console.WriteLine ("the salary for {0} is: {1}\n", emp[0]. EmployeeName, emp[0]. Salary); Console.WriteLine (emp[1]. ToString ()); Console.WriteLine ("\n{0} 's salary is: {1}\n", emp[2]. EmployeeName, emp[2]. Salary);
View Code

3. Write a console program that includes 3 classes, where animal is the parent class, and dog and cat are subclasses derived from animal that define the sounds of various animal classes. Then, an object of 3 classes is created, which in turn shows the type and call of 3 objects.

Code implementation:

//Parent and Child classesclassAnimal { Public Virtual voidshout () {}}classCat:animal { Public Override voidShout () {Console.WriteLine ("cat bark: Meow cat meow--\n"); }    }    classDog:animal { Public Override voidShout () {Console.WriteLine ("The dog barks: Wang Bark--\n"); }    }//Main function Calllist<animal> list =NewList<animal>(); Animal Cat=NewCat (); Animal Dog=NewDog (); List.            ADD (CAT); List.            ADD (dog); foreach(Animal Ainchlist)            {a.shout (); }    
View Code

C # Basics Little Exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.