我如何从库到C#控制台调用方法 [英] How can i call a method from library to C# console
本文介绍了我如何从库到C#控制台调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在新库中创建了一个方法 这是我的代码
i created a method in a new library this is my code
namespace ClassLibrary1
{
public class Class1
{
public static bool ISprime(int prime)
{
if (prime < 2)
return false;
else if (prime == 2)
return true;
else
{
for (int i = 2; i < prime; i++)
{
if (prime % i == 0)
return false;
else
return true;
}
}
}
}
}
- 如何在控制台中将该方法称为"program.cs"
- 我遇到一个错误,提示错误2'ClassLibrary1.Class1.ISprime(int)':并非所有代码路径都返回一个值"
那是什么意思?
对不起,我是一名新程序员.
sorry i'm a new programmer.
推荐答案
1.)通过执行以下操作来调用该方法:
1.) call the method by doing the following:
ClassLibrary1.Class1.ISprime(123);
或
Class1.ISprime(123); // make sure to reference ClassLibrary1 at the top of your class
2.)您需要在方法的最后返回一些值.我还更改了一些逻辑:
2.) You need to return some value at the very end of the method. I also changed some of the logic:
public static bool ISprime(int prime)
{
if (prime == 1)
return false;
if (prime == 2)
return true;
for (int i = 2; i < Math.Sqrt(prime); ++i) {
if (prime % i == 0)
return false;
}
return true;
}
3.)回答有关逻辑上有何不同的评论.尝试运行此命令,您将看到差异.
3.) Answering comment about what's different from the logic. Try running this and you'll see the differences.
for (int n = -10; n < 10; n++)
{
if (Class1.IsPrimeCorrect(n) != Class1.IsPrimeIncorrect(n))
{
Console.WriteLine(n);
}
}
这篇关于我如何从库到C#控制台调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文