1. Declare two variables: int n1 = 10, n2 = 20; exchange the two variables, and output N1 to 20, N2 to 10. Extension (*): How can I switch without the third variable?
int n1 = 10;int n2 = 20;n1 = n1 + n2;n2 = n1 - n2;n1 = n1 - n2;Console.WriteLine("n1:{0},n2:{1}", n1, n2);
2. Implement with methods: encapsulate a method for the above question. Tip: The method has two parameters, N1 and N2. In the method, the N1 and N2 are exchanged and ref is used. (*)
static void Main(string[] args) { int n1 = 10, n2 = 20; Repalce(ref n1,ref n2); Console.WriteLine("n1={0},n2={1}", n1, n2); Console.ReadKey(); } static void Repalce(ref int n1,ref int n2) { n1 = n2 - n1; n2 = n2 - n1; n1 = n2 + n1; }
3. Enter a string to calculate the number of characters in the string and output the string.
String MSG = console. Readline (); console. writeline ("number of characters: {0}", MSG. Length );
4. Use methods to calculate the maximum values of two numbers. Thinking: parameters of the method? Return Value? Extension (*): calculate the maximum value among any number (Note: Params ).
5. Use methods to calculate the sum of all integers between 1 and.
6. Use the method to calculate the sum of all odd numbers between 1 and.
Int sum = 0; For (INT I = 1; I <= 100; I ++) {if (I % 2! = 0) {continue;} sum + = I;} console. writeline ("the sum of all odd numbers between 1 and is {0}", sum );
7. Implemented Using methods: Determine whether a given integer is a "prime number ".
8. Use the method to calculate the sum of all prime numbers (prime numbers) between 1 and.
static void Main(string[] args) { int startNum = 1, endNum = 100; int sum = 0; for (int i = startNum; i <= endNum; i++) { if (IsZhishu(i)) { sum += i; } } Console.WriteLine(sum); Console.ReadKey(); } static bool IsZhishu(int num) { bool isZhishu = true; for (int i = 2; i <= (int)Math.Sqrt(num); i++) { if (num % i == 0) { isZhishu = false; } } return isZhishu;}
9. implemented Using methods: There is an integer array: {1, 3, 5, 7, 90, 2, 4, 6, 8, 10}, find the maximum value, and output. The max () method of the array cannot be called.
10. implementation using methods: There is a string array: {"Malone", "Michael Jordan", "reggimiller", "timduncan", "Kobe Bryant"}, please output the longest string.
static string GetMaxString(string[] arrNames) { string maxName = arrNames[0]; for (int i = 0; i < arrNames.Length; i++) { if (arrNames[i].Length > maxName.Length) { maxName = arrNames[i]; } } return maxName; }