我是C#的新手,在我的教科书中它要求我在Program.cs类的main方法中添加一些代码,但是没有告诉我如何。我是一个初学者,所以我只是在寻找基础知识,当我出去的时候,我会接受更高级的课程,所以请保持你的解释彻底,但是一直到第一天。以下是我提供的代码。它不断向我提供错误<
下面的代码如下:我应该将公共静态语音TestIfElse方法添加到Program.cs类中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ifelse_Statement
{
class Program
{
static void Main(string[] args)
{
TestIfElse(10);
public static void TestIfElse(int n)
{
if (n < 10)
{
Console.WriteLine(“n is less than 10”);
}
else if (n < 20)
{
Console.WriteLine(“n is less than 20”);
}
else if (n < 30)
{
Console.WriteLine(“n is less than 30”);
}
else
{
Console.WriteLine(“n is greater than or equal to 30”);
}
}
}
}
}
答案 0 :(得分:3)
您的错误很简单 - 您无法在C#中使用嵌套函数。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ifelse_Statement
{
class Program
{
static void Main(string[] args)
{
TestIfElse(10);
}
public static void TestIfElse(int n)
{
if (n < 10)
{
Console.WriteLine(“n is less than 10”);
}
else if (n < 20)
{
Console.WriteLine(“n is less than 20”);
}
else if (n < 30)
{
Console.WriteLine(“n is less than 30”);
}
else
{
Console.WriteLine(“n is greater than or equal to 30”);
}
}
}
}
答案 1 :(得分:0)
您应该将TestIfElse方法的主体移出Main方法的主体。这样他们都被认为是班级的方法,并将按照需要工作。
此外,在您的逻辑中,您需要检查范围,即小于20的数字也小于30,因此您可能想要检查数字是否在10到20之间,或20到30之间,依此类推。
static void Main(string[] args)
{
TestIfElse(10);
}
public static void TestIfElse(int n)
{
if (n < 10)
{
Console.WriteLine(“n is less than 10”);
}
else if (n < 20)
{
Console.WriteLine(“n is less than 20”);
}
else if (n < 30)
{
Console.WriteLine(“n is less than 30”);
}
else
{
Console.WriteLine(“n is greater than or equal to 30”);
}
}
答案 2 :(得分:0)
ClassHere是类
的基本结构public class MyClass // this is the declaration of the class
{
// this is a property, it is accessible by things outside of this class.
public static string MyProperty { get; set; } ;
private static string _myField; // this is a ['private] field, it is intended to store the state of the object. it cannot be accessed from outside of this class
static void Main(string[] args)
{
// this is the method that gets run first so it make all of your initial calls
}
public static void TestIfElse(int n)
{
// this is another method (taught as module, operation, action, or subroutine in schools)
// it has return type of void which is more or less "nothing". This type of behavior simply does
// something but doesn't return a value
}
public static bool IsNotPrime(int input)
{
// this is an actual function in that will return a single value whether its a primitive value or an
// object. Whatever it is, there's ONE. The point is that a call to this function is now synonymous with
// the value it returns. So for example, if this method was real, it is equivalent to 'true' so you could
// actually say if(IsNotPrime(8)){ // do things }
return input % 2 == 0;
}
}
你必须将这些方法分开。类可以有很多成员(字段,属性,方法等),一个方法可以调用另一个,但是方法不能包含另一个。因此,当您学习识别这些时,如果您看到诸如public或static之类的关键字,您应该认为这些必须是类内部的实体,而不是其他类成员之外的实体。
例如,除了类声明本身之外,private
或private static
(或任何其他访问修饰符)在类型和标识符之前是您在声明类方法时看到的第一件事,所以不要试图在该成员中加入其他任何东西。
您使用代码时遇到的问题主要是TestIfElse
的声明。你已经在main函数内部声明了它,你不能这样做。将它移到main之外,你应该没问题。