C#:'System.IO.FileNotFoundException'和'Debug profile does not exist'



EDIT:事实证明,局部变量不是问题所在。即使在更改后,我也收到了来自Visual Studio社区的错误消息(以前使用VS代码(:

"未知模块中发生类型为"System.IO.FileNotFoundException"的未处理异常。无法加载文件或程序集"System.Runtime,Version=4.2.2.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"或其依赖项之一。系统找不到指定的文件。

如果我创建了一个新项目,并复制和粘贴代码,则会出现一条不同的消息:

在"ConsoleApp1"调试配置文件中指定的调试可执行文件"D:\Desktop\New folder\ConsoleApp1\ConsoleApp1\bin\debug\netcoreapp3.1\ConsoleApp 1.exe"不存在

还出现了一个问题,说程序不包含静态主,但之前的回复说代码可以正常工作。

旧:

在大学里,我们做了一个C#练习(代码将进一步细化(,由于某种原因,它在我的电脑上不起作用,尽管它完全相同(我甚至复制并粘贴了其他人的,但在他们的电脑上起作用,但在我的上没有(。该代码旨在接受用户的数字,并查看它是否在一个范围内,并根据输出结果显示一条消息。

由于某种原因,局部变量似乎没有被读取,因为它显示为错误的颜色,但如果您将鼠标悬停在单词上,弹出菜单会显示它是局部变量。变量是"if"之后、方括号之前的"result"(第15行(。

起初我们认为这是Visual Studio代码的问题,所以我在Visual Studio Community 2019上尝试了一下,但仍然不起作用。我已经有了相关的框架。我的老师认为我的设备有问题,你怎么看?

以下是它在VS代码中的样子

这是出现的弹出菜单

以下是调试后的问题列表

这是源代码:

using System;
namespace Csharp_learning
{
class MainDemo
{
static int ReadNumber(string prompt, int min, int max)
{
int result = 0;
do 
{
Console.Write(prompt);
string numberString = Console.ReadLine();
result = int.Parse(numberString);
if result (result > max || result < min)
Console.WriteLine("Please enter a value in the range " + min + " to " + max);
else
break;
}
while(true);
return result;
}
}
}

这是我第一次使用这个网站,很抱歉我没有正确地展示这个。

using System;
namespace Csharp_learning
{
class MainDemo
{
public static int ReadNumber(string prompt, int min, int max)
{
int result = 0;
do
{
Console.Write(prompt);
string numberString = Console.ReadLine();
result = int.Parse(numberString);
if (result > max || result < min)
Console.WriteLine("Please enter a value in the range " + min + " to " + max);
else
break;
}
while (true);
return result;
}
}
}

在if语句之前有一个额外的结果。

至于其他错误,您可以尝试下面的链接。

类型为';System.IO.FileNotFoundException';发生在未知模块中

一个问题也出现了,说程序不包含静态主,但之前的回复说代码可以正常工作

每个c控制台程序都有一个静态main。我以为你已经知道了。这个部分应该看起来有点像下面。

using System;
namespace Csharp_learning
{
class Program
{
static void Main(string[] args)
{
MainDemo.ReadNumber("Prompt input ", 1, 10);
}
}
}

这两个不同的代码块通常位于不同的文件中,但您可以尝试在下面复制,看看它是否有效。

using System;
namespace Csharp_learning
{
class MainDemo
{
public static int ReadNumber(string prompt, int min, int max)
{
int result = 0;
do
{
Console.Write(prompt);
string numberString = Console.ReadLine();
result = int.Parse(numberString);
if (result > max || result < min)
Console.WriteLine("Please enter a value in the range " + min + " to " + max);
else
break;
}
while (true);
return result;
}
}
}
namespace Csharp_learning
{
class Program
{
static void Main(string[] args)
{
MainDemo.ReadNumber("Prompt input ", 1, 10);
}
}
}

您的代码对我来说运行良好。它按预期工作。该错误表明项目文件中存在拼写错误。打开CSharp Learning.cssproj项目文件,查找不匹配或错位的括号。另一个解决方案是创建一个新项目。然后将代码复制到新项目中。

最新更新