Printing multiplication Tables
A quick look at the one-month video, is not ah, practice too little, in order to print out a triangular table, and special tangled half an hour, and finally Baidu just know, good weak ....
Static voidMain (string[] args) {chengfatable (); Console.readkey (); } /// <summary> ///Output Multiplication Table/// </summary> Static voidchengfatable () {//i*j = n intI, J; for(i =1; I <=9; i++) { for(j =1; J <= I; J + +) {Console.Write ("{0}*{1}={2}\t", I, J, I *j); } Console.WriteLine (); } }
Defines an array of length, randomly assigns a value to the array, and allows the user to enter a number N, one line N number of output arrays int[] array = new INT[50]; Random r = new Random (); R.next ();
The line wrapping method is tangled for a while:
Idea: When the subscript equals the number entered by the user, the n+1 is the first element in the array, where the line break is exactly 1 rows n elements
Yesterday Tangled try{}catch{} method really useful, tangled, solved, the impression is more profound
Static voidMain (string[] args) { int[] arr =New int[ -]; //int[] arr = new INT[5]; //for (int i = 0; i < arr. Length; i++)//{ //Console.WriteLine (Arr[i]); //} while(true)//re-enter the user as soon as the error is entered, until it is correct { Try{Console.WriteLine ("are you going to print this array in a row of numbers? "); intUsernum =Convert.ToInt32 (Console.ReadLine ()); //Assign Value int[] NewArr =Fill (arr); //line break OutputNewLine (Usernum, NEWARR); Break; } Catch{Console.WriteLine ("input Error, please re-enter"); }} console.readkey (); } /// <summary> ///assigns a value to each element in the array/// </summary> /// <param name= "n" >value</param> /// <param name= "arr" >an array to be assigned a value</param> ///returns an array of type integer Static int[] Fill (int[] arr) {Random R=NewRandom (); for(inti =0; I < arr. Length; i++) { intn = R.next (0, -); Arr[i]=N; } returnarr; } /// <summary> ///receives the number n to wrap the nth element of the array./// </summary> /// <param name= "n" >n Number of user input</param> Static voidNewLine (intNint[] arr) { for(inti =0; I < arr. Length; i++) { //the user enters a number n and outputs an array by one row n number//If the user enters 5, then the line should be wrapped after the subscript (i) is 4 if(i = = n | | I% n = =0) {Console.WriteLine (); } console.write (Arr[i]+"\ t"); } }
. NET Study notes----2015-07-08 (Basic review and Exercise 02)