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         }
View Code

textbox5为要输入的的文本name

posted on 2014-07-23 08:17  谁盗我腾飞名  阅读(95)  评论(0)    收藏  举报

导航