C # the next day,

Source: Internet
Author: User

C # the next day,

I have made a few small exercises today to share with you.

1. Print the isosceles triangle with *. The Code is as follows:

Static void Main (string [] args) {int n = 5; for (int I = 1; I <= n; I ++) // n indicates the total number of rows {for (int j = 1; j <n-I; j ++) {// print the number of spaces. Console. write ("") ;}for (int k = 1; k <= 2 * I-1; k ++) // print * {Console. write ("*");} Console. writeLine ();}}

Because the isosceles triangle is 1, 3, 5, 7..., just look for the rule.

2. Write a program and enter an integer to determine the number of digits (for example, 100 is three digits and-99 is two digits)

Static void Main (string [] args) {string a = Console. readLine (); int count = 0; for (int I = 0; I <. length; I ++) // converts it to an array of characters {Console. writeLine (a [I]); if (a [I]! = '+' & A [I]! = '-') // Judgment symbol {count ++ ;}} Console. WriteLine ("Yes: {0}", count );}

3. compile a common Person class with attributes such as Name, Age, Sex, and so on, then, the Person class is inherited to get a Student class (Student), which can calculate the average score of the five Student scores, and finally verify the Student class function in the main function;

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) {// Variable Parameter return nums. average (); // calculate the draw value} static void Main (string [] args) {Student s = new Student (); s. name = "congyunfeng"; s. age = 21; s. sex = "male"; int [] a = {98, 89, 67, 75,100}; Console. writeLine (s. the age of Name + "is:" + s. age + "Gender:" + s. sex + "score:" + s. ave (a); Console. read ();}}}

4. Write a method, input three numbers from the keyboard, and use the ternary operator (? :) Find the maximum number.

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();        }    }}

There are certainly different solutions to these questions. My code is also very bad, but it is a beginner and will be optimized in the future. I hope you can add more questions.

Related Article

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.