要求:重复让用户输入输入一个数,判断该数是否质数,当输入“q”时,程序运行结束!(质数的判断要求用方法来实现)。
class
Program
{
static
void
Main(
string
[] args)
{
Console.WriteLine(
"请输入一个数:"
);
string
numberString = Console.ReadLine();
while
(numberString !=
"q"
)
{
int
numberInt = Convert.ToInt32(numberString );
bool
result = PanDuan (numberInt );
if
(result ==
true
)
Console.WriteLine(
"{0}是质数"
, numberInt);
else
Console.WriteLine(
"{0}不是质数"
,numberInt );
Console.WriteLine(
"请输入一个数:"
);
numberString = Console.ReadLine();
}
Console.WriteLine(
"操作结束!!!"
);
Console.ReadKey();
}
public
static
bool
PanDuan(
int
number)
{
bool
result=
true
;
for
(
int
i = 2; i < number; i++)
{
if
(number == 2)
{
result =
true
;
break
;
}
else
if
(number % i == 0)
{
result =
false
;
break
;
}
else
{
result =
true
;
break
;
}
}
return
result;
}
}