1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace jisuan 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 Console.WriteLine("请输入2-9之间的数字:"); 14 // int num = Convert.ToInt32(Console.ReadLine()); 15 int sum = 0; 16 string num1 = Console.ReadLine(); 17 //sum = StrJS(num1); 18 sum = StrJsuan(num1); 19 //sum = Jsuan(num); 20 Console.WriteLine(sum); 21 Console.ReadLine(); 22 23 } 24 #region 方法一 25 private static int Jsuan(int num) 26 { 27 int temp = 0; 28 int sum = 0; 29 for (int i = 0; i < num; i++) 30 { 31 temp = temp * 10 + num; 32 sum += temp; 33 } 34 return sum; 35 } 36 #endregion 37 #region 方法二 38 private static int StrJsuan(string num) 39 { 40 string aa = ""; 41 int sum = 0; 42 for (int i = 0; i < Convert.ToInt32(num); i++) 43 { 44 aa += num; 45 sum += Convert.ToInt32(aa); 46 } 47 return sum; 48 } 49 #endregion 50 #region 方法三 51 private static int StrJS(string num) 52 { 53 StringBuilder ss = new StringBuilder(""); 54 int sum = 0; 55 for (int i = 0; i < Convert.ToInt32(num); i++) 56 { 57 ss.Append(num); 58 sum += Convert.ToInt32(ss.ToString()); 59 } 60 return sum; 61 } 62 #endregion 63 } 64 }