C#关于质数的一些问题
今天群里有人在面试的时候,碰到一个面试题叫求素数。
1 int count = 0; 2 string value = TextBox5.Text==""?" ":TextBox5.Text;//需要判断的值 3 4 if (Regex.IsMatch(value, @"^\d*$")) 5 { 6 int i = Convert.ToInt16(value); 7 8 for (int j = 2; j < i; j++) 9 { 10 if (i % j != 0) 11 { 12 continue; 13 } 14 else 15 { 16 count++; 17 } 18 } 19 } 20 else 21 { 22 Response.Write("<script>alert('请输入一个需要判断的数字')</script>"); 23 return; 24 } 25 if (count == 0) 26 { 27 Response.Write("<script>alert('是素数')</script>"); 28 } 29 else 30 { 31 Response.Write("<script>alert('不是素数')</script>"); 32 }
textbox5为要输入的的文本name
浙公网安备 33010602011771号