Hey, today came early, the main problem can not solve, hope to see the pro know how the whole to me, first thank you OH:-D
< a > First review the three-dimensional expression: that is, expression 1, expression 2, expression 3;
Example: bool result = 5 > 3? True:false;
< two > Random number generation, this is not much to say, but still know the good is:
The random r=new random (); Intnumber =r.next (1, one); produces 1-10
This is a method, it feels very fun, you can try.
< three > then learned the way I studied in the cloud and college, and the teacher gave us the understanding that the method is a mechanism for reusing a bunch of code. The method is a piece of code that may have input values (parameters) and may return a value. A method is like a person who specializes in doing this, we call it to do something, it may require us to provide some data to it, it executes after completion may have some execution results to us. The requested data is called a parameter, and the result returned is the return value. As we do not know much now, so the teacher can simply let us know ...
Definition of a method: Seeing static is a static method
[Access modifier] [Static] Return value type method name ()
{
Method body;
}
• Naming rules: Method names start with uppercase, parameter names start with lowercase, parameter names, variable names make sense • Calls to methods are called in two ways for static methods • If you are in the same class, write the name directly on the line. • or the class name. Method name (); return can exit the method immediately.
I've learned a few examples today, but I'll write down the exercises I've done.
(1) Repeatedly let the user enter a number, to determine whether the number is a prime, enter Q end? The method of determining the prime numbers to implement the bool IsPrime (int number)
classProgram {Static voidMain (string[] args) {Console.WriteLine ("Please enter a number:"); intNums =int. Parse (Console.ReadLine ()); IsPrime (Nums); Console.WriteLine (IsPrime (Nums)); Console.readkey (); } Public Static BOOLIsPrime (intnum) { ints=2; stringNums =string. Empty; BOOLFlag =false; for(inti =1; I < S; i++) {flag= s% i = =0; if(Nums = ="Q") { Break; }} Console.WriteLine ("This is a prime number"); return false; } }
In method IsPrime, you need to convert num to string, which is the string nums = string. Empty; Do not know how to rewrite, resulting in the final output of the wrong, very tangled ...
(2) Use the method to find the maximum and minimum values in an array of type int
classprogram{Static voidMain (string[] args) { int[] Nums ={ at, $, A,3,2, About, -, $, -, A}; intnum =Max (nums); intNUM0 =Min (nums); Console.WriteLine ("maximum value {0}", num); Console.WriteLine ("the minimum value {0}", NUM0); Console.readkey (); } /// <summary> ///Maximum Value/// </summary> /// <param name= "Arrey" ></param> /// <returns></returns> Public Static intMax (int[] arrey) { intMax =0; for(inti =0; I < Arrey. Length; i++) { if(Max <Arrey[i]) {Max=Arrey[i]; } } returnMax; } /// <summary> ///Minimum Value/// </summary> /// <param name= "Arrey" ></param> /// <returns></returns> Public Static intMin (int[] arrey) { intMin =0; for(inti =0; I < Arrey. Length; i++) { if(Min >Arrey[i]) {min=Arrey[i]; } } returnmin; } }
This method is relatively simple, the writing method is relatively simple, the call is not a lot of confusing to make people dizzy feeling ... So feel yourself still need to work harder, write a few more methods to call the practice ...
(3) The user can only enter a number from 0 to 100 write a method otherwise prompt out of range
classProgram {Static voidMain (string[] args) {Console.WriteLine ("Please enter a number:"); intnum =int. Parse (Console.ReadLine ()); Int (num); Console.WriteLine (num); Console.readkey (); } Public Static intInt (intInteg) { if(Integ <0|| Integ > -) {Console.WriteLine ("out of range"); } returnInteg; } }
This problem is not too difficult, so just write it out ...
(4) Output a string array to the form of a | split, such as "Messi | kaka | Jong" (using the method to achieve this function)
classProgram {Static voidMain (string[] args) { string[] Nums = {"Lionel Messi","Card Card","Jong" }; Console.WriteLine (Dividing (nums)); Console.readkey (); } Public Static stringDividing (string[] Array) { stringName =string. Empty; for(inti =0; I <array. Length; i++) {Name+="|"+Array[i]; } returnname; } }
This story add separators, also can, can write, hey
These are today's exercises, tomorrow class teacher will speak, but I still like now understand, hope to help me to pull ...
< four > methods of overloading, it should be noted that: The method name is the same, but can be the method of the method of the number of arguments or different types of methods ... (as I said to myself, in a method, the return value type has no relation to the type of the parameter ...) ), and there is a method that does not define the same method name for different return value types
For example, look at the following:
These can see the overloaded instances of the method
< five > keywords out and ref, examples are as follows:
(1)
classProgram {Static voidMain (string[] args) { intx =5; ADD (refx); Console.WriteLine (x); Console.readkey (); } Static intADD (ref intx) {x++;//a value assigned to a post that is passed in. returnx; }
(2)
classProgram {Static voidMain (string[] args) { int[] Array = {1,4,5,6,7, the }; intMax; Max (Array, outmax); Console.WriteLine (max); Console.readkey (); } Static voidMax (int[] Array, out intmax) { intm =0; for(inti =0; i < Array.Length; i++) { if(Array[i] >m) {m=Array[i]; }} Max=m; } }
These two keywords are very important oh (⊙o⊙) ... And the teacher told us to remember the amount. It's written today, and there are a lot of problems to solve,.
Methods in C #, overloads of methods, and several keywords