C# 类不能从同一文件夹中使用
我刚刚开始学习C和Microsoft Visual Studio。我刚刚创建了一个C控制台应用程序项目,它有一个program.cs文件。我修改了它的内容如下:C# 类不能从同一文件夹中使用,c#,visual-studio,oop,C#,Visual Studio,Oop,我刚刚开始学习C和Microsoft Visual Studio。我刚刚创建了一个C控制台应用程序项目,它有一个program.cs文件。我修改了它的内容如下: using System; namespace Calculator { class Calculator { public static double DoOperation(double num1, double num2, string op) { doub
using System;
namespace Calculator
{
class Calculator
{
public static double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error
// Use a switch statement to do the math
switch (op)
{
case "a":
result = num1 + num2;
break;
case "s":
result = num1 - num2;
break;
case "m":
result = num1 * num2;
break;
case "d":
// Ask the user to enter a non-zero divisor
if (num2 != 0)
{
result = num1 / num2;
}
break;
// Return text for an incorrect option entry
default:
break;
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
// NewClass obj = NewClass(); //this line is causing problem
bool endApp = false;
// Display title as the C# console calculator app
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
while (!endApp)
{
// Declare variables and set to empty
string numInput1 = "";
string numInput2 = "";
double result = 0;
// Ask the user to type the first number
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();
double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}
// Ask the user to type the second number
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();
double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput2 = Console.ReadLine();
}
// Ask the user to choose an operator
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
string op = Console.ReadLine();
try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
Console.WriteLine("------------------------\n");
// Wait for the user to respond before closing
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;
Console.WriteLine("\n"); // Friendly linespacing
}
return;
}
}
}
现在,我想在此项目中添加另一个C文件,并在我的程序中使用该类,如下所示:
using System;
namespace Calculator
{
class Calculator
{
public static double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error
// Use a switch statement to do the math
switch (op)
{
case "a":
result = num1 + num2;
break;
case "s":
result = num1 - num2;
break;
case "m":
result = num1 * num2;
break;
case "d":
// Ask the user to enter a non-zero divisor
if (num2 != 0)
{
result = num1 / num2;
}
break;
// Return text for an incorrect option entry
default:
break;
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
// NewClass obj = NewClass(); //this line is causing problem
bool endApp = false;
// Display title as the C# console calculator app
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
while (!endApp)
{
// Declare variables and set to empty
string numInput1 = "";
string numInput2 = "";
double result = 0;
// Ask the user to type the first number
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();
double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}
// Ask the user to type the second number
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();
double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput2 = Console.ReadLine();
}
// Ask the user to choose an operator
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
string op = Console.ReadLine();
try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
Console.WriteLine("------------------------\n");
// Wait for the user to respond before closing
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;
Console.WriteLine("\n"); // Friendly linespacing
}
return;
}
}
}
使用制度
namespace Calculator
{
public class NewClass
{
public NewClass()
{
Console.WriteLine("Hello World");
}
}
}
当我试图在程序中使用NewClass时,它给出了一个错误。我已经将它添加到program.cs所在的同一个文件夹中,但它仍然没有修复。我从中得到了这个解决方案
因此,我的主要问题是:
如何在c中使用来自同一文件夹的其他类就像在java中一样,如果两个程序在同一文件夹中,它们被认为是在同一个包中,并且可以在没有任何导入语句的情况下使用。在这种情况下就不能这样做吗?
这与当前主题无关,但是我们可以在Microsoft Visual studio中创建一个项目吗?它只是一个包含一些C文件的文件夹?更具体地说,我只想像在普通IDE中一样编写C代码,如何在不创建任何项目的情况下完成?
编辑:我应该把确切的错误贴出来……我的错。
因此,当我试图运行程序时,它显示:
1>------ Build started: Project: ConsoleApp1, Configuration: Debug|AnyCPU ------
C:\Users\<SystemName>\Source\Repos\<UserName>\C-sharp-Practice-Codes\ConsoleApp1\ConsoleApp1\Program.cs(42,28,42,36): error CS1955: Non-invocable member 'NewClass' cannot be used like a method.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
为什么NewClass不可开票
如何在c中使用来自同一文件夹的其他类就像在java中一样,如果两个程序在同一文件夹中,它们被认为是在同一个包中,并且可以在没有任何导入语句的情况下使用。在这种情况下就不能这样做吗
你刚刚错过了新接线员。您与NewClass的连线应如下所示:
using System;
namespace Calculator
{
class Calculator
{
public static double DoOperation(double num1, double num2, string op)
{
double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error
// Use a switch statement to do the math
switch (op)
{
case "a":
result = num1 + num2;
break;
case "s":
result = num1 - num2;
break;
case "m":
result = num1 * num2;
break;
case "d":
// Ask the user to enter a non-zero divisor
if (num2 != 0)
{
result = num1 / num2;
}
break;
// Return text for an incorrect option entry
default:
break;
}
return result;
}
}
class Program
{
static void Main(string[] args)
{
// NewClass obj = NewClass(); //this line is causing problem
bool endApp = false;
// Display title as the C# console calculator app
Console.WriteLine("Console Calculator in C#\r");
Console.WriteLine("------------------------\n");
while (!endApp)
{
// Declare variables and set to empty
string numInput1 = "";
string numInput2 = "";
double result = 0;
// Ask the user to type the first number
Console.Write("Type a number, and then press Enter: ");
numInput1 = Console.ReadLine();
double cleanNum1 = 0;
while (!double.TryParse(numInput1, out cleanNum1))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput1 = Console.ReadLine();
}
// Ask the user to type the second number
Console.Write("Type another number, and then press Enter: ");
numInput2 = Console.ReadLine();
double cleanNum2 = 0;
while (!double.TryParse(numInput2, out cleanNum2))
{
Console.Write("This is not valid input. Please enter an integer value: ");
numInput2 = Console.ReadLine();
}
// Ask the user to choose an operator
Console.WriteLine("Choose an operator from the following list:");
Console.WriteLine("\ta - Add");
Console.WriteLine("\ts - Subtract");
Console.WriteLine("\tm - Multiply");
Console.WriteLine("\td - Divide");
Console.Write("Your option? ");
string op = Console.ReadLine();
try
{
result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
if (double.IsNaN(result))
{
Console.WriteLine("This operation will result in a mathematical error.\n");
}
else Console.WriteLine("Your result: {0:0.##}\n", result);
}
catch (Exception e)
{
Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
}
Console.WriteLine("------------------------\n");
// Wait for the user to respond before closing
Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
if (Console.ReadLine() == "n") endApp = true;
Console.WriteLine("\n"); // Friendly linespacing
}
return;
}
}
}
NewClass obj=新的NewClass;
在中阅读有关新运算符的更多信息
这与当前主题无关,但是我们可以在Microsoft Visual studio中创建一个项目吗?它只是一个包含一些C文件的文件夹?更具体地说,我只想像在普通IDE中一样编写C代码,如何在不创建任何项目的情况下完成
您可以通过两种方式完成此操作:
通过在新的项目窗口中创建空白解决方案,创建者可以了解这一点;
通过打开任何目录(如编程编辑器中的目录),您可以通过解决方案资源管理器轻松创建新文件。
错误是什么?请以文本形式发布,而不是截图形式发布。这是一个错误。你把车交给修理工,说它坏了。你认为机修工能用这些信息修好你的车吗?如果需要帮助,您需要提供具体的详细信息,说明哪些内容不起作用。您错过了新的操作员。NewClass obj=新的NewClass@我真为我愚蠢的错误感到羞愧。谢谢你指出。现在已经修好了…:你能解释一下第二个问题吗?如果你熟悉Java,你也可以这样做。只需创建一个扩展名为.cs的文本文件,并使用csc.exe编译器构建它即可。请参见: