C# 并非所有的代码路径都返回值

来源:百度知道 编辑:UC知道 时间:2024/06/21 17:30:03
using System;

class Program
{
//素数的判断
public bool SuShu(int a)
{
int b = 2, c = (int)Math.Sqrt(a);

for(b=2;b<=c;b++)
{
if(a%c==0)return false;
}
if(b>=c)return true;
}

public static void Main()
{
Program App = new Program();
Console.Write("please input the num:");
int a = (int)Console.Read();
if (App.SuShu(a)) Console.WriteLine("the number you just input is a 素数!");
else Console.WriteLine("你刚输入的那个数不是一个素数!!");

}
}

我是用VS2005编的,在定义方法SuShu时,
出错提示为:
并非所有的代码路径都返回值
请问我定义这个方法哪里不对了呢??(补充:这是个素数判断程序)

方法改成:

public bool SuShu(int a)
{
int b = 2, c = (int)Math.Sqrt(a);
bool result=false;
for(b=2;b<=c;b++)
{
if(a%c==0)result=false;
}
if(b>=c)result=true;
return result;
}

using System;

class Program
{
//素数的判断
public bool SuShu(int a)
{
int b = 2, c = (int)Math.Sqrt(a);

for(b=2;b<=c;b++)
{
if(a%c==0)return false;
}
if(b>=c)return true;
return false; //这里直接加句就可以了
}

public static void Main()
{
Program App = new Program();
Console.Write("please input the num:");
int a = (int)Console.Read();
if (App.SuShu(a)) Console.WriteLine("the number you just input is a 素数!");
else Console.WriteLine("你刚输入的那个数不是一个素数!!");

}
}

一定是少个
return